Skip to content

Commit

Permalink
Fix a race condition in socket constructor.
Browse files Browse the repository at this point in the history
There was a race where the socket connect callbacks could get called
before the socket was added to the global cache.  Then, in the callback,
the socket would try to be retrieved from the cache, but it wouldn't be
there, resulting in an error.

This was a pretty bad race, and it affected real code. This was brought
up on #40 by GitHub user
chaoflow.  Many thanks to him for identifying the fix!
  • Loading branch information
codypiersall committed May 30, 2019
1 parent f1c5013 commit 0ea1c43
Showing 1 changed file with 5 additions and 2 deletions.
7 changes: 5 additions & 2 deletions pynng/nng.py
Original file line number Diff line number Diff line change
Expand Up @@ -300,13 +300,16 @@ def __init__(self, *,
check_err(lib.nng_pipe_notify(
self.socket, event, lib._nng_pipe_cb, as_void))

# The socket *must* be added to the _live_sockets map before calling
# listen/dial so that no callbacks are called before the socket is
# added to the map (because then the callback would fail!).
_live_sockets[id(self)] = self

if listen is not None:
self.listen(listen)
if dial is not None:
self.dial(dial, block=block_on_dial)

_live_sockets[id(self)] = self

def dial(self, address, *, block=None):
"""Dial the specified address.
Expand Down

0 comments on commit 0ea1c43

Please # to comment.