Skip to content

Commit 57ba019

Browse files
fmaginileasile
authored andcommitted
Don't kill process when embedded
1 parent dd6d6a5 commit 57ba019

File tree

3 files changed

+15
-4
lines changed

3 files changed

+15
-4
lines changed

src/main/kotlin/org/jetbrains/kotlinx/jupyter/protocol.kt

+9-1
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,15 @@ fun JupyterConnection.Socket.controlMessagesHandler(msg: Message, repl: ReplForJ
195195
is ShutdownRequest -> {
196196
repl?.evalOnShutdown()
197197
send(makeReplyMessage(msg, MessageType.SHUTDOWN_REPLY, content = msg.content))
198-
exitProcess(0)
198+
// exitProcess would kill the entire process that embedded the kernel
199+
// Instead the controlThread will be interrupted,
200+
// which will then interrupt the mainThread and make kernelServer return
201+
if (repl?.isEmbedded == true) {
202+
log.info("Interrupting controlThread to trigger kernel shutdown")
203+
throw InterruptedException()
204+
} else {
205+
exitProcess(0)
206+
}
199207
}
200208
}
201209
}

src/main/kotlin/org/jetbrains/kotlinx/jupyter/repl.kt

+5-2
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,9 @@ interface ReplForJupyter {
132132
val notebook: NotebookImpl
133133

134134
val fileExtension: String
135+
136+
val isEmbedded: Boolean
137+
get() = false
135138
}
136139

137140
fun <T> ReplForJupyter.execute(callback: ExecutionCallback<T>): T {
@@ -145,7 +148,7 @@ class ReplForJupyterImpl(
145148
override val resolverConfig: ResolverConfig? = null,
146149
override val runtimeProperties: ReplRuntimeProperties = defaultRuntimeProperties,
147150
private val scriptReceivers: List<Any> = emptyList(),
148-
private val embedded: Boolean = false,
151+
override val isEmbedded: Boolean = false,
149152
) : ReplForJupyter, ReplOptions, BaseKernelHost, KotlinKernelHostProvider {
150153

151154
constructor(
@@ -262,7 +265,7 @@ class ReplForJupyterImpl(
262265

263266
private val evaluatorConfiguration = ScriptEvaluationConfiguration {
264267
implicitReceivers.invoke(v = scriptReceivers)
265-
if (!embedded) {
268+
if (!isEmbedded) {
266269
jvm {
267270
val filteringClassLoader = FilteringClassLoader(ClassLoader.getSystemClassLoader()) { fqn ->
268271
listOf(

src/test/kotlin/org/jetbrains/kotlinx/jupyter/test/embeddingTest.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ val testLibraryDefinition2 = LibraryDefinitionImpl(
9090
class EmbedReplTest : AbstractReplTest() {
9191
private val repl = run {
9292
val embeddedClasspath: List<File> = System.getProperty("java.class.path").split(File.pathSeparator).map(::File)
93-
ReplForJupyterImpl(resolutionInfoProvider, embeddedClasspath, embedded = true)
93+
ReplForJupyterImpl(resolutionInfoProvider, embeddedClasspath, isEmbedded = true)
9494
}
9595

9696
@Test

0 commit comments

Comments
 (0)