NSTask not working in Xcode 9.4

Hi,

So this morning I’ve updated to Xcode 9.4 and did the command line tools update as well.
Since then it looks like i can’t execute an NSTask anymore.
I used the method from Shane’s book. But now it gives an posix spawn error with a thread 1: EXC_SOFTWARE (code=131072, subcode=0x0)

Here is what I do:


    on doShellScriptInBackground:theCommand taskID:myTaskID callback:selectorName
        log "DoShellScript | > doShellScriptInBackground: " & theCommand & " taskID: " & myTaskID & " callback: " & selectorName

        -- create pipe for standard output, and get reading file handle from it
        set outPipe to current application's NSPipe's pipe()
        set outFileHandle to outPipe's fileHandleForReading()
        -- create pipe for error output
        set errPipe to current application's NSPipe's pipe()
        set errFileHandle to errPipe's fileHandleForReading()
        -- make task and launch it
        set theTask to current application's NSTask's alloc()'s init()
        tell theTask
            its setLaunchPath:"/bin/sh"
            -- the -c argument let's put everything in the command list.
            its setArguments:{"-c", "PATH=\"/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:\"; export PATH; " & theCommand}
            its setStandardOutput:outPipe
            its setStandardError:errPipe
            -- the following line is needed or logging ceases at this point
            its setStandardInput:(current application's NSPipe's pipe())
        end tell
        
        -- Register a notifaction when the task is done.
        set nc to current application's NSNotificationCenter's defaultCenter()
        tell nc to addObserver:me selector:selectorName |name|:(current application's NSTaskDidTerminateNotification) object:theTask
        
        -- execute the task
        theTask's |launch|()

        
        -- Add it to our own taskArray list. This gets used in the callback function.
        set end of tasksArray to {taskObject:theTask, taskID:myTaskID}
        
    end doShellScriptInBackground:taskID:callback:




As soon as it hits theTask’s |launch|() it will crash.

I haven’t got a clue anymore! Please help!
I’m running 10.13.4 so perhaps it’s a kernel extension block or something?

Even a simpel NSTask command like this will crash:


set theTask to current application's NSTask's launchedTaskWithLaunchPath:"/bin/sleep" arguments:{"0.5"}

Is this a bug or can I fix it?

You could try replying to the message I sent you…

Thanks Shane!
Sorry didn’t spot your message. Sent you a sample project just now.

In the meantime I found this on the documentation. https://developer.apple.com/documentation/foundation/nstask

Apparently launch is now deprecated along with + launchedTaskWithLaunchPath:arguments:
and many other that I was using.

Now you’ll have to use launchedTaskWithExecutableURL:arguments:error:terminationHandler:https://developer.apple.com/documentation/foundation/nstask/2890108-launchedtaskwithexecutableurl?language=objc

I’m still trying to figure that one out. I find the documentation very poor on that one.

Also when I adjust the code for this will the application still work on 10.11 or 10.12?

First, I just downloaded Xcode 9.4 and ran your sample, and it worked fine. I changed a hander like so:

    on simpleNSTask_(sender)
    set theTask to current application's NSTask's launchedTaskWithLaunchPath:"/bin/mkdir" arguments:{"-p", "/Users/shane/Desktop/Test_folder"}
        log theTask
    end simpleNSTask_

and the folder was created.

What version of the OS are you running? I’m running 10.13.4.

Also check that Xcode Preferences → Locations → Command Line Tools is set to Xcode 9.4.

Apparently launch is now deprecated along with + launchedTaskWithLaunchPath:arguments:
and many other that I was using.

That doesn’t mean you can’t still use them. In this case, it simply means that if you’re developing for 10.13 and later, you should generally prefer the newer methods. But the old ones should keep working for quite a while yet – these transitions are generally very slow.

Hi Shane,

Thanks for the reply. So far I have figured out indeed that it has to do with my setup.
Still can’t figure out what happened and why it’s broken now.

As soon as I ran an NSTask I get this error.


libsystem_kernel.dylib`__posix_spawn:
    0x7fff7b76093c <+0>:  movl   $0x20000f4, %eax          ; imm = 0x20000F4 
    0x7fff7b760941 <+5>:  movq   %rcx, %r10
    0x7fff7b760944 <+8>:  syscall 
->  0x7fff7b760946 <+10>: jae    0x7fff7b760950            ; <+20>
    0x7fff7b760948 <+12>: movq   %rax, %rdi
    0x7fff7b76094b <+15>: jmp    0x7fff7b757b25            ; cerror
    0x7fff7b760950 <+20>: retq   
    0x7fff7b760951 <+21>: nop    
    0x7fff7b760952 <+22>: nop    
    0x7fff7b760953 <+23>: nop    

Perhaps they only thing left is reinstall my mac :frowning:

Alas, that doesn’t mean anything to me.

I managed to fixed it!

Apparently our End Point Security software (Cylance) flagged the posix_spawn executable after the command line tools update. So I wasn’t allowed to use it anymore. Removed the software and everything works again.

That were a lot of hours trying to fix this…
Glad no having to reinstall my entire system though.