pokeb/asi-http-request — Issue #272: bad access after canceling synchronous ASIHTTPRequest
Author: dzianis-vasileuski
Created: 2011-10-13 16:15:08+00:00
State: open

cancel method uses background thread (returned by threadForRequest), but synchronous thread executed in other thread. When we cancel synchronous request we sometimes get a bad access crash.

NSZombie print: *** -[CFWriteStream hasSpaceAvailable]: message sent to deallocated instance 0x35ef5020

Other issue: Do we need @synchronized() in threadForRequest ? We can lunch request from multiple threads.
This issue relative to code in ASIHTTPRequest:

#pragma mark threading behaviour

// In the default implementation, all requests run in a single background thread
// Advanced users only: Override this method in a subclass for a different threading behaviour
// Eg: return [NSThread mainThread] to run all requests in the main thread
// Alternatively, you can create a thread on demand, or manage a pool of threads
// Threads returned by this method will need to run the runloop in default mode (eg CFRunLoopRun())
// Requests will stop the runloop when they complete
// If you have multiple requests sharing the thread or you want to re-use the thread, you'll need to restart the runloop
+ (NSThread *)threadForRequest:(ASIHTTPRequest *)request
{
if (!networkThread) {
networkThread = [[NSThread alloc] initWithTarget:self selector:@selector(runRequests) object:nil];
[networkThread start];
}
return networkThread;
}

…….

- (void)cancel
{
[self performSelector:@selector(cancelOnRequestThread) onThread:[[self class] threadForRequest:self] withObject:nil waitUntilDone:NO];
}