From c66f977963e139157109f82112828aa607f576f1 Mon Sep 17 00:00:00 2001 From: Robinson Date: Thu, 29 Jun 2023 19:27:25 +0200 Subject: [PATCH] Endpoints will auto-close during JVM shutdown --- src/dorkbox/network/connection/EndPoint.kt | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/src/dorkbox/network/connection/EndPoint.kt b/src/dorkbox/network/connection/EndPoint.kt index d224b708..47c4f772 100644 --- a/src/dorkbox/network/connection/EndPoint.kt +++ b/src/dorkbox/network/connection/EndPoint.kt @@ -157,6 +157,10 @@ abstract class EndPoint private constructor(val type: C */ internal val crypto: CryptoManagement + + private val hook: Thread + + // manage the startup state of the endpoint. True if the endpoint is running internal val endpointIsRunning = atomic(false) @@ -264,6 +268,14 @@ abstract class EndPoint private constructor(val type: C handshaker = Handshaker(logger, config, serialization, listenerManager, aeronDriver) { errorMessage, exception -> return@Handshaker newException(errorMessage, exception) } + + hook = Thread { + runBlocking { + closeSuspending(true, true) + } + } + + Runtime.getRuntime().addShutdownHook(hook) } internal fun isServer(function: Server.() -> Unit) { @@ -889,11 +901,11 @@ abstract class EndPoint private constructor(val type: C * * @param closeEverything unless explicitly called, this is only false when a connection is closed in the client. */ - internal suspend fun closeSuspending(closeEverything: Boolean) { + internal suspend fun closeSuspending(closeEverything: Boolean, initiatedByShutdown: Boolean = false) { // 1) endpoints can call close() // 2) client can close the endpoint if the connection is D/C from aeron (and the endpoint was not closed manually) val shutdownPreviouslyStarted = shutdownInProgress.getAndSet(true) - if (shutdownPreviouslyStarted && !closeEverything) { + if (shutdownPreviouslyStarted && closeEverything) { // this is only called when the client network event poller shuts down // if we have clientConnectionClosed, then run that logic (because it doesn't run on the client when the connection is closed remotely) @@ -909,6 +921,10 @@ abstract class EndPoint private constructor(val type: C return } + if (!shutdownPreviouslyStarted && initiatedByShutdown) { + Runtime.getRuntime().removeShutdownHook(hook) + } + EventDispatcher.CLOSE.launch { logger.debug { "Shutting down endpoint..." }