[core] Ensure Bot's CoroutineScope is always cancelled on Bot.close if network.close threw an exception

This commit is contained in:
Him188 2022-12-27 17:28:52 +00:00
parent 38162db477
commit 2252683cee
No known key found for this signature in database
GPG Key ID: BA439CDDCF652375

View File

@ -110,14 +110,16 @@ internal abstract class AbstractBot constructor(
override fun close(cause: Throwable?) {
if (!this.isActive) return
if (networkInitialized) {
network.close(cause)
}
if (cause == null) {
supervisorJob.cancel()
} else {
supervisorJob.cancel(CancellationException("Bot closed", cause))
try {
if (networkInitialized) {
network.close(cause)
}
} finally { // ensure CoroutineScope is always closed
if (cause == null) {
supervisorJob.cancel()
} else {
supervisorJob.cancel(CancellationException("Bot closed", cause))
}
}
}