Skip to content
This repository was archived by the owner on Jun 22, 2025. It is now read-only.

Commit 9ea4024

Browse files
Shut down the eel server less aggressively
At the moment, every time a websocket connection closes, we check 1 second later to see if there any any connections open. If a user opens and terminates multiple websocket connections, this can mean that one of the earlier "1 second laters" comes along during a later shutdown/reconnect and closes the server, despite the user navigating through the app in a reasonable way. This patch moves this check to a "spawn later" gevent greenlet that can be terminated and rescheduled. Every time a websocket is closed, we cancel any previously-running check and schedule a new one for 1 second in the future, which should mean that Eel shuts down less often during valid navigation step/s. Fixes #248
1 parent 0684eb5 commit 9ea4024

File tree

1 file changed

+13
-4
lines changed

1 file changed

+13
-4
lines changed

eel/__init__.py

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from builtins import range
22
from io import open
33

4+
from gevent.threading import Timer
45
import gevent as gvt
56
import json as jsn
67
import bottle as btl
@@ -25,6 +26,7 @@
2526
_js_functions = []
2627
_mock_queue = []
2728
_mock_queue_done = set()
29+
_shutdown = None
2830

2931
# The maximum time (in milliseconds) that Python will try to retrieve a return value for functions executing in JS
3032
# Can be overridden through `eel.init` with the kwarg `js_result_timeout` (default: 10000)
@@ -326,17 +328,24 @@ def _expose(name, function):
326328
_exposed_functions[name] = function
327329

328330

331+
def _detect_shutdown():
332+
if len(_websockets) == 0:
333+
sys.exit()
334+
335+
329336
def _websocket_close(page):
337+
global _shutdown
338+
330339
close_callback = _start_args.get('close_callback')
331340

332341
if close_callback is not None:
333342
sockets = [p for _, p in _websockets]
334343
close_callback(page, sockets)
335344
else:
336-
# Default behaviour - wait 1s, then quit if all sockets are closed
337-
sleep(1.0)
338-
if len(_websockets) == 0:
339-
sys.exit()
345+
if _shutdown:
346+
_shutdown.kill()
347+
348+
_shutdown = gvt.spawn_later(1.0, _detect_shutdown)
340349

341350

342351
def _set_response_headers(response):

0 commit comments

Comments
 (0)