|
| 1 | +From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 |
| 2 | +From: Oskar Skog < [email protected]> |
| 3 | +Date: Wed, 13 Nov 2024 01:07:10 +0200 |
| 4 | +Subject: [PATCH] Backport gh-126688: Reinit import lock after fork |
| 5 | + |
| 6 | +https://github.com/python/cpython/pull/126765 |
| 7 | +This PR will be included in some future version of Python, |
| 8 | +it is needed for `os.fork` to work on SerenityOS. |
| 9 | + |
| 10 | +Authored-By: Sam Gross < [email protected]> |
| 11 | +--- |
| 12 | + Include/internal/pycore_import.h | 1 + |
| 13 | + Modules/posixmodule.c | 1 + |
| 14 | + Python/import.c | 7 +++++++ |
| 15 | + 3 files changed, 9 insertions(+) |
| 16 | + |
| 17 | +diff --git a/Include/internal/pycore_import.h b/Include/internal/pycore_import.h |
| 18 | +index 3806e0d3cd0a95bd9ddc6557212ded290772cc32..55029abdd31b5a479f16a048d402c44c2d7affca 100644 |
| 19 | +--- a/Include/internal/pycore_import.h |
| 20 | ++++ b/Include/internal/pycore_import.h |
| 21 | +@@ -21,6 +21,7 @@ extern int _PyImport_SetModuleString(const char *name, PyObject* module); |
| 22 | + |
| 23 | + extern void _PyImport_AcquireLock(PyInterpreterState *interp); |
| 24 | + extern void _PyImport_ReleaseLock(PyInterpreterState *interp); |
| 25 | ++extern void _PyImport_ReInitLock(PyInterpreterState *interp); |
| 26 | + |
| 27 | + // This is used exclusively for the sys and builtins modules: |
| 28 | + extern int _PyImport_FixupBuiltin( |
| 29 | +diff --git a/Modules/posixmodule.c b/Modules/posixmodule.c |
| 30 | +index 51e34b5f4b74fc1d7a7ea405fb797d0dd390e4f4..15dca2d4fa04ce88a87d361c12d33ba01e3e6e7d 100644 |
| 31 | +--- a/Modules/posixmodule.c |
| 32 | ++++ b/Modules/posixmodule.c |
| 33 | +@@ -677,6 +677,7 @@ PyOS_AfterFork_Child(void) |
| 34 | + _PyEval_StartTheWorldAll(&_PyRuntime); |
| 35 | + _PyThreadState_DeleteList(list); |
| 36 | + |
| 37 | ++ _PyImport_ReInitLock(tstate->interp); |
| 38 | + _PyImport_ReleaseLock(tstate->interp); |
| 39 | + |
| 40 | + _PySignal_AfterFork(); |
| 41 | +diff --git a/Python/import.c b/Python/import.c |
| 42 | +index 2ec596828e3e6fab5253f58ee14081907c49eff6..125ee439a20a20d8918778500c5c7e6ce554368a 100644 |
| 43 | +--- a/Python/import.c |
| 44 | ++++ b/Python/import.c |
| 45 | +@@ -120,6 +120,13 @@ _PyImport_ReleaseLock(PyInterpreterState *interp) |
| 46 | + _PyRecursiveMutex_Unlock(&IMPORT_LOCK(interp)); |
| 47 | + } |
| 48 | + |
| 49 | ++void |
| 50 | ++_PyImport_ReInitLock(PyInterpreterState *interp) |
| 51 | ++{ |
| 52 | ++ // gh-126688: Thread id may change after fork() on some operating systems. |
| 53 | ++ IMPORT_LOCK(interp).thread = PyThread_get_thread_ident_ex(); |
| 54 | ++} |
| 55 | ++ |
| 56 | + |
| 57 | + /***************/ |
| 58 | + /* sys.modules */ |
0 commit comments