Skip to content

Commit 8432e59

Browse files
authored
Merge pull request #500 from pythonspeed/485-thread-panicked
Attempt to fix reentrancy bugs
2 parents 8ff3749 + 2ee90a0 commit 8432e59

File tree

2 files changed

+7
-0
lines changed

2 files changed

+7
-0
lines changed

.changelog/485.bugfix

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fixed a reentrancy bug that could cause profiling to fail with a BorrowMutError.

filpreload/src/_filpreload.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,9 @@ static void __attribute__((constructor)) constructor() {
221221
#endif
222222
// Initialize Rust static state before we start doing any calls via malloc(),
223223
// to ensure we don't get unpleasant reentrancy issues.
224+
increment_reentrancy();
224225
pymemprofile_reset("/tmp");
226+
decrement_reentrancy();
225227

226228
// Drop LD_PRELOAD so that Linux subprocesses don't have this preloaded.
227229
unsetenv("LD_PRELOAD");
@@ -350,7 +352,9 @@ __attribute__((visibility("default"))) void register_fil_tracer() {
350352
// C threads inherit their callstack from the creating Python thread. That's
351353
// fine. However, if a tracer is being registered, that means this is not a
352354
// pure C thread, it's a new Python thread with its own callstack.
355+
increment_reentrancy();
353356
pymemprofile_clear_current_callstack();
357+
decrement_reentrancy();
354358
// We use 123 as a marker object for tests.
355359
PyEval_SetProfile(fil_tracer, PyLong_FromLong(123));
356360
}
@@ -590,7 +594,9 @@ SYMBOL_PREFIX(pthread_create)(pthread_t *thread, const pthread_attr_t *attr,
590594
}
591595
struct NewThreadArgs *wrapper_args =
592596
REAL_IMPL(malloc)(sizeof(struct NewThreadArgs));
597+
increment_reentrancy();
593598
wrapper_args->callstack = pymemprofile_get_current_callstack();
599+
decrement_reentrancy();
594600
wrapper_args->start_routine = start_routine;
595601
wrapper_args->arg = arg;
596602
int result = underlying_real_pthread_create(

0 commit comments

Comments
 (0)