Re: NSTask is intermittently failing to return results.
Re: NSTask is intermittently failing to return results.
- Subject: Re: NSTask is intermittently failing to return results.
- From: Jason Harris <email@hidden>
- Date: Fri, 18 Mar 2011 23:03:25 +0100
On Mar 18, 2011, at 10:36 PM, email@hidden wrote:
>
> On 18 Mar 2011, at 21:04, Jason Harris wrote:
>>
>>
>> Hi Jonathan,
>>
>> Thanks for looking at this! It's good to know I am not going crazy and there is some subtle thing I am doing wrong here...
>>
>> Just to clarify you mean this example here:
>>
>> http://developer.apple.com/library/mac/#samplecode/CommandLineTool/Listings/CommandLineToolPlugIn_m.html
>>
>> for the CommandLineToolPlugin.m?
> Yep.
>
>>
>> I hadn't seen this approach at the end of using [task waitUntilExit] after you have set up the observers and notifications. I had seen in eg OpenFileKiller: http://cocoawithlove.com/2009/05/invoking-other-processes-in-cocoa.html and in https://bitbucket.org/snej/myutilities/src/tip/MYTask.m where a run loop is used. Ie something like:
>>
>> - (void)launchTaskAndRunSynchronous
>> {
>> [task launch];
>>
>> BOOL isRunning = YES;
>> while (isRunning && (!taskComplete || !outputClosed || !errorClosed))
>> {
>> isRunning =
>> [[NSRunLoop currentRunLoop]
>> runMode:NSDefaultRunLoopMode
>> beforeDate:[NSDate distantFuture]];
>> }
>> }
>>
> I don't see any reason not to use the NSTask implementation unless you encounter any particular issues.
>
>> (Also just glancing at the code, I noticed a couple of really small things... the date at the top of CommandLineToolPlugin.m lists 2009 yet its using old style NS_DURING, NS_HANDLER, NS_ENDHANDLER which seems a little strange... but its not wrong or anything, just a little well strange.
> Yes, a bit curious. Maybe its a rehash of something more archaic though the revision notes don't specify this.
Well just to give some quick feedback... Surprisingly to me your suggested approach here seems to be working straight off the bat!!
Ie if in my code I just replace
- (BOOL) waitTillFinished
{
// wait for task to exit:
while (![self shouldFinishUp])
{
// If the task is terminated we should set up a pending termination which will terminate in a bit. This catches some
// zombie NSTasks where either the outputData or errorData of 0 were never posted..
BOOL terminated = ![task_ isRunning];
if (terminated && !pendingTermination_)
{
DebugLog(@"...Found terminated for %@ ...", [self commandLineString]);
[self setPendingTermination];
}
BOOL runLoopRan = [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];
if (!runLoopRan)
break;
if (!isFinished_)
DebugLog(@"...waitTillFinished still waiting for %@ ...", [self commandLineString]);
}
[self finishUp];
DebugLog(@"...Exiting waitTillFinished for %@ ...", [self commandLineString]);
return (result_ == 0);
}
with
- (BOOL) waitTillFinished
{
// wait for task to exit:
[task_ waitUntilExit];
DebugLog(@"...Exiting waitTillFinished for %@ ...", [self commandLineString]);
return (result_ == 0);
}
Along with some other tweaks (No longer call [self finishUp] in any form) then it works.
Thank you! Thank you! At least I have something to explore now. I don't know *why* this works since it seems to use a different approach to that of eg OpenFileKiller and MyTask but:
(i) The exceptions seem to be gone
(ii) There seems to be no dropped results.
(iii) The tasks seem to be exiting (ie no stuck processes...) (A stuck process is a sort of zombie process which just doesn't finish (but it turns out a zombie process has a defined meaning in unix and my stuck processes where not zombies...))
I'll investigate a bit more and try and boil down what was going on and post my findings back. If anyone else can shed some light on the reasons why this is happening I would be most grateful.
But in any case *Thank You Jonathan*!!!
Cheers,
Jas_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden