Skip to content

Commit

Permalink
Fixed race condition when lots of connections are deleted at the same…
Browse files Browse the repository at this point in the history
… time (Fixes #328)
  • Loading branch information
miguelgrinberg committed Aug 31, 2023
1 parent efc499a commit bb87ec6
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
13 changes: 10 additions & 3 deletions src/engineio/asyncio_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -509,9 +509,16 @@ async def _service_task(self): # pragma: no cover

try:
# iterate over the current clients
for socket in self.sockets.copy().values():
if not socket.closing and not socket.closed:
await socket.check_ping_timeout()
for s in self.sockets.copy().values():
if s.closed:
try:
del self.sockets[s.sid]
except KeyError:
# the socket could have also been removed by
# the _get_socket() method from another thread
pass
elif not s.closing:
await s.check_ping_timeout()
try:
await asyncio.wait_for(self.service_task_event.wait(),
timeout=sleep_interval)
Expand Down
9 changes: 8 additions & 1 deletion src/engineio/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -776,7 +776,14 @@ def _service_task(self): # pragma: no cover
try:
# iterate over the current clients
for s in self.sockets.copy().values():
if not s.closing and not s.closed:
if s.closed:
try:
del self.sockets[s.sid]
except KeyError:
# the socket could have also been removed by
# the _get_socket() method from another thread
pass
elif not s.closing:
s.check_ping_timeout()
if self.service_task_event.wait(timeout=sleep_interval):
raise KeyboardInterrupt()
Expand Down

0 comments on commit bb87ec6

Please sign in to comment.