Repository navigation
Use plain fork() instead of __fork on macOS - #38
Merged
Merged
Conversation
__fork was used to skip pthread_atfork handlers, but nothing we link registers any, and libSystem's own handlers are what keep the child's libc state (malloc zone locks, mach ports) consistent. The child only makes plain syscalls, so plain fork() is sufficient and drops a private, weak-imported symbol.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
The macOS spawn path called the private
__fork(weak-imported, returning ENOSYS if absent) to skippthread_atforkhandlers. That isn't needed:pthread_atforkhandlers. The MoonBit runtime (libmoonbitrun.o) statically links mimalloc, but as a non-overriding build: it only servesmi_mallocfor MoonBit objects, does not replacemallocor register a malloc zone, and neither it nor the linked test binaries importpthread_atfork(checked withnm -uon moon 0.1.20261023). So the only handlers__forkskipped were libSystem's own, and those are what keep the child's libc state consistent (malloc zone locks taken around the fork, mach ports reset in the child).sigaction,setsid,ioctl(TIOCSCTTY),dup2,chdir,execve,nanosleep) and never allocates (neither throughmallocnor mimalloc), so plainfork()is sufficient.We still have to fork rather than
posix_spawnon macOS (XNU runs file actions beforePOSIX_SPAWN_SETSID, so the child can't acquire the pty as its controlling terminal); this only changes which fork is called. All unix platforms now share the samefork()path that Linux already used.Testing
moon testpassed 26/26 in 13 consecutive runs, no hangs.fork()).