Add uwsgi-style process manager with heartbeat watchdog#282
Draft
Add uwsgi-style process manager with heartbeat watchdog#282
Conversation
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.
Summary
Adds a robust process supervision layer to the PySOA standalone server, modeled after uwsgi's worker management approach. The parent process now monitors child workers via a shared-memory heartbeat and automatically kills any worker that hangs mid-request, then respawns a replacement.
What changed
standalone.py— process manager overhaul--fork-processesfrom0(no forking) to1(always fork at least one worker). Use--fork 0to run directly in the current process.--ping-timeout(default 10 s): per-request watchdog; kills a child that hasn't pinged within this window.--startup-timeout(default 60 s): how long a freshly-spawned child has to send its first ping before being killed.--process-shutdown-timeout(default 30 s): grace period after SIGTERM before escalating to SIGKILL on shutdown._ProcessMonitornow uses a short-polling loop (join(timeout=1s)) instead of a blockingjoin(), enabling timely reaction to shutdown signals and hung-worker detection._kill_process()helper for SIGKILL with logging and a finaljoin()._start_process()before every (re)spawn to prevent a stale timestamp from a crashed child from instantly killing the replacement.server.py— child-side heartbeat_ping_timestamp(amultiprocessing.Value('d')supplied by the parent) and_ping_parent()which writestime.monotonic()into it.handle_next_request()calls_ping_parent()at three points: onMessageReceiveTimeout(idle), immediately after a request is dequeued, and in thefinallyblock after the request completes._validate_receive_timeout_vs_ping_timeout()classmethod raisesValueErrorat startup if the Redis transport'sreceive_timeout_in_secondsexceedsping_timeout / 2, which would cause spurious kills of healthy workers blocked in a longBLPOP.main()accepts explicit_ping_timestampand_ping_timeoutkeyword arguments so the values are available in spawn-mode child processes (Windows, macOS Python 3.13+) wheresys.argvis reset.Test coverage
test_handle_next_request.py:_ping_parentno-op without shared memory, timestamp monotonicity, ping calls on idle/receive/complete/exception, receive-timeout validation (boundary conditions, Redis-default fallback, non-Redis skip),_ping_timeoutkwarg priority oversys.argv.test_standalone.py: default fork count, CLI argument defaults,_ProcessMonitorwatchdog (stale ping → SIGKILL, fresh ping → no kill, SIGTERM → SIGKILL escalation, graceful exit → no SIGKILL, respawn after ping-kill, stale-timestamp-from-dead-child doesn't kill replacement, startup timeout, startup window protection, ping timeout takes over after first ping).