Skip to content

Commit

Permalink
Always mark background threads as daemon.
Browse files Browse the repository at this point in the history
Fix #1455.
  • Loading branch information
aaugustin committed Apr 16, 2024
1 parent e217458 commit 5f24866
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
9 changes: 7 additions & 2 deletions src/websockets/sync/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,13 @@ def __init__(
# Mapping of ping IDs to pong waiters, in chronological order.
self.ping_waiters: Dict[bytes, threading.Event] = {}

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

# Exception raised in recv_events, to be chained to ConnectionClosed
Expand Down
3 changes: 3 additions & 0 deletions src/websockets/sync/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,9 @@ def serve_forever(self) -> None:
sock, addr = self.socket.accept()
except OSError:
break
# Since there isn't a mechanism for tracking connections and waiting
# for them to terminate, we cannot use daemon threads, or else all
# connections would be terminate brutally when closing the server.
thread = threading.Thread(target=self.handler, args=(sock, addr))
thread.start()

Expand Down

0 comments on commit 5f24866

Please sign in to comment.