Skip to content

Commit

Permalink
Merge pull request #12710 from mvdbeek/join_uvicorn_embedded_thread
Browse files Browse the repository at this point in the history
Join thread that hosts asyncio loop for embedded uvicorn
  • Loading branch information
mvdbeek authored Oct 14, 2021
2 parents 4d65f2a + 2d8fb88 commit eb17bb5
Showing 1 changed file with 10 additions and 4 deletions.
14 changes: 10 additions & 4 deletions lib/galaxy_test/driver/driver_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -583,7 +583,7 @@ def run_in_loop(loop):
t = threading.Thread(target=run_in_loop, args=(loop,))
t.start()

return server, port
return server, port, t


def cleanup_directory(tempdir):
Expand Down Expand Up @@ -725,10 +725,11 @@ def stop(self):

class EmbeddedServerWrapper(ServerWrapper):

def __init__(self, app, server, name, host, port):
def __init__(self, app, server, name, host, port, thread=None):
super().__init__(name, host, port)
self._app = app
self._server = server
self._thread = thread

@property
def app(self):
Expand All @@ -745,6 +746,11 @@ def stop(self):
self._server.should_exit = True
log.info(f"Embedded web server {self.name} stopped")

if self._thread is not None:
log.info("Stopping embedded server thread")
self._thread.join()
log.info("Embedded server thread stopped")

if self._app is not None:
log.info(f"Stopping application {self.name}")
self._app.shutdown()
Expand Down Expand Up @@ -873,11 +879,11 @@ def launch_uvicorn(webapp_factory, prefix=DEFAULT_CONFIG_PREFIX, galaxy_config=N
)
from galaxy.webapps.galaxy.fast_app import initialize_fast_app
app = initialize_fast_app(gx_wsgi_webapp, gx_app)
server, port = uvicorn_serve(app, host=host, port=port)
server, port, thread = uvicorn_serve(app, host=host, port=port)
set_and_wait_for_http_target(prefix, host, port)
log.info(f"Embedded uvicorn web server for {name} started at {host}:{port}")
return EmbeddedServerWrapper(
gx_app, server, name, host, port
gx_app, server, name, host, port, thread=thread
)


Expand Down

0 comments on commit eb17bb5

Please sign in to comment.