Skip to content

Commit

Permalink
Don't grab stack trace of non-GIL threads when --gil is provided
Browse files Browse the repository at this point in the history
  • Loading branch information
d3dave authored and Jongy committed Mar 22, 2021
1 parent df7b5a6 commit 59ebcb9
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 7 deletions.
4 changes: 1 addition & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -265,9 +265,7 @@ fn record_samples(pid: remoteprocess::Pid, config: &Config) -> Result<(), Error>
continue;
}

if config.gil_only && !trace.owns_gil {
continue;
}
assert!(trace.owns_gil || !config.gil_only);

if config.include_thread_ids {
let threadid = trace.format_threadid();
Expand Down
14 changes: 10 additions & 4 deletions src/python_spy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -211,12 +211,16 @@ impl PythonSpy {
let mut traces = Vec::new();
let mut threads = interp.head();
while !threads.is_null() {
// Get the stack trace of the python thread
let thread = self.process.copy_pointer(threads).context("Failed to copy PyThreadState")?;
let mut trace = get_stack_trace(&thread, &self.process, self.config.dump_locals > 0)?;
let python_thread_id = thread.thread_id();
let owns_gil = python_thread_id == gil_thread_id;

if self.config.gil_only && !owns_gil {
threads = thread.next();
continue;
}

// Try getting the native thread id
let python_thread_id = thread.thread_id();
let mut os_thread_id = self._get_os_thread_id(python_thread_id, &interp)?;

// linux can see issues where pthread_ids get recycled for new OS threads,
Expand All @@ -230,9 +234,11 @@ impl PythonSpy {
}
}

// Get the stack trace of the python thread
let mut trace = get_stack_trace(&thread, &self.process, self.config.dump_locals > 0)?;
trace.os_thread_id = os_thread_id.map(|id| id as u64);
trace.thread_name = self._get_python_thread_name(python_thread_id);
trace.owns_gil = trace.thread_id == gil_thread_id;
trace.owns_gil = owns_gil;

// Figure out if the thread is sleeping from the OS if possible
trace.active = true;
Expand Down

0 comments on commit 59ebcb9

Please sign in to comment.