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 changes the startup logic pretty significantly, to provide comprehensive protection against many clients all attempting to start the daemon at the same time.
A lockfile is used to ensure that only a single contender is responsible for either starting the daemon script, or usurping a wedged or misbehaving server.
The lockfile contains the pid of the server attempting to take over, and is checked to ensure that it STILL contains the correct pid before confirming, to handle the race when multiple contenders all try to delete a stale lockfile at the same time. That is, the lockfile used as a mutex against startup races is also subject to races, since it can be disregarded and deleted if it's stale, so that has to be protected against.
This also makes it so that it attempts to listen first, and only usurps a wedged process if the listen fails, which is more efficient in the common happy-path case, and only involves minimal waiting in the edge cases.