Skip to content

Commit

Permalink
core: wake Waker outside of lock.
Browse files Browse the repository at this point in the history
Given:

- Broker asleep in poll()
- thread B calling Latch.put()

Previously,

- B takes lock,
- B wakes socket by dropping GIL and writing to it
- Broker wakes from poll(), acquires GIL only to find Latch._lock is held
- Broker drops GIL, sleeps on futex() for _lock
- B wakes, acquires GIL, releases _lock
- Broker wakes from futex(), acquires lock

Now,

- B takes lock, updates state, releases lock
- B wakes socket by droppping GIL and writing to it
- Broker wakes from poll(), acquires GIL and _lock
- Everyone lives happily ever after.
  • Loading branch information
dw committed Jul 29, 2019
1 parent 807cbef commit d6faff0
Showing 1 changed file with 4 additions and 2 deletions.
6 changes: 4 additions & 2 deletions mitogen/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -2613,12 +2613,14 @@ def defer(self, func, *args, **kwargs):
self.stream.transmit_side.fd)
self._lock.acquire()
try:
if not self._deferred:
self._wake()
should_wake = not self._deferred
self._deferred.append((func, args, kwargs))
finally:
self._lock.release()

if should_wake:
self._wake()


class IoLoggerProtocol(DelimitedProtocol):
"""
Expand Down

0 comments on commit d6faff0

Please sign in to comment.