Close behavior is unclear
steamstreetjon opened this issue · comments
Describe the bug
A simple application, from main, doesn't exit immediately after completion. After investigation, I can see that if I call 'close' on the service, it completed, but this isn't documented anywhere so I'm not sure if this is expected.
Expected behavior
After completing the task, the execution of the application should exit.
Current behavior
An application that uses a service client will not immediately exit, and instead waits for a minute before exiting.
Steps to Reproduce
The following application was created:
fun main() {
runBlocking {
S3Client.fromEnvironment().listBuckets {
}
}
println("done")
}
When it runs, the done
is output quickly, but the application doesn't exit for some time.
Possible Solution
Changing the code as follows fixes the problem, but does not appear to be documented or understood.
fun main() {
runBlocking {
val client = S3Client.fromEnvironment()
client.listBuckets {
}
client.close()
}
println("done")
}
Context
Trying to create simple tools and it was very confusing what was happening when an application doesn't exit.
AWS Kotlin SDK version used
1.0.48
Platform (JVM/JS/Native)
JVM
Operating System and version
MacOS Sonoma
Thanks for the feedback. This is documented in the developer guide here.
If you are using the default HTTP client (as it would appear) the hang is likely due to the OkHttp dispatcher and/or thread pool preventing the application from exiting right away. Closing the client releases any resources associated with it (including closing the HTTP client which in this case would close the OkHttp resources) preventing any leaks and allowing the program to exit cleanly.
We can't give specifics about the resources held by any given client as it will differ depending on the HTTP engine used and may evolve over time (e.g. we were discussing recently about whether SDK clients should/could implement CoroutineScope
which would be another resource that needs released).
This is "expected" behavior in that there are underlying resources still open that are preventing the JVM from exiting right away.
The SDK clients all implement Closeable which have the use extension method (similar to the one in stdlib). You can use it to ensure clients are closed up automatically.
e.g.
suspend fun main() {
S3Client.fromEnvironment().use { s3 ->
s3.listBuckets().let(::println)
} // "s3" client is valid until here after which it will be closed (even if an exception is thrown)
println("done")
}
It looks like this issue has not been active for more than 5 days. In the absence of more information, we will be closing this issue soon. If you find that this is still a problem, please add a comment to prevent automatic closure, or if the issue is already closed please feel free to reopen it.