gildor / kotlin-coroutines-retrofit

Kotlin Coroutines await() extension for Retrofit Call
Apache License 2.0
847 stars 64 forks source link

Program doesn't terminate when invoking one of the suspend library functions #14

Closed cypressious closed 7 years ago

cypressious commented 7 years ago

This program doesn't terminate

fun main(args: Array<String>) = runBlocking(CommonPool) {
    val result = service.someCall().awaitResponse()
    println(result) 
}
gildor commented 7 years ago

I've tried and I see no issue: https://gist.github.com/gildor/aa2b2f23ffbc963e3bde2f180150cc07

Could you reproduce this issue and share code? The only possible reason that I see it's some exceptions in your OkHttp interceptor or Retrofit adapter factory, I just not sure that in this case, coroutine getting callbacks

cypressious commented 7 years ago

When I build and run the project in https://gist.github.com/anonymous/e13969100d35b778986db6bf7fb2275f, the app doesn't terminate.

cypressious commented 7 years ago

I'm sorry, the gist is a bit messy, you can compile the project from these sources

KotlinDesktopPlayground.zip

gildor commented 7 years ago

Yes, I see that if I use gradle application plugin and run task. Looks like some thread still alive, even after coroutines finished. I will investigate this

gildor commented 7 years ago

@cypressious Sorry, I had no time to investigate, but just now got the same problem with retofit + rxjava2 call adapter RxJava2CallAdapterFactory.createAsync() so it's not only coroutine implementation problem. And program will be finished in 1 min, same as for coroutines. Will investigate.

gildor commented 7 years ago

@cypressious The reason of that behavior Executors.newCachedThreadPool() that used as default executor for Retrofit2 (except cases when you run request synchronously). According to [docs](https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/Executors.html#newCachedThreadPool()) threads of CachedThreadPool wait 60 seconds before shutdown. So I see only one way: set custom Executor to OkHttpClient and shutdown it manually or create executor that creates daemon threads.

gildor commented 7 years ago

@cypressious I'm closing this issue. if you have some other ideas please reopen with your comment