Skip to content

Commit a47e63b

Browse files
oskar-skognico
authored andcommitted
Ports/python3: Backport gh-126688: Reinit import lock after fork
python/cpython#126765 Closes #25263 Make sure to revert this commit before updating Python.
1 parent 3e29a19 commit a47e63b

File tree

2 files changed

+70
-0
lines changed

2 files changed

+70
-0
lines changed
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
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 */

Ports/python3/patches/ReadMe.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ As usual, make the `configure` script recognize Serenity. Also set
1515
`MACHDEP` (which is used for `sys.platform`) to a version-less
1616
`serenityos`, even when not cross-compiling.
1717

18+
1819
## `0003-Include-sys-uio.h-in-socketmodule.c.patch`
1920

2021
Include `sys/uio.h` in `socketmodule.c`
@@ -34,6 +35,7 @@ unknown.
3435

3536
Don't include `sys/syscall.h` in mimalloc
3637

38+
3739
## `0006-Force-disable-pyrepl.patch`
3840

3941
Force-disable pyrepl
@@ -42,3 +44,13 @@ We are lacking termios support for this leading to a non-functional
4244
modern REPL. Force-disable it in the source instead of requiring users
4345
to set PYTHON_BASIC_REPL=1 to work around the issue.
4446

47+
## `0007-Backport-gh-126688-Reinit-import-lock-after-fork.patch`
48+
49+
Backport gh-126688: Reinit import lock after fork
50+
51+
https://github.com/python/cpython/pull/126765
52+
This PR will be included in some future version of Python,
53+
it is needed for `os.fork` to work on SerenityOS.
54+
55+
Authored-By: Sam Gross <[email protected]>
56+

0 commit comments

Comments
 (0)