clear_after_fork by youknowone · Pull Request #6933 · RustPython/RustPython
📝 Walkthrough
Walkthrough
Adds post-fork cleanup routines for signal state, wakeup file descriptors, and weakref locks. These functions reset inherited parent state in child processes before Python code execution, preventing stale parent signals and locks from interfering with child process behavior.
Changes
| Cohort / File(s) | Summary |
|---|---|
Signal state cleanup crates/vm/src/signal.rs, crates/vm/src/stdlib/signal.rs |
Added UNIX-only functions to reset global signal state (clear_after_fork()) and wakeup fd state (clear_wakeup_fd_after_fork()) after fork operations. |
Weakref lock cleanup crates/vm/src/object/core.rs |
Added UNIX-only functions to reset weakref stripe locks (reset_all_after_fork()) and a public wrapper (reset_weakref_locks_after_fork()) guarded by threading configuration. |
Post-fork orchestration crates/vm/src/stdlib/posix.rs |
Integrated cleanup calls into py_os_after_fork_child() to invoke signal, wakeup fd, and weakref lock resets before Python code runs in child process. |
Sequence Diagram
sequenceDiagram
participant Fork Process
participant Child Init as py_os_after_fork_child()
participant Signal Module
participant Signal FD Module
participant Weakref Module
Fork Process->>Child Init: fork() → child process starts
Child Init->>Signal Module: clear_after_fork()
Signal Module->>Signal Module: reset ANY_TRIGGERED & TRIGGERS
Signal Module-->>Child Init: ✓ signal state cleared
Child Init->>Signal FD Module: clear_wakeup_fd_after_fork()
Signal FD Module->>Signal FD Module: reset WAKEUP to INVALID_WAKEUP
Signal FD Module-->>Child Init: ✓ wakeup fd cleared
Child Init->>Weakref Module: reset_weakref_locks_after_fork()
Weakref Module->>Weakref Module: reset all stripe locks
Weakref Module-->>Child Init: ✓ weakref locks cleared
Child Init->>Child Init: run after_fork_child(vm)
Child Init-->>Fork Process: child initialized & ready
Estimated code review effort
🎯 3 (Moderate) | ⏱️ ~20 minutes
Possibly related PRs
- impl preexec_fn #6479: Modifies child-after-fork execution path and VM handling during fork operations.
- various fix to support subprocess/multiprocessing #6567: Adds RLock recursion reset to child-after-fork reinitialization, complementing this PR's other state resets.
Poem
🐰 A fork in the road, the process did split,
Locks and signals left in a bit of a fit!
Reset, reset, the rabbit did say,
Clear state before Python runs its way! 🔄
🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
| Check name | Status | Explanation |
|---|---|---|
| Description Check | ✅ Passed | Check skipped - CodeRabbit’s high-level summary is enabled. |
| Title check | ✅ Passed | The title 'clear_after_fork' directly matches the primary objective of the changes: adding fork-safe cleanup functions to reset global state (signals, weakref locks, wakeup FDs) after process forking. |
| Docstring Coverage | ✅ Passed | Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%. |
✏️ Tip: You can configure your own custom pre-merge checks in the settings.
✨ Finishing touches
- 📝 Generate docstrings
🧪 Generate unit tests (beta)
- Create PR with unit tests
- Post copyable unit tests in a comment
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 @coderabbitai help to get the list of available commands and usage tips.