Skip to content

Commit fe5b7aa

Browse files
committed
Fix race condition in synchronous requests.
1 parent 3c8328f commit fe5b7aa

File tree

1 file changed

+13
-7
lines changed

1 file changed

+13
-7
lines changed

lib/XMLHttpRequest.js

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -422,6 +422,7 @@ exports.XMLHttpRequest = function() {
422422
self.dispatchEvent("loadstart");
423423
} else { // Synchronous
424424
// Create a temporary file for communication with the other Node process
425+
var contentFile = ".node-xmlhttprequest-content-" + process.pid;
425426
var syncFile = ".node-xmlhttprequest-sync-" + process.pid;
426427
fs.writeFileSync(syncFile, "", "utf8");
427428
// The async request the other Node process executes
@@ -432,28 +433,33 @@ exports.XMLHttpRequest = function() {
432433
+ "var req = doRequest(options, function(response) {"
433434
+ "response.setEncoding('utf8');"
434435
+ "response.on('data', function(chunk) {"
435-
+ "responseText += chunk;"
436+
+ " responseText += chunk;"
436437
+ "});"
437438
+ "response.on('end', function() {"
438-
+ "fs.writeFileSync('" + syncFile + "', 'NODE-XMLHTTPREQUEST-STATUS:' + response.statusCode + ',' + responseText, 'utf8');"
439+
+ "fs.writeFileSync('" + contentFile + "', 'NODE-XMLHTTPREQUEST-STATUS:' + response.statusCode + ',' + responseText, 'utf8');"
440+
+ "fs.unlinkSync('" + syncFile + "');"
439441
+ "});"
440442
+ "response.on('error', function(error) {"
441-
+ "fs.writeFileSync('" + syncFile + "', 'NODE-XMLHTTPREQUEST-ERROR:' + JSON.stringify(error), 'utf8');"
443+
+ "fs.writeFileSync('" + contentFile + "', 'NODE-XMLHTTPREQUEST-ERROR:' + JSON.stringify(error), 'utf8');"
444+
+ "fs.unlinkSync('" + syncFile + "');"
442445
+ "});"
443446
+ "}).on('error', function(error) {"
444-
+ "fs.writeFileSync('" + syncFile + "', 'NODE-XMLHTTPREQUEST-ERROR:' + JSON.stringify(error), 'utf8');"
447+
+ "fs.writeFileSync('" + contentFile + "', 'NODE-XMLHTTPREQUEST-ERROR:' + JSON.stringify(error), 'utf8');"
448+
+ "fs.unlinkSync('" + syncFile + "');"
445449
+ "});"
446450
+ (data ? "req.write('" + data.replace(/'/g, "\\'") + "');":"")
447451
+ "req.end();";
448452
// Start the other Node Process, executing this string
449453
syncProc = spawn(process.argv[0], ["-e", execString]);
450-
while((self.responseText = fs.readFileSync(syncFile, 'utf8')) == "") {
451-
// Wait while the file is empty
454+
var statusText;
455+
while(fs.existsSync(syncFile)) {
456+
// Wait while the sync file is empty
452457
}
458+
self.responseText = fs.readFileSync(contentFile, 'utf8');
453459
// Kill the child process once the file has data
454460
syncProc.stdin.end();
455461
// Remove the temporary file
456-
fs.unlinkSync(syncFile);
462+
fs.unlinkSync(contentFile);
457463
if (self.responseText.match(/^NODE-XMLHTTPREQUEST-ERROR:/)) {
458464
// If the file returned an error, handle it
459465
var errorObj = self.responseText.replace(/^NODE-XMLHTTPREQUEST-ERROR:/, "");

0 commit comments

Comments
 (0)