From 494aa0ede7dbd9511b31198ab6a0d8fda5ab2261 Mon Sep 17 00:00:00 2001 From: Julian Chang Date: Thu, 3 Dec 2026 18:54:08 -0500 Subject: [PATCH] Keep a replaced EOS platform from freeing the new platform's callbacks --- .../client/network/eos/EosSdkBridge.kt | 55 ++++++++++++++----- 1 file changed, 40 insertions(+), 15 deletions(-) diff --git a/src/main/kotlin/org/polyfrost/polyplus/client/network/eos/EosSdkBridge.kt b/src/main/kotlin/org/polyfrost/polyplus/client/network/eos/EosSdkBridge.kt index 38bb26276..f28da0c23 100644 --- a/src/main/kotlin/org/polyfrost/polyplus/client/network/eos/EosSdkBridge.kt +++ b/src/main/kotlin/org/polyfrost/polyplus/client/network/eos/EosSdkBridge.kt @@ -98,6 +98,12 @@ class EosSdkBridge { @Volatile private var sdkPoisoned = false + private val sdkLock = Any() + + private var sdkOwner: EosSdkBridge? = null + + private var logCallbackInstalled = false + internal fun markSdkRetired() { sdkRetired = true } @@ -149,6 +155,7 @@ class EosSdkBridge { starting.set(false) return false } + synchronized(sdkLock) { sdkOwner = this } tickThread = worker running = true worker.start() @@ -220,7 +227,12 @@ class EosSdkBridge { sdkInitialized = true logTarget = this - EosLogging.setCallback { msg -> logTarget?.onLog(msg.category, msg.level, msg.message) } + synchronized(sdkLock) { + if (!logCallbackInstalled) { + EosLogging.setCallback { msg -> logTarget?.onLog(msg.category, msg.level, msg.message) } + logCallbackInstalled = true + } + } EosLogging.setLogLevel(EosLogCategory.AllCategories, EosLogLevel.Info) val mode = EosMode.current @@ -354,7 +366,7 @@ class EosSdkBridge { park(idleTicks) } - runCatching { pump() }.onFailure { logger.warn("The final EOS tick before shutdown failed", it) } + if (!abandoned) runCatching { pump() }.onFailure { logger.warn("The final EOS tick before shutdown failed", it) } rejectPendingCalls() teardown(retireSdk = !abandoned && !keepSdk) } @@ -446,20 +458,33 @@ class EosSdkBridge { detachHandlers() - this.platform?.let { platform -> - this.platform = null - runCatching { platform.close() }.onFailure { logger.warn("Closing the EOS platform failed", it) } - } + synchronized(sdkLock) { + val ownsSdk = sdkOwner === this && !abandoned + if (ownsSdk) sdkOwner = null - if (sdkInitialized) { - sdkInitialized = false - when { - !retireSdk -> Unit - sdkPoisoned -> - logger.warn("Not shutting the EOS SDK down; an abandoned platform from this process is still open") - else -> { - sdkRetired = true - runCatching { Eos.shutdown() }.onFailure { logger.warn("Shutting the EOS SDK down failed", it) } + this.platform?.let { platform -> + this.platform = null + if (!ownsSdk) { + logger.warn("Leaving a replaced EOS platform open until the game exits; closing it would free the callbacks of the platform that replaced it") + return@let + } + if (logCallbackInstalled) { + runCatching { EosLogging.setCallback(null) }.onFailure { logger.warn("Clearing the EOS log callback failed", it) } + logCallbackInstalled = false + } + runCatching { platform.close() }.onFailure { logger.warn("Closing the EOS platform failed", it) } + } + + if (sdkInitialized) { + sdkInitialized = false + when { + !retireSdk -> Unit + sdkPoisoned || !ownsSdk -> + logger.warn("Not shutting the EOS SDK down; another EOS platform from this process is still open") + else -> { + sdkRetired = true + runCatching { Eos.shutdown() }.onFailure { logger.warn("Shutting the EOS SDK down failed", it) } + } } } }