mirror of
https://github.com/mamoe/mirai.git
synced 2025-02-10 02:27:01 +08:00
Move setState
from coroutine scope completion handler to network.close
, fix #1311
This commit is contained in:
parent
8d4eb45386
commit
2e47740b1b
@ -148,6 +148,8 @@ internal interface NetworkHandler : CoroutineScope {
|
||||
|
||||
/**
|
||||
* Closes this handler gracefully (i.e. asynchronously).
|
||||
*
|
||||
* After invocation of [close], [state] will always be [State.CLOSED].
|
||||
*/
|
||||
fun close(cause: Throwable?)
|
||||
|
||||
|
@ -47,11 +47,6 @@ internal abstract class NetworkHandlerSupport(
|
||||
}
|
||||
|
||||
override fun close(cause: Throwable?) {
|
||||
// if (cause == null) {
|
||||
// logger.info { "NetworkHandler '$this' closed" }
|
||||
// } else {
|
||||
// logger.info { "NetworkHandler '$this' closed: $cause" }
|
||||
// }
|
||||
if (coroutineContext.job.isActive) {
|
||||
coroutineContext.job.cancel("NetworkHandler closed", cause)
|
||||
}
|
||||
@ -202,6 +197,7 @@ internal abstract class NetworkHandlerSupport(
|
||||
protected data class StateSwitchingException(
|
||||
val old: BaseStateImpl,
|
||||
val new: BaseStateImpl,
|
||||
override val cause: Throwable? = new.getCause(), // so it can be unwrapped
|
||||
) : CancellationException("State is switched from $old to $new")
|
||||
|
||||
/**
|
||||
|
@ -195,15 +195,14 @@ internal open class NettyNetworkHandler(
|
||||
///////////////////////////////////////////////////////////////////////////
|
||||
|
||||
override fun close(cause: Throwable?) {
|
||||
if (state == State.CLOSED) return // already
|
||||
super.close(cause)
|
||||
// when coroutine scope completed, state is already set to CLOSED,
|
||||
// see the following `init` block.
|
||||
if (state == State.CLOSED) return // quick check if already closed
|
||||
if (setState { StateClosed(cause) } == null) return // atomic check
|
||||
super.close(cause) // cancel coroutine scope
|
||||
}
|
||||
|
||||
init {
|
||||
coroutineContext.job.invokeOnCompletion { e ->
|
||||
setState { StateClosed(e?.unwrapCancellationException()) }
|
||||
close(e?.unwrapCancellationException())
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user