Fix fork locking on win32 (#1141)

* Fix fork locking on win32

- __enable_threads / set __threaded in __proc_setup as threads are required for
  win32 subprocess management
- move mmi/fds locking out of pthread_atfork.c into fork.c so it's done anytime
  __threaded is set instead of being dependent of pthreads
- explicitly yoink _pthread_onfork_prepare, _pthread_onfork_parent, and
  _pthread_onfork_child in pthread_create.c so they are linked in in-case they
  are separated from _pthread_atfork

Big Thanks to @dfyz for help with locating the issue, testing, and devising a fix!

* fix child processes not being able to open files, initialize all necessary locks on fork
This commit is contained in:
Gavin Hayes 2024-04-25 23:01:27 -04:00 committed by GitHub
parent 6e6fc38935
commit 69db501c68
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 49 additions and 23 deletions

View file

@ -18,6 +18,7 @@
*/
#include "libc/assert.h"
#include "libc/calls/calls.h"
#include "libc/calls/state.internal.h"
#include "libc/calls/struct/sigset.h"
#include "libc/calls/struct/sigset.internal.h"
#include "libc/calls/syscall-nt.internal.h"
@ -34,12 +35,45 @@
#include "libc/nt/thread.h"
#include "libc/proc/proc.internal.h"
#include "libc/runtime/internal.h"
#include "libc/runtime/memtrack.internal.h"
#include "libc/runtime/runtime.h"
#include "libc/runtime/syslib.internal.h"
#include "libc/sysv/consts/sig.h"
#include "libc/thread/posixthread.internal.h"
#include "libc/thread/tls.h"
static void _onfork_prepare(void) {
if (_weaken(_pthread_onfork_prepare)) {
_weaken(_pthread_onfork_prepare)();
}
_pthread_lock();
__fds_lock();
__mmi_lock();
}
static void _onfork_parent(void) {
__mmi_unlock();
__fds_unlock();
_pthread_unlock();
if (_weaken(_pthread_onfork_parent)) {
_weaken(_pthread_onfork_parent)();
}
}
static void _onfork_child(void) {
pthread_mutexattr_t attr;
pthread_mutexattr_init(&attr);
pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
extern pthread_mutex_t __mmi_lock_obj;
pthread_mutex_init(&__mmi_lock_obj, &attr);
pthread_mutex_init(&__fds_lock_obj, &attr);
pthread_mutexattr_destroy(&attr);
_pthread_init();
if (_weaken(_pthread_onfork_child)) {
_weaken(_pthread_onfork_child)();
}
}
int _fork(uint32_t dwCreationFlags) {
struct Dll *e;
int ax, dx, tid, parent;
@ -47,8 +81,8 @@ int _fork(uint32_t dwCreationFlags) {
BLOCK_SIGNALS;
if (IsWindows())
__proc_lock();
if (__threaded && _weaken(_pthread_onfork_prepare)) {
_weaken(_pthread_onfork_prepare)();
if (__threaded) {
_onfork_prepare();
}
if (!IsWindows()) {
ax = sys_fork();
@ -99,14 +133,14 @@ int _fork(uint32_t dwCreationFlags) {
atomic_store_explicit(&pt->pt_canceled, false, memory_order_relaxed);
// run user fork callbacks
if (__threaded && _weaken(_pthread_onfork_child)) {
_weaken(_pthread_onfork_child)();
if (__threaded) {
_onfork_child();
}
STRACE("fork() → 0 (child of %d)", parent);
} else {
// this is the parent process
if (__threaded && _weaken(_pthread_onfork_parent)) {
_weaken(_pthread_onfork_parent)();
if (__threaded) {
_onfork_parent();
}
if (IsWindows())
__proc_unlock();