Skip to content

Commit 5f24866

Browse files
committed
Always mark background threads as daemon.
Fix #1455.
1 parent e217458 commit 5f24866

File tree

2 files changed

+10
-2
lines changed

2 files changed

+10
-2
lines changed

src/websockets/sync/connection.py

+7-2
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,13 @@ def __init__(
8282
# Mapping of ping IDs to pong waiters, in chronological order.
8383
self.ping_waiters: Dict[bytes, threading.Event] = {}
8484

85-
# Receiving events from the socket.
86-
self.recv_events_thread = threading.Thread(target=self.recv_events)
85+
# Receiving events from the socket. This thread explicitly is marked as
86+
# to support creating a connection in a non-daemon thread then using it
87+
# in a daemon thread; this shouldn't block the intpreter from exiting.
88+
self.recv_events_thread = threading.Thread(
89+
target=self.recv_events,
90+
daemon=True,
91+
)
8792
self.recv_events_thread.start()
8893

8994
# Exception raised in recv_events, to be chained to ConnectionClosed

src/websockets/sync/server.py

+3
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,9 @@ def serve_forever(self) -> None:
233233
sock, addr = self.socket.accept()
234234
except OSError:
235235
break
236+
# Since there isn't a mechanism for tracking connections and waiting
237+
# for them to terminate, we cannot use daemon threads, or else all
238+
# connections would be terminate brutally when closing the server.
236239
thread = threading.Thread(target=self.handler, args=(sock, addr))
237240
thread.start()
238241

0 commit comments

Comments
 (0)