fix(server): add O_TRUNC when creating PID file to avoid stale content - #3598
Open
weim0000 wants to merge 1 commit into
Open
fix(server): add O_TRUNC when creating PID file to avoid stale content#3598weim0000 wants to merge 1 commit into
weim0000 wants to merge 1 commit into
Conversation
|
Hi @weim0000, Thank you for your pull request. Please review our Contributing Guide. Please make sure you understand your changes and explain your reasoning in this pull request. Low-quality pull requests may be closed. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
What problem does this change solve?
CreatePidFile opens the PID file with O_RDWR | O_CREAT but without O_TRUNC. If a stale PID file already exists from a previous run and the old PID string is longer than the new one, open() + write() will only overwrite the first N bytes of the file, leaving the trailing bytes from the old content intact. This results in a corrupted PID file.
Example:
Step File content Note
Previous run (PID=12345) 12345 correct
New run (PID=678), without O_TRUNC 67845 corrupted — trailing 45 is stale
New run (PID=678), with O_TRUNC 678 correct
A corrupted PID file can cause process management tools (e.g., kill $(cat pidfile), systemd, init scripts) to target the wrong process or fail to stop/restart the service.
How does this change fix the problem?
Add O_TRUNC to the open() flags in CreatePidFile so the file is truncated to zero length before writing the new PID, ensuring no stale bytes remain.
Changes