Skip to content

Commit d64c2d0

Browse files
committed
Fix leaky sockets in NetworkingThread
1 parent 3e56a4e commit d64c2d0

File tree

2 files changed

+16
-4
lines changed

2 files changed

+16
-4
lines changed

CHANGES.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ Changelog
44
2.0.1 (unreleased)
55
-------------------
66

7-
- nothing yet
7+
- Fix socket leaks in `NetworkingThread`
88

99
2.0.0 (2020-04-16)
1010
-------------------

wsdiscovery/threaded.py

+15-3
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import threading
1010
import selectors
1111
import platform
12+
from typing import cast
1213

1314
from .udp import UDPMessage
1415
from .actions import *
@@ -156,10 +157,14 @@ def run(self):
156157

157158
def _recvMessages(self):
158159
for key, events in self._selector.select(0):
159-
sock = socket.fromfd(key.fd, socket.AF_INET, socket.SOCK_DGRAM)
160+
if self._quitEvent.is_set():
161+
break
162+
163+
sock = cast(socket.socket, key.fileobj)
164+
160165
try:
161166
data, addr = sock.recvfrom(BUFFER_SIZE)
162-
except socket.error as e:
167+
except socket.error:
163168
time.sleep(0.01)
164169
continue
165170

@@ -253,12 +258,19 @@ def start(self):
253258
self._multiOutUniInSockets = {} # FIXME synchronisation
254259

255260
def join(self):
261+
assert self._quitEvent.is_set()
256262
super(NetworkingThread, self).join()
257-
self._uniOutSocket.close()
258263

259264
self._selector.unregister(self._multiInSocket)
265+
self._uniOutSocket.close()
260266
self._multiInSocket.close()
261267

268+
for sock in self._multiOutUniInSockets.values():
269+
try:
270+
sock.close()
271+
except socket.error:
272+
...
273+
262274

263275
class ThreadedNetworking:
264276
"handle threaded networking start & stop, address add/remove & message sending"

0 commit comments

Comments
 (0)