Skip to content

Commit 2dcbadc

Browse files
committed
frame: fix thread-safety issue when accessing f_lineno
It wasn't safe to access f_lineno from the thread that doesn't won the frame. This can happen, for example, when calling sys.current_frames() and accessing another thread's frames (such as for profiling purposes). See python#66
1 parent 4b35823 commit 2dcbadc

File tree

1 file changed

+5
-1
lines changed

1 file changed

+5
-1
lines changed

Objects/frameobject.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,11 @@ PyFrame_GetLineNumber(PyFrameObject *f)
4343
return f->f_lineno;
4444
}
4545
struct ThreadState *ts = f->f_ts;
46-
if (ts && ts->ts && !ts->ts->tracing) {
46+
PyThreadState *tstate = PyThreadState_GET();
47+
// We are supposed to update the current line number when we're not tracing,
48+
// but that's not safe to do from another thread. If we're not in the owning
49+
// thread, just use the potentially stale line number.
50+
if (ts && ts->ts == tstate && !tstate->tracing) {
4751
// update f->f_lasti
4852
vm_frame_at_offset(ts, f->f_offset);
4953
}

0 commit comments

Comments
 (0)