FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

clear_after_fork by youknowone · Pull Request #6933 · RustPython/RustPython · GitHub

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .rs  (4) All 1 file type selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
16 changes: 16 additions & 0 deletions crates/vm/src/object/core.rs
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
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,15 @@ mod weakref_lock {
core::hint::spin_loop();
}
}

/// Reset all weakref stripe locks after fork in child process.
/// Locks held by parent threads would cause infinite spin in the child.
#[cfg(unix)]
pub(crate) fn reset_all_after_fork() {
for lock in &LOCKS {
lock.store(0, Ordering::Release);
}
}
}

#[cfg(not(feature = "threading"))]
Expand All @@ -212,6 +221,13 @@ mod weakref_lock {
}
}

/// Reset weakref stripe locks after fork. Must be called before any
/// Python code runs in the child process.
#[cfg(all(unix, feature = "threading"))]
pub(crate) fn reset_weakref_locks_after_fork() {
weakref_lock::reset_all_after_fork();
}

// === WeakRefList: inline on every object (tp_weaklist) ===

pub(super) struct WeakRefList {
Expand Down
10 changes: 10 additions & 0 deletions crates/vm/src/signal.rs
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
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,16 @@ pub(crate) fn set_triggered() {
ANY_TRIGGERED.store(true, Ordering::Release);
}

/// Reset all signal trigger state after fork in child process.
/// Stale triggers from the parent must not fire in the child.
#[cfg(unix)]
pub(crate) fn clear_after_fork() {
ANY_TRIGGERED.store(false, Ordering::Release);
for trigger in &TRIGGERS {
trigger.store(false, Ordering::Relaxed);
}
}

pub fn assert_in_range(signum: i32, vm: &VirtualMachine) -> PyResult<()> {
if (1..NSIG as i32).contains(&signum) {
Ok(())
Expand Down
10 changes: 9 additions & 1 deletion crates/vm/src/stdlib/posix.rs
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
Original file line number Diff line number Diff line change
Expand Up @@ -660,8 +660,16 @@ pub mod module {
}

fn py_os_after_fork_child(vm: &VirtualMachine) {
// Reset low-level state before any Python code runs in the child.
// Signal triggers from the parent must not fire in the child.
crate::signal::clear_after_fork();
crate::stdlib::signal::_signal::clear_wakeup_fd_after_fork();

// Reset weakref stripe locks that may have been held during fork.
#[cfg(feature = "threading")]
crate::object::reset_weakref_locks_after_fork();

// Mark all other threads as done before running Python callbacks
// See _PyThread_AfterFork behavior
#[cfg(feature = "threading")]
crate::stdlib::thread::after_fork_child(vm);

Expand Down
7 changes: 7 additions & 0 deletions crates/vm/src/stdlib/signal.rs
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
Original file line number Diff line number Diff line change
Expand Up @@ -679,6 +679,13 @@ pub(crate) mod _signal {
}
}

/// Reset wakeup fd after fork in child process.
/// The child must not write to the parent's wakeup fd.
#[cfg(unix)]
pub(crate) fn clear_wakeup_fd_after_fork() {
WAKEUP.store(INVALID_WAKEUP, Ordering::Relaxed);
}

pub(crate) fn module_exec(
vm: &VirtualMachine,
module: &Py<crate::builtins::PyModule>,
Expand Down
Loading

Back | FazBrowse Home | New Git URL