Skip to content

Commit cdb5d46

Browse files
Stronger check for recursive traces
1 parent 9f5f8b5 commit cdb5d46

File tree

4 files changed

+10
-10
lines changed

4 files changed

+10
-10
lines changed

Include/internal/pycore_optimizer.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -354,7 +354,7 @@ _PyJit_TryInitializeTracing(PyThreadState *tstate, _PyInterpreterFrame *frame,
354354
_Py_CODEUNIT *close_loop_instr, int curr_stackdepth, int chain_depth, _PyExitData *exit,
355355
int oparg);
356356

357-
void _PyJit_FinalizeTracing(PyThreadState *tstate);
357+
void _PyJit_ResetTracing(PyThreadState *tstate);
358358

359359
void _PyJit_Tracer_InvalidateDependency(PyThreadState *old_tstate, void *obj);
360360

Python/ceval.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1486,7 +1486,7 @@ stop_tracing_and_jit(PyThreadState *tstate, _PyInterpreterFrame *frame)
14861486
exit->temperature = initial_temperature_backoff_counter();
14871487
}
14881488
}
1489-
_PyJit_FinalizeTracing(tstate);
1489+
_PyJit_ResetTracing(tstate);
14901490
return err;
14911491
}
14921492
#endif

Python/optimizer.c

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -138,12 +138,6 @@ _PyOptimizer_Optimize(
138138
// return immediately without optimization.
139139
return 0;
140140
}
141-
if (_tstate->jit_tracer_state.initial_state.func == NULL) {
142-
// gh-143123: It is possible for another function to finalize the current
143-
// tracer's state while tracing. This might happen in a
144-
// Python -> C -> Python call.
145-
return 0;
146-
}
147141
assert(!interp->compiling);
148142
assert(_tstate->jit_tracer_state.initial_state.stack_depth >= 0);
149143
#ifndef Py_GIL_DISABLED
@@ -1031,7 +1025,11 @@ _PyJit_TryInitializeTracing(
10311025
_PyThreadStateImpl *_tstate = (_PyThreadStateImpl *)tstate;
10321026
// A recursive trace.
10331027
// Don't trace into the inner call because it will stomp on the previous trace, causing endless retraces.
1034-
if (_tstate->jit_tracer_state.prev_state.code_curr_size > CODE_SIZE_EMPTY) {
1028+
if (_tstate->jit_tracer_state.prev_state.code_curr_size > CODE_SIZE_EMPTY ||
1029+
_tstate->jit_tracer_state.initial_state.func != NULL) {
1030+
// gh-143123: It is possible for another function to finalize the current
1031+
// tracer's state while tracing. This might happen in a
1032+
// Python -> C -> Python call.
10351033
return 0;
10361034
}
10371035
if (oparg > 0xFFFF) {
@@ -1095,7 +1093,7 @@ _PyJit_TryInitializeTracing(
10951093
}
10961094

10971095
Py_NO_INLINE void
1098-
_PyJit_FinalizeTracing(PyThreadState *tstate)
1096+
_PyJit_ResetTracing(PyThreadState *tstate)
10991097
{
11001098
_PyThreadStateImpl *_tstate = (_PyThreadStateImpl *)tstate;
11011099
Py_CLEAR(_tstate->jit_tracer_state.initial_state.code);

Python/pystate.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1526,6 +1526,7 @@ init_threadstate(_PyThreadStateImpl *_tstate,
15261526

15271527
#ifdef _Py_TIER2
15281528
_tstate->jit_tracer_state.code_buffer = NULL;
1529+
_PyJit_ResetTracing(tstate);
15291530
#endif
15301531
tstate->delete_later = NULL;
15311532

@@ -1846,6 +1847,7 @@ tstate_delete_common(PyThreadState *tstate, int release_gil)
18461847
_PyObject_VirtualFree(_tstate->jit_tracer_state.code_buffer, UOP_BUFFER_SIZE);
18471848
_tstate->jit_tracer_state.code_buffer = NULL;
18481849
}
1850+
_PyJit_ResetTracing(tstate);
18491851
#endif
18501852

18511853
HEAD_UNLOCK(runtime);

0 commit comments

Comments
 (0)