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

How to properly close shutdown an apollo client version 3? #3802

Closed
AddictArts opened this issue Jan 21, 2022 · 5 comments · Fixed by #3803
Closed

How to properly close shutdown an apollo client version 3? #3802

AddictArts opened this issue Jan 21, 2022 · 5 comments · Fixed by #3803

Comments

@AddictArts
Copy link

AddictArts commented Jan 21, 2022

With version 3 a simple fun main with a runBlocking calling a query execute will continue to run and not exit. When commenting out the Apollo code the main exits and process exits. Thanks for the help.

Note: This didn't help
https://github.com/martinbonnin/termination-test/blob/main/src/main/kotlin/com/example/Main.kt
and I am not sure how it worked since the below
okHttpClient.dispatcher().executorService().shutdown()
for me is
okHttpClient.dispatcher.executorService.shutdown()

The only different thing I've done besides that code there, is the okHttpClient and apolloClient are defined outside the fun main like the apollo version 3 docs suggests apolloClient is a top-level variable.

@martinbonnin
Copy link
Contributor

Hi 👋

You can use ApolloClient.dispose() to free resources associated with the ApolloClient. Note that this doesn't shut down the okHttpClient as it may be shared with other users. If you want to do so, keep a reference to the OkHttpClient and call okHttpClient.dispatcher.executorService.shutdown() as you indicated above (the difference in methods vs properties is for okhttp 3 vs okhttp 4)

@martinbonnin
Copy link
Contributor

My bad, looks like there's a regression there that keeps a thread open. Will keep looking and update this thread.

@martinbonnin
Copy link
Contributor

martinbonnin commented Jan 21, 2022

Indeed, this should be fixed with #3803. Sorry about this. Until next version, you can set a no-op SubscriptionNetworkTransport as a workaround:

      ApolloClient.Builder()
      .serverUrl("https://www.google.com")
      .okHttpClient(okHttpClient)
      .subscriptionNetworkTransport(object: NetworkTransport {
        override fun dispose() {
        }
        override fun <D : Operation.Data> execute(request: ApolloRequest<D>): Flow<ApolloResponse<D>> {
          TODO("Not yet implemented")
        }
      })

@AddictArts
Copy link
Author

Thanks for validating. That led to
Caused by: java.lang.IllegalStateException: Apollo: 'webSocketEngine' has no effect if 'subscriptionNetworkTransport' is set at com.apollographql.apollo3.ApolloClient$Builder.build(ApolloClient.kt:482)

@martinbonnin
Copy link
Contributor

ooops, my bad.okHttpClient will indeed set a webSocketEngine based on OkHttp too. Use httpEngine instead:

  val apolloClient = ApolloClient.Builder()
    .serverUrl("https://www.google.com")
    .httpEngine(DefaultHttpEngine(okHttpClient))
    .subscriptionNetworkTransport(object : NetworkTransport {
      override fun dispose() {
      }
      override fun <D : Operation.Data> execute(request: ApolloRequest<D>): Flow<ApolloResponse<D>> {
        TODO("Not yet implemented")
      }
    })
    .build()

This was referenced Dec 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
2 participants