driverdan/node-XMLHttpRequest — pull request #54 by max99x (2013-04-30 01:46:03+00:00)
Title: Fix race condition in synchronous requests

This resolves a race condition that can (and often does) result in truncated response text being returned for synchronous requests.

The reason this happens is that the busy loop in the main node process that monitors the output file stops as soon as it's non-empty. However, file writes are not guaranteed to be atomic, so the parent process may read the file while the child process is still writing to it, resulting in truncated output.

The fix is simple. Instead of monitoring the content of the output file, we create a separate empty sync file and monitor its existence. After the whole content has been written to the output file from the child process, we delete it, guaranteeing that the parent process won't try to access the content file until it's all written out.