Skip to content

Commit 27d5db7

Browse files
committed
Only re-initialize thread id
1 parent 4bf74b8 commit 27d5db7

File tree

4 files changed

+10
-9
lines changed

4 files changed

+10
-9
lines changed

Include/internal/pycore_import.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ extern int _PyImport_SetModuleString(const char *name, PyObject* module);
2121

2222
extern void _PyImport_AcquireLock(PyInterpreterState *interp);
2323
extern void _PyImport_ReleaseLock(PyInterpreterState *interp);
24+
extern void _PyImport_ReInitLock(PyInterpreterState *interp);
2425

2526
// This is used exclusively for the sys and builtins modules:
2627
extern int _PyImport_FixupBuiltin(

Include/internal/pycore_lock.h

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -164,13 +164,6 @@ extern PyLockStatus _PyRecursiveMutex_LockTimed(_PyRecursiveMutex *m, PyTime_t t
164164
PyAPI_FUNC(void) _PyRecursiveMutex_Unlock(_PyRecursiveMutex *m);
165165
extern int _PyRecursiveMutex_TryUnlock(_PyRecursiveMutex *m);
166166

167-
static inline void
168-
_PyRecursiveMutex_at_fork_reinit(_PyRecursiveMutex *m)
169-
{
170-
memset(m, 0, sizeof(*m));
171-
}
172-
173-
174167
// A readers-writer (RW) lock. The lock supports multiple concurrent readers or
175168
// a single writer. The lock is write-preferring: if a writer is waiting while
176169
// the lock is read-locked then, new readers will be blocked. This avoids

Modules/posixmodule.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -678,8 +678,8 @@ PyOS_AfterFork_Child(void)
678678
_PyEval_StartTheWorldAll(&_PyRuntime);
679679
_PyThreadState_DeleteList(list);
680680

681-
// gh-126688: Reinit lock because thread id may differ in child process.
682-
_PyRecursiveMutex_at_fork_reinit(&tstate->interp->imports.lock);
681+
_PyImport_ReInitLock(tstate->interp);
682+
_PyImport_ReleaseLock(tstate->interp);
683683

684684
_PySignal_AfterFork();
685685

Python/import.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,13 @@ _PyImport_ReleaseLock(PyInterpreterState *interp)
122122
_PyRecursiveMutex_Unlock(&IMPORT_LOCK(interp));
123123
}
124124

125+
void
126+
_PyImport_ReInitLock(PyInterpreterState *interp)
127+
{
128+
// gh-126688: Thread id may change after fork() on some operating systems.
129+
IMPORT_LOCK(interp).thread = PyThread_get_thread_ident_ex();
130+
}
131+
125132

126133
/***************/
127134
/* sys.modules */

0 commit comments

Comments
 (0)