Skip to content

Commit

Permalink
Ensure threaded servers are killed after test failures
Browse files Browse the repository at this point in the history
  • Loading branch information
philippjfr committed Aug 29, 2021
1 parent a5b470b commit f3c8940
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 0 deletions.
2 changes: 2 additions & 0 deletions panel/io/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -538,6 +538,8 @@ def serve(panels, port=0, address=None, websocket_origin=None, loop=None,
server = StoppableThread(
target=get_server, io_loop=loop, args=(panels,), kwargs=kwargs
)
server_id = kwargs.get('server_id', uuid.uuid4().hex)
state._threads[server_id] = server
server.start()
else:
server = get_server(panels, **kwargs)
Expand Down
7 changes: 7 additions & 0 deletions panel/io/state.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ class _state(param.Parameterized):

# An index of all currently active servers
_servers = {}
_threads = {}

# Jupyter display handles
_handles = {}
Expand Down Expand Up @@ -260,6 +261,12 @@ def kill_all_servers(self):
"""
Stop all servers and clear them from the current state.
"""
for thread in self._threads.values():
try:
thread.stop()
except Exception:
pass
self._threads = {}
for server_id in self._servers:
try:
self._servers[server_id][0].stop()
Expand Down
9 changes: 9 additions & 0 deletions panel/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,3 +163,12 @@ def set_env_var(env_var, value):
del os.environ[env_var]
else:
os.environ[env_var] = old_value


@pytest.fixture(autouse=True)
def server_cleanup():
"""
Clean up after test fails
"""
yield
state.kill_all_servers()

0 comments on commit f3c8940

Please sign in to comment.