Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 359bcaa

Browse files
committed
This fix (across 4 files in 3 directories) solves a subtle problem with
signal handlers in a fork()ed child process when Python is compiled with thread support. The bug was reported by Scott <[email protected]>. What happens is that after a fork(), the variables used by the signal module to determine whether this is the main thread or not are bogus, and it decides that no thread is the main thread, so no signals will be delivered. The solution is the addition of PyOS_AfterFork(), which fixes the signal module's variables. A dummy version of the function is present in the intrcheck.c source file which is linked when the signal module is not used.
1 parent c9fd600 commit 359bcaa

4 files changed

Lines changed: 16 additions & 0 deletions

File tree

Include/intrcheck.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ PERFORMANCE OF THIS SOFTWARE.
3737

3838
extern int PyOS_InterruptOccurred Py_PROTO((void));
3939
extern void PyOS_InitInterrupts Py_PROTO((void));
40+
void PyOS_AfterFork Py_PROTO((void));
4041

4142
#ifdef __cplusplus
4243
}

Modules/posixmodule.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1082,6 +1082,7 @@ posix_fork(self, args)
10821082
pid = fork();
10831083
if (pid == -1)
10841084
return posix_error();
1085+
PyOS_AfterFork();
10851086
return PyInt_FromLong((long)pid);
10861087
}
10871088
#endif

Modules/signalmodule.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -613,3 +613,12 @@ PyOS_InterruptOccurred()
613613
}
614614
return 0;
615615
}
616+
617+
void
618+
PyOS_AfterFork()
619+
{
620+
#ifdef WITH_THREAD
621+
main_thread = get_thread_ident();
622+
main_pid = getpid();
623+
#endif
624+
}

Parser/intrcheck.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,3 +219,8 @@ PyOS_InterruptOccurred()
219219
}
220220

221221
#endif /* !OK */
222+
223+
void
224+
PyOS_AfterFork()
225+
{
226+
}

0 commit comments

Comments
 (0)