Lock the dynamips reader an writer

Fix #103
This commit is contained in:
Julien Duponchelle 2015-03-20 10:21:27 +01:00
parent 7473dec5ad
commit 01bcbe2fd9

View File

@ -59,6 +59,8 @@ class DynamipsHypervisor:
self._reader = None self._reader = None
self._writer = None self._writer = None
self._io_lock = asyncio.Lock()
@asyncio.coroutine @asyncio.coroutine
def connect(self, timeout=10): def connect(self, timeout=10):
""" """
@ -80,6 +82,7 @@ class DynamipsHypervisor:
while time.time() - begin < timeout: while time.time() - begin < timeout:
yield from asyncio.sleep(0.01) yield from asyncio.sleep(0.01)
try: try:
with (yield from self._io_lock):
self._reader, self._writer = yield from asyncio.open_connection(host, self._port) self._reader, self._writer = yield from asyncio.open_connection(host, self._port)
except OSError as e: except OSError as e:
last_exception = e last_exception = e
@ -120,6 +123,7 @@ class DynamipsHypervisor:
""" """
yield from self.send("hypervisor close") yield from self.send("hypervisor close")
with (yield from self._io_lock):
self._writer.close() self._writer.close()
self._reader, self._writer = None self._reader, self._writer = None
@ -129,6 +133,7 @@ class DynamipsHypervisor:
Stops this hypervisor (will no longer run). Stops this hypervisor (will no longer run).
""" """
with (yield from self._io_lock):
try: try:
# try to properly stop the hypervisor # try to properly stop the hypervisor
yield from self.send("hypervisor stop") yield from self.send("hypervisor stop")
@ -255,6 +260,7 @@ class DynamipsHypervisor:
# but still have more data. The only thing we know for sure is the last line # but still have more data. The only thing we know for sure is the last line
# will begin with '100-' or a '2xx-' and end with '\r\n' # will begin with '100-' or a '2xx-' and end with '\r\n'
with (yield from self._io_lock):
if self._writer is None or self._reader is None: if self._writer is None or self._reader is None:
raise DynamipsError("Not connected") raise DynamipsError("Not connected")