Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This patch (on top of #17) avoids a race on pidfile creation by creating the pidfile before the JSON is processed.
Prior to this patch, the pidfile was created only after the JSON had been processed. This creates a race if the initiating process assumes the daemon is running if it the call to start the daemon has returned.
For instance, before the patch, this was happening around 50% of the time under OS-X:
This patch corrects the problem by not consuming the JSON (and thus preventing the parent from quitting) until the lock file has been created. As the parent now cannot quit until the child has consumed the JSON (or closed the FD by crashing), we know that the child will have created the PID file by the time the parent exits, so it safe for the called (step 6 in the above) to check for the PID file and determine the daemon is running (or at least was started).