Skip to content

Commit f11d5f4

Browse files
quark-zjufacebook-github-bot
authored andcommitted
cmdpy: prevent crash on debugpython --help or --version
Summary: Command flags like `--help` and `--version` go through a code path that *partially* finalizes the Python runtime. They called `_PyRuntime_Finalize` [1] but not `Py_Finalize`. There is no public API like `Py_IsInitialized` to detect this situation and our `keepalive`'s `Py_Finalize` will crash like: *** Signal 11 (SIGSEGV) (0x0) received by PID _ (code: address not mapped to object), stack trace: *** ... PyThread_acquire_lock_timed _PyUnicode_FromId wait_for_thread_shutdown(_ts*) Py_FinalizeEx <cpython_ext::keepalive::PythonKeepAlive as core::ops::drop::Drop>::drop eden/scm/lib/cpython-ext/src/keepalive.rs:108 commands::run::run_command [1]: python/cpython@f5f336a Fix it by explicitly skipping `Py_Finalize` before calling `Py_Main`. Reviewed By: muirdm Differential Revision: D75820513 fbshipit-source-id: e7f4478ed02ad9dd4ed96026f0a4e0488a6b9d36
1 parent 1e33b1a commit f11d5f4

1 file changed

Lines changed: 8 additions & 2 deletions

File tree

eden/scm/lib/commands/cmdpy/src/hgpython.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -262,8 +262,14 @@ impl HgPython {
262262
pub fn run_python(&mut self, args: &[String], io: &clidispatch::io::IO) -> u8 {
263263
let args = Self::prepare_args(args);
264264
if self.is_python_initialized_by_us() {
265-
// Note Py_Main will call Py_Finalize too. `PythonKeepAlive::drop`
266-
// detects that and avoids calling `Py_Finalize` again.
265+
let keep_alive = self.keep_alive.take();
266+
// Command flags like `--help` and `--version` go through a code path that
267+
// *partially* finalizes the Python runtime. They called `_PyRuntime_Finalize` [1]
268+
// but not `Py_Finalize`. There is no public API like `Py_IsInitialized` to detect
269+
// this situation. Calling `Py_Finalize` by `keep_alive::drop` will segfault.
270+
// So let's forget the `keep_alive`.
271+
// [1]: https://github.com/python/cpython/commit/f5f336a819a3d881bb217bf8f9b5cacba03a4e45
272+
std::mem::forget(keep_alive);
267273
py_main(&args)
268274
} else {
269275
// If Python is not initialized by us, it's expected that this

0 commit comments

Comments
 (0)