Skip to content

Commit 1846d68

Browse files
committed
Delay adding socket to asyncore map
- prevents race conditions - also remove unneeded code from asyncore
1 parent 5a85515 commit 1846d68

2 files changed

Lines changed: 19 additions & 16 deletions

File tree

‎src/network/asyncore_pollchoose.py‎

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -509,7 +509,7 @@ def __init__(self, sock=None, map=None):
509509
# Set to nonblocking just to make sure for cases where we
510510
# get a socket from a blocking source.
511511
sock.setblocking(0)
512-
self.set_socket(sock, map)
512+
self.set_socket(sock)
513513
self.connected = True
514514
# The constructor no longer requires that the socket
515515
# passed be connected.
@@ -524,7 +524,7 @@ def __init__(self, sock=None, map=None):
524524
# The socket is broken in some unknown way, alert
525525
# the user and remove it from the map (to prevent
526526
# polling of broken sockets).
527-
self.del_channel(map)
527+
self.del_channel()
528528
raise
529529
else:
530530
self.socket = None
@@ -544,22 +544,18 @@ def __repr__(self):
544544

545545
__str__ = __repr__
546546

547-
def add_channel(self, map=None):
548-
"""Add a channel"""
547+
def add_channel(self):
548+
"""Add a channel to the asyncore map set during __init__."""
549549
# pylint: disable=attribute-defined-outside-init
550-
if map is None:
551-
map = self._map
552-
map[self._fileno] = self
550+
self._map[self._fileno] = self
553551
self.poller_flags = 0
554552
self.poller_filter = 0
555553

556-
def del_channel(self, map=None):
557-
"""Delete a channel"""
554+
def del_channel(self):
555+
"""Delete a channel from the asyncore map set during __init__."""
558556
fd = self._fileno
559-
if map is None:
560-
map = self._map
561-
if fd in map:
562-
del map[fd]
557+
if fd in self._map:
558+
del self._map[fd]
563559
if self._fileno:
564560
try:
565561
kqueue_poller.pollster.control([select.kevent(
@@ -595,11 +591,15 @@ def create_socket(
595591
sock.setblocking(0)
596592
self.set_socket(sock)
597593

598-
def set_socket(self, sock, map=None):
599-
"""Set socket"""
594+
def set_socket(self, sock):
595+
"""Set socket without registering in the asyncore map.
596+
597+
Registration is deferred to an explicit add_channel() call,
598+
typically done by BMConnectionPool.addConnection() after the
599+
connection is fully initialised with a pool reference.
600+
"""
600601
self.socket = sock
601602
self._fileno = sock.fileno()
602-
self.add_channel(map)
603603

604604
def set_reuse_addr(self):
605605
"""try to re-use a server port if possible"""

‎src/network/connectionpool.py‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ def addConnection(self, connection):
121121
"""Add a connection object to our internal dict"""
122122
from .udp import UDPSocket
123123
connection.pool = self
124+
connection.add_channel()
124125
if isinstance(connection, UDPSocket):
125126
return
126127
if connection.isOutbound:
@@ -184,6 +185,7 @@ def startListening(self, bind=None):
184185
# correct port even if it changed
185186
ls = TCPServer(host=bind, port=port)
186187
ls.pool = self
188+
ls.add_channel()
187189
self.listeningSockets[ls.destination] = ls
188190

189191
def startUDPSocket(self, bind=None):
@@ -201,6 +203,7 @@ def startUDPSocket(self, bind=None):
201203
else:
202204
udpSocket = UDPSocket(host=bind, announcing=True)
203205
udpSocket.pool = self
206+
udpSocket.add_channel()
204207
self.udpSockets[udpSocket.listening.host] = udpSocket
205208

206209
def startBootstrappers(self):

0 commit comments

Comments
 (0)