Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 25a889a

Browse files
committed
Handle race when clearing side exits of side exits
1 parent 04feb78 commit 25a889a

File tree

1 file changed

+12
-8
lines changed

1 file changed

+12
-8
lines changed

Python/optimizer.c

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -82,10 +82,12 @@ get_index_for_executor(PyCodeObject *code, _Py_CODEUNIT *instr)
8282
static void
8383
insert_executor(PyCodeObject *code, _Py_CODEUNIT *instr, int index, _PyExecutorObject *executor)
8484
{
85+
_PyExecutorObject *old_executor = NULL;
8586
Py_INCREF(executor);
8687
if (instr->op.code == ENTER_EXECUTOR) {
8788
assert(index == instr->op.arg);
88-
_Py_ExecutorClear(code->co_executors->executors[index]);
89+
old_executor = code->co_executors->executors[index];
90+
code->co_executors->executors[index] = NULL;
8991
}
9092
else {
9193
assert(code->co_executors->size == index);
@@ -100,6 +102,7 @@ insert_executor(PyCodeObject *code, _Py_CODEUNIT *instr, int index, _PyExecutorO
100102
assert(index < MAX_EXECUTORS_SIZE);
101103
instr->op.code = ENTER_EXECUTOR;
102104
instr->op.arg = index;
105+
Py_XDECREF(old_executor);
103106
}
104107

105108
int
@@ -1591,13 +1594,14 @@ _Py_ExecutorClear(_PyExecutorObject *executor)
15911594
executor->exits[i].temperature = initial_unreachable_backoff_counter();
15921595
}
15931596
_Py_CODEUNIT *instruction = &_PyCode_CODE(code)[executor->vm_data.index];
1594-
assert(instruction->op.code == ENTER_EXECUTOR);
1595-
int index = instruction->op.arg;
1596-
assert(code->co_executors->executors[index] == executor);
1597-
instruction->op.code = executor->vm_data.opcode;
1598-
instruction->op.arg = executor->vm_data.oparg;
1599-
executor->vm_data.code = NULL;
1600-
Py_CLEAR(code->co_executors->executors[index]);
1597+
if (instruction->op.code == ENTER_EXECUTOR) {
1598+
int index = instruction->op.arg;
1599+
assert(code->co_executors->executors[index] == executor);
1600+
instruction->op.code = executor->vm_data.opcode;
1601+
instruction->op.arg = executor->vm_data.oparg;
1602+
executor->vm_data.code = NULL;
1603+
Py_CLEAR(code->co_executors->executors[index]);
1604+
}
16011605
}
16021606

16031607
void

0 commit comments

Comments
 (0)