Search code examples
swiftbashexc-bad-accessnstaskbus-error

-[NSConcreteTask waitUntilExit] results in KERN_PROTECTION_FAILURE


I'm getting an error like the following (censored and trimmed to protect proprietary info):

Process:               MyExecutable [7150]
Path:                  /Applications/Company Name/Parent App.app/Contents/PlugIns/MyExecutable
Identifier:            MyExecutable
Version:               0
Code Type:             X86-64 (Native)
Parent Process:        ??? [1]
Responsible:           MyExecutable [7150]
User ID:               1129915948
Date/Time:             2018-03-27 11:42:12.401 -0400
OS Version:            Mac OS X 10.12.6 (16G1212)
Report Version:        12
Anonymous UUID:        013E2942-CED9-22FA-438A-E3D0BA89EB5C
Time Awake Since Boot: 6200 seconds
System Integrity Protection: enabled
Crashed Thread:        0  Dispatch queue: com.apple.main-thread
Exception Type:        EXC_BAD_ACCESS (SIGBUS)
Exception Codes:       KERN_PROTECTION_FAILURE at 0x00007fa9bc632b10
Exception Note:        EXC_CORPSE_NOTIFY
Termination Signal:    Bus error: 10
Termination Reason:    Namespace SIGNAL, Code 0xa
Terminating Process:   exc handler [0]
VM Regions Near 0x7fa9bc632b10:
    MALLOC_TINY            00007fa9bc400000-00007fa9bc600000 [ 2048K] rw-/rwx SM=PRV  
--> MALLOC_TINY            00007fa9bc600000-00007fa9bc700000 [ 1024K] rw-/rwx SM=COW  
    MALLOC_TINY            00007fa9bc700000-00007fa9bc800000 [ 1024K] rw-/rwx SM=PRV  
Thread 0 Crashed:: Dispatch queue: com.apple.main-thread
0   ???                             0x00007fa9bc632b10 0 + 140366986816272
1   com.apple.Foundation            0x00007fffa3225051 -[NSConcreteTask waitUntilExit] + 213
2   MyExecutable                    0x000000010dcfeb86 _TTSf4gs_gs_g___TF8MyExecutableP33_446BC8CF8CAA764CF050D96869CE3A5A18mySwiftFunctionFTSS7purposeSS18callbackFCSo7ProcessT__T_ + 1254
3   MyExecutable                    0x000000010dcfdbb7 _TZFC8MyExecutable12MyClassP33_446BC8CF8CAA764CF050D96869CE3A5A31someSwiftFunctionfT_T_ + 215
4   MyExecutable                    0x000000010dcfd985 _TZFC8MyExecutable12MyClass40someSwiftFunctionfT_T_ + 21
5   MyExecutable                    0x000000010dcd8e0f main + 241
6   libdyld.dylib                   0x00007fffb6ef3235 start + 1

The code in question attempts to run a terminal command by using Process (AKA NSTask in Objective-C; implemented by NSConcreteTask). It's prepared like this:

process.launchPath = "/bin/bash"
process.arguments = ["-c", bashCommand]
process.terminationHandler = callback
process.launch()
process.waitUntilExit()

It runs fine until the call to waitUntilExit. When that runs, the app crashes immediately with an output similar to that above.

For various reasons, I can't run a debugger to test this, but I can print log lines to the console. Why would it crash this hard? I even see a bus error in there for crying out loud...


Solution

  • Synchronous waitUntilExit() and asynchronous completion handler don't work very well together.

    If you use the asynchronous completion handler waiting for exit is actually pointless.

    Just delete the line

    process.waitUntilExit()