Thank you @eskimo
I was digging more and I found the source of problem and repro[1] - in the dynamic library we load some static intializer is creating a nw_path_monitor and once we fork the process it crashes in the atfork handler when Network frameworks tries to cleanup. I'll report a bug and see if someone tells me if it's operator error or just a bug :-)
I would still have a one more question for educational purposes: Are the the posix_spawn and NSTask doing something fundamentally different to just calling fork and exec*? I mean calling a mach APis or other dark magic?
[1] Simple main.m (used application template from Xcode)
// main.m
#import <Cocoa/Cocoa.h>
#import <Network/Network.h>
#import <dispatch/dispatch.h>
int main(int argc, const char * argv[]) {
@autoreleasepool {
nw_path_monitor_t mon = nw_path_monitor_create();
nw_path_monitor_set_update_handler(mon, ^(nw_path_t path) {
NSLog(@"monitor updated");
});
nw_path_monitor_start(mon);
}
pid_t pid = fork();
if (pid == -1) {
NSLog(@"Fork failed");
exit(1);
}
if (pid == 0) {
while (true) {
NSLog(@"Forked child here");
sleep(1);
}
return 0;
}
return NSApplicationMain(argc, argv);
}
Child crashes with:
Process: NWForkCrash [69516]
Path: /Users/USER/Library/Developer/Xcode/DerivedData/NWForkCrash-dnvdeuuhbnuhxublasbhfmmluzqb/Build/Products/Debug/NWForkCrash.app/Contents/MacOS/NWForkCrash
Identifier: com.****.NWForkCrash
Version: 1.0 (1)
Code Type: ARM-64 (Native)
Parent Process: NWForkCrash [69508]
Responsible: NWForkCrash [69508]
User ID: 501
Date/Time: 2023-09-15 16:52:56.2815 +0200
OS Version: macOS 13.5.2 (22G91)
Report Version: 12
Anonymous UUID: D6E5A34D-2127-16AF-16E7-BDA9139A6A82
Sleep/Wake UUID: DF75986B-D513-4000-993D-69A7AB7261A1
Time Awake Since Boot: 88000 seconds
Time Since Wake: 740 seconds
System Integrity Protection: enabled
Crashed Thread: 0 Dispatch queue: com.apple.main-thread
Exception Type: EXC_BREAKPOINT (SIGTRAP)
Exception Codes: 0x0000000000000001, 0x0000000194551238
Termination Reason: Namespace SIGNAL, Code 5 Trace/BPT trap: 5
Terminating Process: exc handler [69516]
Application Specific Information:
BUG IN CLIENT OF LIBPLATFORM: os_unfair_lock is corrupt
Abort Cause 258
crashed on child side of fork pre-exec
Thread 0 Crashed:: Dispatch queue: com.apple.main-thread
0 libsystem_platform.dylib 0x194551238 _os_unfair_lock_corruption_abort + 88
1 libsystem_platform.dylib 0x19454c788 _os_unfair_lock_lock_slow + 332
2 Network 0x19b1b4af0 nw_path_shared_necp_fd + 124
3 Network 0x19b1b4698 -[NWConcrete_nw_path_evaluator dealloc] + 72
4 Network 0x19af9d970 __nw_dictionary_dispose_block_invoke + 32
5 libxpc.dylib 0x194260210 _xpc_dictionary_apply_apply + 68
6 libxpc.dylib 0x19425c9a0 _xpc_dictionary_apply_node_f + 156
7 libxpc.dylib 0x1942600e8 xpc_dictionary_apply + 136
8 Network 0x19acd5210 -[OS_nw_dictionary dealloc] + 112
9 Network 0x19b1beb08 nw_path_release_globals + 120
10 Network 0x19b3d4fa0 nw_settings_child_has_forked() + 312
11 libsystem_pthread.dylib 0x10463f7c8 _pthread_atfork_child_handlers + 76
12 libsystem_c.dylib 0x1943d9944 fork + 112
13 NWForkCrash 0x1045db024 main + 96 (main.m:21)
14 dyld 0x1941c7f28 start + 2236