apollographql / apollo-ios

📱  A strongly-typed, caching GraphQL client for iOS, written in Swift.
https://www.apollographql.com/docs/ios/
MIT License
3.86k stars 712 forks source link

Crash Thread 1: Fatal error: No data found for task 1, cannot append received data #3409

Closed KhaledElsherbeny closed 2 weeks ago

KhaledElsherbeny commented 1 month ago

Summary

crash occurred randomly while starting calling query

Screenshot 2024-07-11 at 4 16 18 PM

Version

1.13.0

Steps to reproduce the behavior

crash occurred randomly while starting calling query

Screenshot 2024-07-11 at 4 16 18 PM

Logs

No response

Anything else?

No response

AnthonyMDev commented 1 month ago

Interesting! I don't think we've ever had anyone hit this assertion failure before! It's possible there is a race condition going on here, but I would need more information in order to determine what's going on here.

Are you using a custom InterceptorProvider? I might need some more example code or a reproduction case to dig into this.

KhaledElsherbeny commented 1 month ago

Thanks for your response @AnthonyMDev We don't use any custom InterceptorProvider

func createApolloClient() -> ApolloClientProtocol {
        let configuration = URLSessionConfiguration.default
        configuration.httpAdditionalHeaders = headers

        NetworkLogger.enableLogging(for: configuration)

        let store = ApolloStore(cache: ApolloCacheContainer.shared.normalizedCache())

        let client = URLSessionClient(sessionConfiguration: configuration)
        let provider = DefaultInterceptorProvider(client: client, store: store)

        let networkTransport = RequestChainNetworkTransport(interceptorProvider: provider, endpointURL: url)
        let apolloClient = ApolloClient(networkTransport: networkTransport, store: store)

        return ApolloClientProxy(apolloClient: apolloClient)
    }

and our ApolloClientProxy class we added to perform some business needs but its simple

final class ApolloClientProxy: ApolloClientProtocol {
    var apolloClient: ApolloClient
    var store: ApolloStore

    init(apolloClient: ApolloClient) {
        self.apolloClient = apolloClient
        store = apolloClient.store
    } 
    }

please advise is there any idea to help ?

calvincestari commented 1 month ago

@KhaledElsherbeny is this a consistent crash? Is it reproducible?

calvincestari commented 1 month ago

The cause of the assertion is that the internal task (TaskData) related to the URLSessionDataTask cannot be found. We use the TaskData instance to store the response, data and completion handlers for the request; I thought we should maybe send a request failure result instead of asserting but without TaskData we don't have a completion block so an assertion might be the only thing we can do here otherwise it would just fail silently.

There are a number of ways the internal task data can be removed:

  1. A call to invalidate which essentially kills the session client so this is a pretty severe action. The only place we call this method is when the DefaultInterceptorProvider is released (deinit) and that is further controllable through the shouldInvalidateClientOnDeinit property.
  2. A call to clear(task:) which is called when a task is cancelled or completes. I don't believe we ever make the call to cancel a task.
  3. A call to clearAllTasks which is called by invalidate or when 4 occurs.
  4. URLSession being invalidated through the delegate call urlSession(_:didBecomeInvalidWithError:)

I recommend checking whether you call any of those methods in your own code. Also trying to nail down the circumstances of, and how reproducible, the error is would be helpful in tracking down the root cause.

AnthonyMDev commented 2 weeks ago

@KhaledElsherbeny were you ever able to resolve this one? We're going to need some more information on this in order to determine if there is a bug in our code and how to resolve it.

We're closing this for now due to inactivity, but we will be happy to re-open if you respond with additional information.

github-actions[bot] commented 2 weeks ago

Do you have any feedback for the maintainers? Please tell us by taking a one-minute survey. Your responses will help us understand Apollo iOS usage and allow us to serve you better.