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

Query execute prevents process from finishing in 3.0.0 beta 04 #3619

Closed
andreaskaltenbach opened this issue Nov 26, 2021 · 12 comments
Closed

Comments

@andreaskaltenbach
Copy link

Summary
Using an Apollo Android Client and performing a query execute will make that the Java process never finishes.

Version
3.0.0 beta 04

Description

When running the following code, the Java process will terminate correctly with exit code 0:

fun main() = runBlocking {

    val apolloClient = ApolloClient.Builder()
        .serverUrl("https://www.google.com")
        .build()

    try {
        val query = apolloClient.query(OrganizationQuery(id = "232"))

//        uncomment line below and the process will never finish with exit code 0
//        val response = query.execute()

    } catch (ex:ApolloHttpException) {
        // Do not care about exceptions in this case :)
        println(ex.message)
    }
    apolloClient.dispose()
}

If I uncomment the line which performs the query.execute(), the Java program will never terminate.

Note that I even have to call apolloClient.dispose() explicitly. Otherwise the Apollo Client will prevent the process from finishing as well.

Do ApolloClient and ApolloQueryCall have any resources that need to be cleaned up?

@martinbonnin
Copy link
Contributor

martinbonnin commented Nov 26, 2021

One thing that comes to mind is the OkHttp threadpools.

Can you try passing an explicit OkHttpClient and releasing it as in https://square.github.io/okhttp/4.x/okhttp/okhttp3/-ok-http-client/#shutdown-isnt-necessary

    val okHttpClient = OkHttpClient.builder().build()

    val apolloClient = ApolloClient.Builder()
        .serverUrl("https://www.google.com")
        .okHttpClient(okHttpClient)
        .build()

    // do things...

    // when done
    apolloClient.dispose()
    okHttpClient.dispatcher().executorService().shutdown();
    okHttpClient.connectionPool().evictAll();
  

@andreaskaltenbach
Copy link
Author

Thanks for the fast reply @martinbonnin .
I added the okHttpClient and the cleanup calls in the end but I run into the same problem.

I even tested with Apollo Android 2.5.11 and it seems to be the same thing there.

@martinbonnin
Copy link
Contributor

Yep, this rings a bell. Let me try quickly

@martinbonnin
Copy link
Contributor

Nope, all seems to work here. Can you check https://github.com/martinbonnin/termination-test/tree/main and see if you can make it hang ?

@andreaskaltenbach
Copy link
Author

Really strange. Your example is exiting properly. Will have to check what's different compared to my project setup.
Will let you know.
Thanks for your help!

@martinbonnin
Copy link
Contributor

Sure thing! If you manage to hook a debugger, it might interesting to see what threads are still running.

Let us know what you find!

@andreaskaltenbach
Copy link
Author

@martinbonnin I boiled it down to this dependency:

implementation("com.apollographql.apollo3:apollo-mockserver:3.0.0-beta04")

If I add it to my project, the Java process exits as it should. Removing the line from your project will make your main running forever as well.

Is this dependency adding something that the runtime dependency is not?

@martinbonnin
Copy link
Contributor

Good catch! mockserver pulls a more recent version of OkHttp. Looks like 3.14.0 and later fixes this issue.

You can workaround by adding an explicit dependency to your project:

dependencies {
  // anything higher than 3.14.0 will do. Might as well use latest
  implementation("com.squareup.okhttp3:okhttp:4.9.1")
}

We were discussing with @BoD the opportunity to update OkHttp. Looks like that settles it. Also OkHttp 3 stop being supported in December 2021, yet another reason to update.

@martinbonnin
Copy link
Contributor

Hi @andreaskaltenbach 👋
Is it ok if we close this issue? 3.0.0-rc01 has the latest version of OkHttp so this should not happen again 🤞

@andreaskaltenbach
Copy link
Author

Yes, that's fine. I'll update to 3.0.0-rc01 right away!

@AddictArts
Copy link

This is happening in the latest v3 stable release.

@BoD
Copy link
Contributor

BoD commented Jan 21, 2022

(Just a note that this is followed up in #3802 - leaving this one closed for now)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants