I have a little app that downloads stock prices and was working perfectly (for years) until my recent upgrade to 10.5.7. After the upgrade, the program would crash on this
At The Omni Group, we are seeing this new crash in 10.5.7 in all of our NSURL-using applications too, in asynchronous use (so I think the synchronous thing is a red herring). We've also had it happen in TextMate (presumably in the software update).
I've logged Radar 6932684 on this issue, and if you are seeing it too, I strongly suggest you all report it, with whatever details you can gather.
For what it's worth, this seems to be fixed on 10.5.8 --- but it's been replaced by a new bug which prepends the redirect's response-body to the real response about 2% of the time. (Easy to reproduce by spinning on -stringWithContentsOfURL:, but we've also seen it in the wild.) Reported that bug as radar #7169953.
I have also seen this exact same crash appearing in 10.5.7 in several applications.
Given that implementing the simplest connection:willSendRequest:redirectResponse:
delegate method will solve the problem, I think might be highly related to radar #6700222.
I think Eugene's reply in this thread is properly describing the problem; after some testing, here is what I've concluded seems to be going on, with hopefully some details to help others stuck with this issue:
Redirecting URLs will periodically cause failures. This happens both in the sync and async usages of NSURLConnection
. I've created a test project to track down this bug, and this crash will consistently occur (typically between 25-500 iterations). Running the same test on 10.5.6 or without redirecting URLs does not fail (have run it up to 20,000 iterations).
There are two possible work-arounds:
stringWithContentsOfURL:
) and this will work fine. Dennis, in your case, the proper server URL is download.finance.yahoo.com
, not finance.yahoo.com
, so I believe this would fix your particular problem. Using curl
you can see that you get a 301 redirect when you hit the latter address.connection:willSendRequest:redirectResponse:
- (NSURLRequest *)connection:(NSURLConnection *)connection
willSendRequest:(NSURLRequest *)request
redirectResponse:(NSURLResponse *) redirectResponse
{
return request;
}
All of this seems to suggest to me that there is something broken in Apple's implementation in 10.5.7, but if anyone else has any insights as to what might be going on, please chime in.
I have submitted a bug with my test project to Apple as rdar://6936109
and referenced tjw's report (Radar 6932684).
I'd suggest not using synchronous URL connections. It does require some code restructuring, but it's really bad behaviour to block the main thread on network. (Assuming you're doing this in the main thread).
Also, I'm guessing it's code that Apple is planning to deprecate or stop maintaining, which might be what you're seeing here.
Hope that helps….
This seems to be related to redirects. My app downloading about 500 megs of data directly (few hundred separate files) doesn't crash. Same app downloading smaller set of urls, all of which are redirected will randomly crash several times in exactly that point (restart resumes, and restarting over and over again will actually result in successful download).
EDIT: BTW, Colin's suggestion about reimplementing redirects doesn't seem to work for NSURLDownload :(.
EDIT2: Ok, this seems to be a race condition. Adding cerr << "redirect" << endl; in the callback "fixes" it for me with NSURLDownload. Sleeping for 1 second or locking a local static mutex have no effect however...