Create the event loop after daemonizing, not before - #15
Open
stupalov wants to merge 1 commit into
Open
Conversation
--daemonize has been broken since the Python 3.14 compatibility fix: main() creates the default event loop before daemon.daemonize() forks, and an event loop does not survive fork() - its selector and self-pipe descriptors belong to the parent. The daemon binds its socket, logs "Listening", then dies in loop.run_forever() with "OSError: [Errno 9] Bad file descriptor". Because daemonize() redirects stderr to /dev/null, the traceback goes nowhere and the process just disappears moments after startup. Create the loop after the daemonize block instead, so the daemon builds its own. Nothing between the two points needs a loop.
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.
Problem
--daemonizeis broken: the responder binds its socket, logsListening at UDP/IPv4 endpoint …, and then vanishes a fraction of a second later, leaving no PID file, no traceback and no log entry beyond a normalProcess terminated.The cause is the order of two things in
main(). The Python 3.14 compatibility fix (1.2.1) creates the default event loop at the very top ofmain();daemon.daemonize()then forks twice. An asyncio event loop does not survivefork()— its selector (kqueue/epoll) and self-pipe descriptors belong to the parent — so the daemon ends up running a loop whose descriptors are invalid:daemonize()has already pointed stderr at/dev/nullby then, so the traceback is lost and the process simply disappears — which is what makes this hard to diagnose in the field. Before 1.2.1 the loop was created lazily byAsyncioDispatcher, i.e. after the fork, so daemon mode worked.Fix
Move the loop creation below the
--daemonizeblock, so the daemon creates its own loop after the fork. Nothing between the two points touches asyncio. Non-daemon startup is unaffected: the loop is still created before the dispatcher is built.The same reordering is applied to
responder_lite.py, which has the identical pattern.Verification
Before:
--daemonizeexits within a second, no PID file. After: the daemon stays up, writes its PID file, answers SNMP, andSIGTERMshuts it down cleanly and removes the PID file.Found while setting up a daemonized start script for a public simulator, where the responder kept dying seconds after startup. It is now running daemonized in production.