Skip to content

Commit d5476b9

Browse files
committed
Fix a few data races when (de)instrumenting opcodes
- Fix a few places where we were not using atomics to (de)instrument opcodes. - Fix a few places where we weren't using atomics to reset adaptive counters. - Remove some redundant non-atomic resets of adaptive counters that presumably snuck as merge artifacts of python#118064 and python#117144 landing close together.
1 parent 7d2eb27 commit d5476b9

File tree

1 file changed

+8
-7
lines changed

1 file changed

+8
-7
lines changed

Python/instrumentation.c

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -681,9 +681,10 @@ de_instrument_line(_Py_CODEUNIT *bytecode, _PyCoMonitoringData *monitoring, int
681681
}
682682
CHECK(original_opcode != 0);
683683
CHECK(original_opcode == _PyOpcode_Deopt[original_opcode]);
684-
instr->op.code = original_opcode;
684+
FT_ATOMIC_STORE_UINT8(instr->op.code, original_opcode);
685685
if (_PyOpcode_Caches[original_opcode]) {
686-
instr[1].counter = adaptive_counter_warmup();
686+
FT_ATOMIC_STORE_UINT16_RELAXED(instr[1].counter.as_counter,
687+
adaptive_counter_warmup().as_counter);
687688
}
688689
assert(instr->op.code != INSTRUMENTED_LINE);
689690
}
@@ -705,9 +706,10 @@ de_instrument_per_instruction(_Py_CODEUNIT *bytecode,
705706
int original_opcode = monitoring->per_instruction_opcodes[i];
706707
CHECK(original_opcode != 0);
707708
CHECK(original_opcode == _PyOpcode_Deopt[original_opcode]);
708-
*opcode_ptr = original_opcode;
709+
FT_ATOMIC_STORE_UINT8_RELAXED(*opcode_ptr, original_opcode);
709710
if (_PyOpcode_Caches[original_opcode]) {
710-
instr[1].counter = adaptive_counter_warmup();
711+
FT_ATOMIC_STORE_UINT16_RELAXED(instr[1].counter.as_counter,
712+
adaptive_counter_warmup().as_counter);
711713
}
712714
assert(*opcode_ptr != INSTRUMENTED_INSTRUCTION);
713715
assert(instr->op.code != INSTRUMENTED_INSTRUCTION);
@@ -740,7 +742,6 @@ instrument(_Py_CODEUNIT *bytecode, _PyCoMonitoringData *monitoring, int i)
740742
if (_PyOpcode_Caches[deopt]) {
741743
FT_ATOMIC_STORE_UINT16_RELAXED(instr[1].counter.as_counter,
742744
adaptive_counter_warmup().as_counter);
743-
instr[1].counter = adaptive_counter_warmup();
744745
}
745746
}
746747
}
@@ -756,7 +757,7 @@ instrument_line(_Py_CODEUNIT *bytecode, _PyCoMonitoringData *monitoring, int i)
756757
_PyCoLineInstrumentationData *lines = &monitoring->lines[i];
757758
lines->original_opcode = _PyOpcode_Deopt[opcode];
758759
CHECK(lines->original_opcode > 0);
759-
*opcode_ptr = INSTRUMENTED_LINE;
760+
FT_ATOMIC_STORE_UINT8_RELAXED(*opcode_ptr, INSTRUMENTED_LINE);
760761
}
761762

762763
static void
@@ -786,7 +787,7 @@ instrument_per_instruction(_Py_CODEUNIT *bytecode,
786787
monitoring->per_instruction_opcodes[i] = _PyOpcode_Deopt[opcode];
787788
}
788789
assert(monitoring->per_instruction_opcodes[i] > 0);
789-
*opcode_ptr = INSTRUMENTED_INSTRUCTION;
790+
FT_ATOMIC_STORE_UINT8_RELAXED(*opcode_ptr, INSTRUMENTED_INSTRUCTION);
790791
}
791792

792793
static void

0 commit comments

Comments
 (0)