twitter-archive / CocoaSPDY

SPDY for iOS and OS X
Apache License 2.0
2.39k stars 233 forks source link

Request in NSURLConnection and UIWebView will lead to crash. #42

Closed jwx0925 closed 9 years ago

jwx0925 commented 10 years ago

I send many requests with my custom NSURLConnection.After that I send many other same request in UIWebView. The result is crash!I found SPDYSocket is not thread-safe. And NSURLConnection and UIWebView will run in different threads.(NSURLConnectionLoader and WebCore CFNetwork) How to fix?

jwx0925 commented 10 years ago

Can I use this method to let WebView request run in NSURLConnectionLoader thread? The code:

- (void)startLoading
{
    NSRunLoop *socketRunLoop = [SPDYSocket socketRunLoop];
    if (socketRunLoop && socketRunLoop != [NSRunLoop currentRunLoop]){
        [socketRunLoop performSelector:@selector(startLoading0) target:self argument:nil order:0 modes:@[NSRunLoopCommonModes]];
    }else{
        [self startLoading0];
    }
}
goaway commented 10 years ago

This seems risky - if you had a UIWebView start loading requests first, you could end up with your normal NSURLConnection requests dispatched to the UIWebView's thread. Also you'd need to intercept on calls to stopLoading as well.

I had originally considered using a dedicated shared thread for all networking. That would solve this and might be preferable. The simplest way to accomplish that would be to modify startLoading and stopLoading to both dispatch to the thread - then you could guarantee its behavior.

However, you may run into other issues if NSURLProtocolClient callbacks expect to be called on the network dispatch thread. Might need an additional level of dispatching within SPDYStream as well.

I realize it's not ideal to have separate sockets for UIWebViews and NSURLConnection requests, and I'll think more about a long term solution.

That said, I'm still curious about the crash you mentioned. Can you share a stack trace?

jwx0925 commented 10 years ago

It's not necessary to use two threads.I had modified startLoading and stopLoading to both dispatch to the socket thread.And it works well. I did not find the problem about NSURLProtocolClient callback.

goaway commented 10 years ago

Cool, glad to hear it worked.