core: fix _tls_init() race.

The GIL could be lost between the check for an empty list and popping a
socket off the list. Previously _tls_init (per its name) used per-thread
storage, hence the bug.
pull/234/head
David Wilson 8 years ago
parent 79fd35092b
commit 7316c08237

@ -1008,12 +1008,14 @@ class Latch(object):
return len(self._queue) == 0
def _tls_init(self):
if self._sockets:
# pop() must be atomic, which is true for GIL-equipped interpreters.
try:
return self._sockets.pop()
rsock, wsock = socket.socketpair()
set_cloexec(rsock.fileno())
set_cloexec(wsock.fileno())
return rsock, wsock
except IndexError:
rsock, wsock = socket.socketpair()
set_cloexec(rsock.fileno())
set_cloexec(wsock.fileno())
return rsock, wsock
def get(self, timeout=None, block=True):
_vv and IOLOG.debug('%r.get(timeout=%r, block=%r)',

@ -798,7 +798,6 @@ class Context(mitogen.core.Context):
LOG.debug('%r.shutdown() sending SHUTDOWN', self)
latch = mitogen.core.Latch()
mitogen.core.listen(self, 'disconnect', lambda: latch.put(None))
self.send(
mitogen.core.Message(
handle=mitogen.core.SHUTDOWN,
@ -807,6 +806,7 @@ class Context(mitogen.core.Context):
if wait:
latch.get()
return latch
class RouteMonitor(object):

Loading…
Cancel
Save