Skip to content

Commit

Permalink
fix: Python embedded server should shut down any jvm threads/work it …
Browse files Browse the repository at this point in the history
  • Loading branch information
niloc132 authored Sep 4, 2024
1 parent 992fc48 commit a457ff9
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 2 deletions.
4 changes: 2 additions & 2 deletions py/embedded-server/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ dependencies {
}
}

def testPyClient = Docker.registerDockerTask(project, 'testPyClient') {
def testEmbeddedServer = Docker.registerDockerTask(project, 'testEmbeddedServer') {
copyIn {
from('tests') {
into 'project/tests'
Expand Down Expand Up @@ -62,4 +62,4 @@ def testPyClient = Docker.registerDockerTask(project, 'testPyClient') {
}
}

tasks.getByName('check').dependsOn(testPyClient)
tasks.getByName('check').dependsOn(testEmbeddedServer)
4 changes: 4 additions & 0 deletions py/embedded-server/deephaven_server/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

from typing import List, Optional
import sys
import atexit

from .start_jvm import start_jvm

Expand Down Expand Up @@ -167,6 +168,9 @@ def __init__(
# Keep a reference to the server so we know it is running
Server.instance = self

# On halt, prevent the JVM from writing to sys.out and sys.err
atexit.register(self.j_server.prepareForShutdown)

def start(self):
"""
Starts the server. Presently once the server is started, it cannot be stopped until the
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import io.deephaven.server.session.ObfuscatingErrorTransformerModule;
import io.deephaven.server.session.SslConfigModule;
import io.deephaven.time.calendar.CalendarsFromConfigurationModule;
import io.deephaven.util.process.ProcessEnvironment;
import org.jpy.PyModule;
import org.jpy.PyObject;

Expand Down Expand Up @@ -167,6 +168,15 @@ public void start() throws Exception {
Bootstrap.printf("Server started on port %d%n", getPort());
}

public void prepareForShutdown() {
// Stop any DH-started threads and work
ProcessEnvironment.getGlobalShutdownManager().maybeInvokeTasks();

// Shut down our wrapped stdout/stderr instances
System.out.close();
System.err.close();
}

public int getPort() {
return server.server().getPort();
}
Expand Down

0 comments on commit a457ff9

Please sign in to comment.