From ad0d6ed591ab0225033ead065d6b75eaac1b8610 Mon Sep 17 00:00:00 2001 From: oglassdev <23040613+oglassdev@users.noreply.github.com> Date: Wed, 14 Oct 2026 19:37:50 -0400 Subject: [PATCH] feat(coroutines): let async handlers and executors run in a caller-owned scope Add asyncHandler, executorAsync and defaultExecutorAsync overloads that take a CoroutineScope, so cancelling that scope stops their coroutines. All async variants now report failures to Minestom's ExceptionManager instead of letting one failing handler cancel the scope it runs in. --- .../kotstom/coroutines/CoroutineEvents.kt | 16 +++++++- .../kotstom/coroutines/LaunchReporting.kt | 22 ++++++++++ .../kotstom/coroutines/kommand/KommandExt.kt | 23 ++++++++--- .../kotstom/coroutines/kommand/SyntaxExt.kt | 22 ++++++++-- .../kotstom/test/CoroutineEventsTest.kt | 41 +++++++++++++++++++ 5 files changed, 113 insertions(+), 11 deletions(-) create mode 100644 src/main/kotlin/net/bladehunt/kotstom/coroutines/LaunchReporting.kt create mode 100644 src/test/kotlin/net/bladehunt/kotstom/test/CoroutineEventsTest.kt diff --git a/src/main/kotlin/net/bladehunt/kotstom/coroutines/CoroutineEvents.kt b/src/main/kotlin/net/bladehunt/kotstom/coroutines/CoroutineEvents.kt index bf7754a..6267241 100644 --- a/src/main/kotlin/net/bladehunt/kotstom/coroutines/CoroutineEvents.kt +++ b/src/main/kotlin/net/bladehunt/kotstom/coroutines/CoroutineEvents.kt @@ -1,7 +1,6 @@ package net.bladehunt.kotstom.coroutines import kotlinx.coroutines.CoroutineScope -import kotlinx.coroutines.launch import kotlinx.coroutines.runBlocking import net.bladehunt.kotstom.dsl.EventBuilder import net.minestom.server.event.Event @@ -17,6 +16,19 @@ inline fun EventBuilder.blockingHandler(crossinline block: suspen runBlocking { block(it) } } +/** + * A coroutine handler that runs asynchronously in [scope]. Cancelling [scope] cancels running + * handlers, and handler failures are reported to the `ExceptionManager` without cancelling [scope]. + * + * @param scope The `CoroutineScope` that owns the launched coroutines + * @param block The suspending code to run + * @author oglassdev + */ +inline fun EventBuilder.asyncHandler( + scope: CoroutineScope, + crossinline block: suspend (T) -> Unit +) = handler { scope.launchReporting { block(it) } } + /** * A coroutine handler that runs asynchronously using the `MinestomDispatcher` * @@ -27,4 +39,4 @@ inline fun EventBuilder.blockingHandler(crossinline block: suspen inline fun EventBuilder.asyncHandler( context: CoroutineContext = MinestomDispatcher, crossinline block: suspend (T) -> Unit -) = handler { CoroutineScope(context).launch { block(it) } } \ No newline at end of file +) = asyncHandler(CoroutineScope(context), block) diff --git a/src/main/kotlin/net/bladehunt/kotstom/coroutines/LaunchReporting.kt b/src/main/kotlin/net/bladehunt/kotstom/coroutines/LaunchReporting.kt new file mode 100644 index 0000000..282700a --- /dev/null +++ b/src/main/kotlin/net/bladehunt/kotstom/coroutines/LaunchReporting.kt @@ -0,0 +1,22 @@ +package net.bladehunt.kotstom.coroutines + +import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.Job +import kotlinx.coroutines.launch +import net.bladehunt.kotstom.ExceptionManager +import kotlin.coroutines.cancellation.CancellationException + +/** + * Launches [block] in this scope, reporting failures to Minestom's `ExceptionManager` instead of + * cancelling the scope. + */ +@PublishedApi +internal fun CoroutineScope.launchReporting(block: suspend CoroutineScope.() -> Unit): Job = launch { + try { + block() + } catch (e: CancellationException) { + throw e + } catch (e: Throwable) { + ExceptionManager.handleException(e) + } +} diff --git a/src/main/kotlin/net/bladehunt/kotstom/coroutines/kommand/KommandExt.kt b/src/main/kotlin/net/bladehunt/kotstom/coroutines/kommand/KommandExt.kt index bc21177..cf297d3 100644 --- a/src/main/kotlin/net/bladehunt/kotstom/coroutines/kommand/KommandExt.kt +++ b/src/main/kotlin/net/bladehunt/kotstom/coroutines/kommand/KommandExt.kt @@ -2,10 +2,10 @@ package net.bladehunt.kotstom.coroutines.kommand import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers -import kotlinx.coroutines.launch import kotlinx.coroutines.runBlocking import net.bladehunt.kotstom.command.Kommand import net.bladehunt.kotstom.coroutines.MinestomDispatcher +import net.bladehunt.kotstom.coroutines.launchReporting import net.bladehunt.kotstom.dsl.kommand.KommandDSL import net.minestom.server.command.CommandSender import net.minestom.server.command.builder.CommandContext @@ -15,21 +15,34 @@ import kotlin.coroutines.CoroutineContext internal typealias SuspendKommandExecutor = suspend context(CommandContext) (CommandSender) -> Unit /** - * Sets the async default executor of the Kommand + * Sets the async default executor of the Kommand. Cancelling [scope] cancels running executions, + * and failures are reported to the `ExceptionManager` without cancelling [scope]. * - * @param context The `CoroutineContext` to launch the coroutine in + * @param scope The `CoroutineScope` that owns the launched coroutines * @param block The suspending code to run * @author oglassdev */ inline fun Kommand.defaultExecutorAsync( - context: CoroutineContext = MinestomDispatcher, + scope: CoroutineScope, crossinline block: @KommandDSL SuspendKommandExecutor ) = CommandExecutor { sender, ctx -> - CoroutineScope(context).launch { block(ctx, sender) } + scope.launchReporting { block(ctx, sender) } } .also { defaultExecutor = it } +/** + * Sets the async default executor of the Kommand + * + * @param context The `CoroutineContext` to launch the coroutine in + * @param block The suspending code to run + * @author oglassdev + */ +inline fun Kommand.defaultExecutorAsync( + context: CoroutineContext = MinestomDispatcher, + crossinline block: @KommandDSL SuspendKommandExecutor +) = defaultExecutorAsync(CoroutineScope(context), block) + /** * Sets the blocking default executor of the Kommand * diff --git a/src/main/kotlin/net/bladehunt/kotstom/coroutines/kommand/SyntaxExt.kt b/src/main/kotlin/net/bladehunt/kotstom/coroutines/kommand/SyntaxExt.kt index 4d1f6f6..7219960 100644 --- a/src/main/kotlin/net/bladehunt/kotstom/coroutines/kommand/SyntaxExt.kt +++ b/src/main/kotlin/net/bladehunt/kotstom/coroutines/kommand/SyntaxExt.kt @@ -1,30 +1,44 @@ package net.bladehunt.kotstom.coroutines.kommand import kotlinx.coroutines.CoroutineScope -import kotlinx.coroutines.launch import kotlinx.coroutines.runBlocking import net.bladehunt.kotstom.coroutines.MinestomDispatcher +import net.bladehunt.kotstom.coroutines.launchReporting import net.bladehunt.kotstom.dsl.kommand.KommandDSL import net.bladehunt.kotstom.dsl.kommand.KommandSyntaxBuilder import net.minestom.server.command.builder.CommandExecutor import kotlin.coroutines.CoroutineContext /** - * Sets the async executor for the command + * Sets the async executor for the command. Cancelling [scope] cancels running executions, and + * failures are reported to the `ExceptionManager` without cancelling [scope]. * + * @param scope The `CoroutineScope` that owns the launched coroutines * @param block The executor logic * @return The command executor * @author oglassdev */ inline fun KommandSyntaxBuilder.executorAsync( - context: CoroutineContext = MinestomDispatcher, + scope: CoroutineScope, crossinline block: @KommandDSL SuspendKommandExecutor ) = CommandExecutor { sender, ctx -> - CoroutineScope(context).launch { block(ctx, sender) } + scope.launchReporting { block(ctx, sender) } } .also { executor = it } +/** + * Sets the async executor for the command + * + * @param block The executor logic + * @return The command executor + * @author oglassdev + */ +inline fun KommandSyntaxBuilder.executorAsync( + context: CoroutineContext = MinestomDispatcher, + crossinline block: @KommandDSL SuspendKommandExecutor +) = executorAsync(CoroutineScope(context), block) + /** * Sets the blocking executor for the command * diff --git a/src/test/kotlin/net/bladehunt/kotstom/test/CoroutineEventsTest.kt b/src/test/kotlin/net/bladehunt/kotstom/test/CoroutineEventsTest.kt new file mode 100644 index 0000000..8276f0d --- /dev/null +++ b/src/test/kotlin/net/bladehunt/kotstom/test/CoroutineEventsTest.kt @@ -0,0 +1,41 @@ +package net.bladehunt.kotstom.test + +import io.kotest.core.spec.style.FunSpec +import io.kotest.matchers.shouldBe +import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.Job +import kotlinx.coroutines.cancel +import kotlinx.coroutines.delay +import kotlinx.coroutines.isActive +import kotlinx.coroutines.job +import net.bladehunt.kotstom.coroutines.asyncHandler +import net.bladehunt.kotstom.dsl.builder +import net.minestom.server.event.Event +import net.minestom.server.event.EventNode + +private class TestEvent : Event + +class CoroutineEventsTest : + FunSpec({ + beforeAny { SERVER } + test("a failing async handler doesn't cancel its scope") { + val scope = CoroutineScope(Job() + Dispatchers.Unconfined) + val node = EventNode.all("failing") + node.builder { asyncHandler(scope) { error("boom") } } + + node.call(TestEvent()) + + scope.isActive shouldBe true + } + test("cancelling the scope cancels running async handlers") { + val scope = CoroutineScope(Job() + Dispatchers.Unconfined) + val node = EventNode.all("cancelling") + node.builder { asyncHandler(scope) { delay(60_000) } } + + node.call(TestEvent()) + scope.cancel() + + scope.coroutineContext.job.children.all { it.isCancelled } shouldBe true + } + })