X Tutup
Skip to content

Commit 35748aa

Browse files
committed
Use try_lock in py_os_after_fork_child
after_forkers_child.lock() can deadlock in the forked child if another thread held the mutex at the time of fork. Use try_lock and skip at-fork callbacks when the lock is unavailable, matching the pattern used in after_fork_child for thread_handles.
1 parent e3c533a commit 35748aa

File tree

1 file changed

+9
-1
lines changed

1 file changed

+9
-1
lines changed

crates/vm/src/stdlib/posix.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -716,7 +716,15 @@ pub mod module {
716716
vm.signal_handlers
717717
.get_or_init(crate::signal::new_signal_handlers);
718718

719-
let after_forkers_child: Vec<PyObjectRef> = vm.state.after_forkers_child.lock().clone();
719+
let after_forkers_child = match vm.state.after_forkers_child.try_lock() {
720+
Some(guard) => guard.clone(),
721+
None => {
722+
// SAFETY: After fork in child process, only the current thread
723+
// exists. The lock holder no longer exists.
724+
unsafe { vm.state.after_forkers_child.force_unlock() };
725+
vm.state.after_forkers_child.lock().clone()
726+
}
727+
};
720728
run_at_forkers(after_forkers_child, false, vm);
721729
}
722730

0 commit comments

Comments
 (0)
X Tutup