Fix the resource leak when closing TxCaptchaHelper(Swing) directly

This commit is contained in:
Karlatemp 2021-06-01 12:47:42 +08:00
parent c89d31cef6
commit 1e95482158
No known key found for this signature in database
GPG Key ID: 21FBDDF664FF06F8
2 changed files with 8 additions and 4 deletions

View File

@ -11,15 +11,13 @@ package net.mamoe.mirai.utils
import io.ktor.client.*
import io.ktor.client.request.*
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.delay
import kotlinx.coroutines.isActive
import kotlinx.coroutines.launch
import kotlinx.coroutines.*
import net.mamoe.mirai.Mirai
internal abstract class TxCaptchaHelper {
private val newClient: Boolean
val client: HttpClient
private lateinit var queue: Job
init {
var newClient = false
@ -62,5 +60,10 @@ internal abstract class TxCaptchaHelper {
if (newClient) {
queue.invokeOnCompletion { client.close() }
}
this.queue = queue
}
fun dispose() {
queue.cancel()
}
}

View File

@ -86,6 +86,7 @@ public object SwingSolver : LoginSolver() {
}
}
helper.start(this, url)
txhelperSolver.def.invokeOnCompletion { helper.dispose() }
solver.def.complete(txhelperSolver.openAndWait().trim())
}
openWithTxCaptchaHelper.onClick { doClickEvent() }