Skip to content

Commit 173422f

Browse files
authored
Merge pull request #117 from StanFromIreland/tachyon-subprocesses-atomic-speed
2 parents 86cb186 + d104fe6 commit 173422f

File tree

3 files changed

+94
-11
lines changed

3 files changed

+94
-11
lines changed

Lib/profiling/sampling/_child_monitor.py

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -66,16 +66,7 @@ def is_python_process(pid):
6666
# Can't read exe link, fall through to full probe
6767
pass
6868

69-
try:
70-
# Full probe: Attempt to create a RemoteUnwinder - this will:
71-
# 1. Search for the PyRuntime section in process memory maps
72-
# 2. Read and validate debug offsets
73-
# 3. Check Python version compatibility
74-
_remote_debugging.RemoteUnwinder(pid)
75-
return True
76-
except (OSError, RuntimeError, PermissionError, ValueError):
77-
# Not a Python process or not accessible
78-
return False
69+
return _remote_debugging.is_python_process(pid)
7970

8071

8172
class ChildProcessMonitor:

Modules/_remote_debugging/clinic/module.c.h

Lines changed: 62 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Modules/_remote_debugging/module.c

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1157,8 +1157,39 @@ _remote_debugging_get_child_pids_impl(PyObject *module, int pid,
11571157
return enumerate_child_pids((pid_t)pid, recursive);
11581158
}
11591159

1160+
/*[clinic input]
1161+
_remote_debugging.is_python_process
1162+
1163+
pid: int
1164+
1165+
Check if a process is a Python process.
1166+
[clinic start generated code]*/
1167+
1168+
static PyObject *
1169+
_remote_debugging_is_python_process_impl(PyObject *module, int pid)
1170+
/*[clinic end generated code: output=22947dc8afcac362 input=13488e28c7295d84]*/
1171+
{
1172+
proc_handle_t handle;
1173+
1174+
if (_Py_RemoteDebug_InitProcHandle(&handle, pid) < 0) {
1175+
PyErr_Clear();
1176+
Py_RETURN_FALSE;
1177+
}
1178+
1179+
uintptr_t runtime_start_address = _Py_RemoteDebug_GetPyRuntimeAddress(&handle);
1180+
_Py_RemoteDebug_CleanupProcHandle(&handle);
1181+
1182+
if (runtime_start_address == 0) {
1183+
PyErr_Clear();
1184+
Py_RETURN_FALSE;
1185+
}
1186+
1187+
Py_RETURN_TRUE;
1188+
}
1189+
11601190
static PyMethodDef remote_debugging_methods[] = {
11611191
_REMOTE_DEBUGGING_GET_CHILD_PIDS_METHODDEF
1192+
_REMOTE_DEBUGGING_IS_PYTHON_PROCESS_METHODDEF
11621193
{NULL, NULL, 0, NULL},
11631194
};
11641195

0 commit comments

Comments
 (0)