Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Crash in URLSessionClient #1181

Closed
eelcokoelewijn opened this issue May 2, 2020 · 14 comments
Closed

Crash in URLSessionClient #1181

eelcokoelewijn opened this issue May 2, 2020 · 14 comments
Labels
crash Issues which cause crashes networking-stack
Milestone

Comments

@eelcokoelewijn
Copy link

I'm experiencing a crash(EXC_BAD_ACCESS) in the URLSessionClient when a pending GQL request gets deallocated. For example when a screen is loading data and dismissed before completing the request.

The crash is happening in sendRequest(_ request: URLRequest, rawTaskCompletionHandler: RawCompletion? = nil, completion: @escaping Completion) -> URLSessionTask, when performing self.rawCompletions[dataTask.taskIdentifier] = rawCompletion.

See also the attached crashlog.crash.zip.

In my case there are multiple GQL request performed while the screen gets deallocated. All pending requests are cancelled before the screen gets removed.

If I can provide more information or it needs more explanation, please let me know.

@designatednerd
Copy link
Contributor

Looking at the stack, there's a bunch of Promise stuff going on before this gets enqueued, which indicates there's some kind of cache work going on before the call actually gets made.

Can you answer a few questions for me to help narrow down what might be going on:

  • Are you setting up your own URLSessionClient or using the default one? Either subclassing or setting it up before setting up the HTTPNetworkTransport?
  • Are you using the returnCacheDataElseFetch cache policy? This is the default one so if you're not doing anything to set it, this is what you'd be using. If not, which cache policy are you using?
  • Are you using query watchers here or is this just individual requests?
  • Is there any point at which the entire ApolloClient might be getting deallocated - for instance, are you using a separate Client instance per screen?

@sneakyness
Copy link

I'm getting this too!

Normal URLSessionClient. Default cache I think. Individual requests but a few of em. <Apollo.URLSessionClient: 0x6000037e9020> exists but that's as far as I managed to look, not doing anything wonky with client afaik? Project & Codegen are both v0.27

@designatednerd
Copy link
Contributor

@sneakyness Also getting this when cancelling or are you seeing it at a different time?

@eelcokoelewijn
Copy link
Author

@designatednerd, see answers below.

Are you setting up your own URLSessionClient or using the default one? Either subclassing or setting it up before setting up the HTTPNetworkTransport?

Using the default URLSessionClient, no sub-classing.

Are you using the returnCacheDataElseFetch cache policy? This is the default one so if you're not doing anything to set it, this is what you'd be using. If not, which cache policy are you using?

We are using the returnCacheDataAndFetch policy.

Are you using query watchers here or is this just individual requests?

Individual requests.

Is there any point at which the entire ApolloClient might be getting deallocated - for instance, are you using a separate Client instance per screen?

We use a singleton/ shared client for all the requests.

@pkluz
Copy link

pkluz commented May 3, 2020

seeing the same crash after updating. reverted back to previous version that was working for us (0.24.0).

The same client is used for all apollo requests and it is only re-allocated if the session configuration headers need to change (i.e. Bearer token has changed and needs to re-set.)

This requires a re-allocation of the Apollo client since the session configuration cannot easily be changed once passed.

@sneakyness
Copy link

@designatednerd Just normal requests, some of them nest fairly deep and we hit a few of those rapid fire up front, which is what seems to do it. Sometimes the promise chain is longer, other times it doesn't crash but the app is somehow up in the 100's for thread lifecycles (!)

I thought I might have been trying to be too concurrent, so I pulled all that out and it persisted. If I had to ship tomorrow, I'd follow pkluz and downgrade to 0.24.

Should I hoard crashlogs in the meantime?

@designatednerd
Copy link
Contributor

Yes any crashlogs y'all can share would be a huge help.

I'll try to see if I can throw enough concurrent requests at it to make it crash locally.

@designatednerd designatednerd added the crash Issues which cause crashes label May 3, 2020
@designatednerd
Copy link
Contributor

Oh one more question: For those of you seeing this crash, are you seeing it on both simulator and device, or device only?

@pkluz
Copy link

pkluz commented May 3, 2020

tested on device only

@designatednerd
Copy link
Contributor

OK as I was looking at the code it occurred to me that particularly if things are getting kicked off by Promises on random threads, I need to guard more defensively against concurrent access from multiple threads, which definitely is a recipe for EXC_BAD_ACCESS.

Before I go too far down other rabbit holes, can I ask the brave among you to see if this reasonably simple patch fixes the issue without totally tanking performance: fix/atomic-dicts?

@sneakyness
Copy link

I yeeted it, definitely seems better. Far less threads being burned. I can't get it to crash

🚀

@designatednerd
Copy link
Contributor

OK - PR opened at #1184 - would appreciate any further feedback on this if anyone has it. If I don't hear anything else, I'll ship that as a patch release tomorrow.

@eelcokoelewijn
Copy link
Author

For those of you seeing this crash, are you seeing it on both simulator and device, or device only?

Both, I can reproduce the crash on the device and simulator.

Before I go too far down other rabbit holes, can I ask the brave among you to see if this reasonably simple patch fixes the issue without totally tanking performance: fix/atomic-dicts?

The patch seems to fix the crash, I can't get the app to crash anymore.

@designatednerd designatednerd added this to the Next Release milestone May 4, 2020
@designatednerd
Copy link
Contributor

designatednerd commented May 4, 2020

Fix shipped with 0.27.1! Thank you all for your help in chasing this one down.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
crash Issues which cause crashes networking-stack
Projects
None yet
Development

No branches or pull requests

4 participants