🐛 fix: prefork children exit immediately in Docker containers#4133
Conversation
watchMaster() assumed PID 1 always means the parent died and the child was reparented to init. In Docker containers, the master process itself commonly runs as PID 1, causing all children to exit after the first 500ms health check. Fix by storing the original parent PID at startup and detecting parent death by checking if the parent PID changed, rather than comparing against a hardcoded value of 1. Fixes gofiber#4132 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Summary of ChangesHello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request resolves a critical bug affecting prefork child processes when running in Docker containers. Previously, child processes would prematurely exit due to an incorrect assumption about the master process's PID. The fix introduces a more robust mechanism for detecting master process termination, ensuring stable operation within containerized environments. Highlights
Changelog
Activity
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Repository UI Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (1)
WalkthroughReplaces the hardcoded PID-1 check with dynamic parent tracking: the child captures the master's PID at startup and uses it in Changes
Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 minutes Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Code Review
This pull request fixes a bug where child processes in a prefork setup would exit immediately when running inside a Docker container. The fix correctly changes the logic to check if the parent PID has changed, instead of checking if it is 1. While the core logic of the fix is correct for its intended purpose, I've identified a race condition where the child process may fail to detect the master's death if the master exits at a specific moment during child process initialization. I've left a comment with a suggestion to make the implementation more robust by eliminating this race condition.
There was a problem hiding this comment.
Pull request overview
Fixes prefork child processes exiting immediately when running inside Docker containers where the master process is commonly PID 1, by changing the master-liveness check in watchMaster() to detect PPID changes instead of comparing against a hardcoded PID value.
Changes:
- Update
watchMaster()to store the initial parent PID and exit only when the PPID changes. - Expand inline documentation to explain the reparenting-based detection approach and the Docker PID 1 scenario.
- Capture masterPID in caller before spawning goroutine to eliminate race condition window between goroutine scheduling and Getppid() call - Use masterPID for Windows FindProcess path too, for consistency - Update comment to mention subreapers, not just init/PID 1 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #4133 +/- ##
==========================================
- Coverage 91.10% 91.05% -0.06%
==========================================
Files 119 119
Lines 11385 11386 +1
==========================================
- Hits 10372 10367 -5
- Misses 642 647 +5
- Partials 371 372 +1
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
|
Congrats on merging your first pull request! 🎉 We here at Fiber are proud of you! If you need help or want to chat with us, join us on Discord https://gofiber.io/discord |
|
@meruiden How does the fasthttp prefork work? Is there the same issue there? We’re planning to switch directly to this version soon. |
|
@ReneWerner87 I checked fasthttp's prefork — it doesn't have the same issue, but it has the inverse problem. fasthttp's I've opened a PR to fix it: valyala/fasthttp#2158 It uses the same pattern as this PR — store the initial PPID at startup and exit when it changes. |
Summary
Fixes #4132
watchMaster()kills child processes whenos.Getppid() == 1, assuming the parent died and the child was reparented to init. In Docker containers, the master process commonly is PID 1, so children immediately seeGetppid() == 1and callos.Exit(1)after the first 500ms tick.Fix
Store the original parent PID at startup and detect parent death by checking if the parent PID changed, rather than comparing against a hardcoded value of 1.
Testing