fix: ensure Python backend shm cleanup on forced shutdown#450
Conversation
Register parent-owned shm regions for atexit cleanup, remove regions explicitly in TerminateStub, and honor stub-timeout-seconds during stub teardown to avoid orphaned regions when server exit times out.
- Bump copyright to 2021-2026 on src/shm_manager.{cc,h}.
- Collapse wrapped stub_timeout_seconds_ assignment per clang-format.
Greptile SummaryThis PR fixes two root causes of orphaned
Confidence Score: 5/5Safe to merge; the core teardown logic is correct and all three issues from the first review round are addressed. The bounded teardown path (single shared budget, correct remaining-seconds accounting, final waitpid recheck) and the idempotent RemoveShmRegion flow are logically sound. The two remaining comments are about the atexit handler behaviour in a shared-library unload scenario and a missing flag-reset on an exceedingly rare atexit failure — neither affects normal operation or the primary fix. src/shm_manager.cc — the atexit registration block warrants a second look if Triton ever dynamically unloads backends via dlclose mid-session. Important Files Changed
Sequence DiagramsequenceDiagram
participant Server
participant StubLauncher
participant Stub
participant ShmManager
Server->>StubLauncher: TerminateStub()
alt is_healthy
StubLauncher->>Stub: Push(PYTHONSTUB_FinalizeRequest)
StubLauncher->>StubLauncher: Pop(pop_timeout_ms, success)
alt success within timeout
StubLauncher->>StubLauncher: compute remaining_seconds
StubLauncher->>StubLauncher: WaitForStubProcessWithTimeout(remaining_seconds)
alt stub exited in time
StubLauncher-->>StubLauncher: OK
else timeout
StubLauncher->>Stub: KillStubProcess() SIGKILL
end
else timeout / no response
StubLauncher->>Stub: KillStubProcess() SIGKILL
end
else not healthy
StubLauncher->>Stub: KillStubProcess() SIGKILL
end
StubLauncher->>ShmManager: RemoveShmRegion() idempotent
ShmManager->>ShmManager: shm_unlink + UnregisterParentShmRegion
Note over ShmManager: atexit(CleanupParentShmRegions) fires at exit for any regions not cleaned up by TerminateStub
Reviews (3): Last reviewed commit: "polish: log atexit registration failure;..." | Re-trigger Greptile |
- Enforce stub_timeout_seconds_ as a single total budget across the finalize-wait and process-exit-wait phases; healthy teardown previously could take up to 2 * stub_timeout_seconds_. - Clamp the Pop timeout to INT_MAX to avoid narrowing when passing int64_t into MessageQueue::Pop(int const&). - Re-poll waitpid once after the final sleep window in WaitForStubProcessWithTimeout so a stub that exits during that second is not force-killed unnecessarily.
- Log a warning to stderr if std::atexit registration fails so the loss of the last-resort shm cleanup is visible; primary cleanup path is unaffected. - Tighten inline comments to explain only intent/constraints, not mechanics.
What does the PR do?
Runtime fix for orphaned
triton_python_backend_shm_region_*files when Python backend stub shutdown exceeds the server exit timeout. Pairs with the QA-side change inserver#8881.Ticket
Root cause
stub-timeout-secondswas parsed at backend init but not applied insideTerminateStub— the wait on the stub was effectively unbounded.SharedMemoryManagerdestructor, so a forced/abnormal exit could skip cleanup and leak/dev/shm/triton_python_backend_shm_region_*.Fix
stub_timeout_seconds_throughStubLauncher::TerminateStubwith a bounded pop +WaitForStubProcessWithTimeout; fall back toKillStubProcesson timeout (src/stub_launcher.{cc,h}).SharedMemoryManager::RemoveShmRegion()and call it fromTerminateStub(src/shm_manager.{cc,h}).std::atexitcleanup as a safety net for forced exits.Test plan
L0_backend_python--IGX-Orinlifecycle PASSED with this PR +server#8881;/dev/shmshm-region count unchanged before/after run.L0_backend_python/lifecycle.Checklist
<type>: <description>Commit Type