Skip to content

Commit

Permalink
Merge pull request #13 from SpiNNakerManchester/thread-local-bug
Browse files Browse the repository at this point in the history
Resolve intermittent threading bug.
  • Loading branch information
rowleya committed Sep 12, 2017
2 parents ea9b29a + fc3be38 commit a5f5807
Showing 1 changed file with 12 additions and 4 deletions.
16 changes: 12 additions & 4 deletions spalloc/protocol_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,16 @@ class SpallocServerException(Exception):
"""


class _ProtocolThreadLocal(local):
"""Subclass of threading.local to ensure that we get sane initialisation\
of our state in each thread.
"""
# See https://github.com/SpiNNakerManchester/spalloc/issues/12
def __init__(self):
self.buffer = b""
self.sock = None


class ProtocolClient(object):
"""A simple (blocking) client implementation of the `spalloc-server
<https://github.com/project-rig/spalloc_server>`_ protocol.
Expand Down Expand Up @@ -69,7 +79,7 @@ def __init__(self, hostname, port=22244, timeout=None):
# shut down all sockets at once.
self._socks = dict()
# Thread local variables
self._local = local()
self._local = _ProtocolThreadLocal()
# A queue of unprocessed notifications
self._notifications = deque()
self._dead = True
Expand Down Expand Up @@ -128,8 +138,6 @@ def _do_connect(self, sock):
return success

def _has_open_socket(self):
if "sock" not in self._local.__dict__:
return False
return self._local.sock is not None

def connect(self, timeout=None):
Expand Down Expand Up @@ -176,7 +184,7 @@ def close(self):
keys = list(self._socks.keys())
for key in keys:
self._close(key)
self._local = local()
self._local = _ProtocolThreadLocal()

def _recv_json(self, timeout=None):
"""Receive a line of JSON from the server.
Expand Down

0 comments on commit a5f5807

Please sign in to comment.