Skip to content

Commit

Permalink
Don't kill process when embedded
Browse files Browse the repository at this point in the history
  • Loading branch information
fmagin authored and ileasile committed Mar 2, 2021
1 parent dd6d6a5 commit ea1382a
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 4 deletions.
10 changes: 9 additions & 1 deletion src/main/kotlin/org/jetbrains/kotlinx/jupyter/protocol.kt
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,15 @@ fun JupyterConnection.Socket.controlMessagesHandler(msg: Message, repl: ReplForJ
is ShutdownRequest -> {
repl?.evalOnShutdown()
send(makeReplyMessage(msg, MessageType.SHUTDOWN_REPLY, content = msg.content))
exitProcess(0)
// exitProcess would kill the entire process that embedded the kernel
// Instead the controlThread will be interrupted,
// which will then interrupt the mainThread and make kernelServer return
if (repl?.isEmbedded == true) {
log.info("Interrupting controlThread to trigger kernel shutdown")
throw InterruptedException()
} else {
exitProcess(0)
}
}
}
}
Expand Down
7 changes: 5 additions & 2 deletions src/main/kotlin/org/jetbrains/kotlinx/jupyter/repl.kt
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,9 @@ interface ReplForJupyter {
val notebook: NotebookImpl

val fileExtension: String

val isEmbedded: Boolean
get() = false
}

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

constructor(
Expand Down Expand Up @@ -262,7 +265,7 @@ class ReplForJupyterImpl(

private val evaluatorConfiguration = ScriptEvaluationConfiguration {
implicitReceivers.invoke(v = scriptReceivers)
if (!embedded) {
if (!isEmbedded) {
jvm {
val filteringClassLoader = FilteringClassLoader(ClassLoader.getSystemClassLoader()) { fqn ->
listOf(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ val testLibraryDefinition2 = LibraryDefinitionImpl(
class EmbedReplTest : AbstractReplTest() {
private val repl = run {
val embeddedClasspath: List<File> = System.getProperty("java.class.path").split(File.pathSeparator).map(::File)
ReplForJupyterImpl(resolutionInfoProvider, embeddedClasspath, embedded = true)
ReplForJupyterImpl(resolutionInfoProvider, embeddedClasspath, isEmbedded = true)
}

@Test
Expand Down

0 comments on commit ea1382a

Please sign in to comment.