Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't kill process when embedded #135

Merged
merged 1 commit into from
Mar 2, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
@@ -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)
}
}
}
}
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
@@ -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 {
@@ -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(
@@ -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(
Original file line number Diff line number Diff line change
@@ -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