[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
NSTask not thread safe
From: |
Wim Oudshoorn |
Subject: |
NSTask not thread safe |
Date: |
Fri, 27 Oct 2006 16:15:49 +0200 |
User-agent: |
Gnus/5.1002 (Gnus v5.10.2) Emacs/22.0.50 (darwin) |
It seems that the NSTask code to monitor running tasks
is not thread safe.
If we have multiple threads running and lets say that
thread A runs the following code:
thread-A code:
{
arp = CreateAutoreleasePool ();
task = [[NSTask alloc] init];
[task setLaunchPath: @"A program"];
[task launch];
[task waitUntilExit];
[task release];
[arp release];
}
and thread-B is running code with its own runloop.
Now the following sequence of events can happen:
1 - B - thread B executes in its runloop code GSCheckTasks.
2 - B - GSCheckTasks discovers that the task started in thread A is exited.
B - it will call -[NSTask _terminatedChild]
3 - B - This method will flag the task as terminated.
4 - A - the [task waitUntilExit] discovers the task is exited
5 - A - the line [task release] will destroy the task
6 - B - the [NSTask _terminatedChild] will continue and
will send the task is terminated notification
but with the ALREADY DEALLOCATED task.
Remarks
-------
It is not immediately clear to me how to solve this.
But before I spent to much time thinking about that, I want an opinion
about the following:
The current code can (and sometimes will) throw a NSTaskDidTerminate
notification
in a different thread than the thread that started the task. Personally I
prefer if the thread starting the task is the same thread as the thread
sending the notification. But maybe there are good reason for this.
(One reason of course being that the thread that started a task
can already have exited. But those tasks can be moved in a general
pool.)
Wim Oudshoorn.
- NSTask not thread safe,
Wim Oudshoorn <=