From 4b74bb925d605d606f7b4fbee4ab96d67c23142c Mon Sep 17 00:00:00 2001 From: Matthew Nelson Date: Fri, 26 Apr 2024 15:34:23 -0400 Subject: [PATCH] Call unref on child process when destroy invoked --- .../io/matthewnelson/kmp/process/testing/ProcessBaseTest.kt | 2 ++ .../io/matthewnelson/kmp/process/internal/JsChildProcess.kt | 2 ++ .../io/matthewnelson/kmp/process/internal/NodeJsProcess.kt | 4 ++++ 3 files changed, 8 insertions(+) diff --git a/library/process-testing/src/commonTest/kotlin/io/matthewnelson/kmp/process/testing/ProcessBaseTest.kt b/library/process-testing/src/commonTest/kotlin/io/matthewnelson/kmp/process/testing/ProcessBaseTest.kt index 0ac8555..209b4d6 100644 --- a/library/process-testing/src/commonTest/kotlin/io/matthewnelson/kmp/process/testing/ProcessBaseTest.kt +++ b/library/process-testing/src/commonTest/kotlin/io/matthewnelson/kmp/process/testing/ProcessBaseTest.kt @@ -441,6 +441,8 @@ abstract class ProcessBaseTest { .args("1") .args("--RunAsDaemon") .args("0") + .args("--__OwningControllerProcess") + .args(Process.Current.pid().toString()) .destroySignal(Signal.SIGTERM) .envHome() .stdin(Stdio.Null) diff --git a/library/process/src/jsMain/kotlin/io/matthewnelson/kmp/process/internal/JsChildProcess.kt b/library/process/src/jsMain/kotlin/io/matthewnelson/kmp/process/internal/JsChildProcess.kt index 6cffc25..fcd324d 100644 --- a/library/process/src/jsMain/kotlin/io/matthewnelson/kmp/process/internal/JsChildProcess.kt +++ b/library/process/src/jsMain/kotlin/io/matthewnelson/kmp/process/internal/JsChildProcess.kt @@ -55,4 +55,6 @@ internal external class child_process_ChildProcess: events_EventEmitter { internal val stdin: stream_Writable? internal val stdout: stream_Readable? internal val stderr: stream_Readable? + + internal fun unref() } diff --git a/library/process/src/jsMain/kotlin/io/matthewnelson/kmp/process/internal/NodeJsProcess.kt b/library/process/src/jsMain/kotlin/io/matthewnelson/kmp/process/internal/NodeJsProcess.kt index d2a91a1..89d7634 100644 --- a/library/process/src/jsMain/kotlin/io/matthewnelson/kmp/process/internal/NodeJsProcess.kt +++ b/library/process/src/jsMain/kotlin/io/matthewnelson/kmp/process/internal/NodeJsProcess.kt @@ -40,12 +40,16 @@ internal class NodeJsProcess internal constructor( ) { override fun destroy(): Process { + val wasDestroyed = !isDestroyed isDestroyed = true if (!jsProcess.killed && isAlive) { // TODO: check result. check error jsProcess.kill(destroySignal.name) } + + if (wasDestroyed) jsProcess.unref() + return this }