Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid pidfile creation race #18

Merged
merged 2 commits into from
May 18, 2017

Conversation

abligh
Copy link
Contributor

@abligh abligh commented Nov 11, 2016

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:

  1. Shell process calls go program
  2. Go program forks
  3. Parent writes JSON
  4. Child consumes JSON
  5. Parent quits
  6. Shell process does something that checks for the existence of the PID file
  7. Shell process determines daemon is not running, and starts course of action in result
  8. Child writes PID file
  9. Daemon starts running

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).

Add support for getFdName() on Darwin using fcntl(F_GETPATH), as Darwin does not have /proc

Signed-off-by: Alex Bligh <[email protected]>
Avoid 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. This corrects the problem by not consuming
the JSON (and thus preventing the parent from quitting) until the lock file has been
created.

Signed-off-by: Alex Bligh <[email protected]>
@sevlyar sevlyar merged commit 3010cd3 into sevlyar:master May 18, 2017
@sevlyar
Copy link
Owner

sevlyar commented May 18, 2017

Great work. Thank you very much!

@abligh abligh deleted the avoid-pidfile-creation-race branch May 18, 2017 13:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants