Correclty cleanup packet capture objects when closing server

Fix #592
This commit is contained in:
Julien Duponchelle committed 2016-08-19 11:05:54 +02:00
1 parent 8eab94f01e
commit f0fad5289c
5 files changed
+30 -13

No files matched your search

+14 -1
View File
@@ -295,11 +295,24 @@ class Compute:
:returns: A file stream
"""
# Due to Python 3.4 limitation we can't use with and asyncio
# https://www.python.org/dev/peps/pep-0492/
# that why we wrap the answer
class StreamResponse:
def __init__(self, response):
self._response = response
def __enter__(self):
return self._response.content
def __exit__(self):
self._response.close()
url = self._getUrl("/projects/{}/stream/{}".format(project.id, path))
response = yield from self._session().request("GET", url, auth=self._auth)
if response.status == 404:
raise aiohttp.web.HTTPNotFound(text="{} not found on compute".format(path))
return response.content
return StreamResponse(response)
@asyncio.coroutine
def http_query(self, method, path, data=None, **kwargs):
+12 -11
View File
@@ -116,17 +116,18 @@ class Link:
Dump a pcap file on disk
"""
stream = yield from self.read_pcap_from_source()
with open(self.capture_file_path, "wb+") as f:
while self._capturing:
# We read 1 bytes by 1 otherwise the remaining data is not read if the traffic stops
data = yield from stream.read(1)
if data:
f.write(data)
# Flush to disk otherwise the live is not really live
f.flush()
else:
break
stream_content = yield from self.read_pcap_from_source()
with stream_content as stream:
with open(self.capture_file_path, "wb+") as f:
while self._capturing:
# We read 1 bytes by 1 otherwise the remaining data is not read if the traffic stops
data = yield from stream.read(1)
if data:
f.write(data)
# Flush to disk otherwise the live is not really live
f.flush()
else:
break
@asyncio.coroutine
def stop_capture(self):
+1 -1
View File
@@ -120,7 +120,7 @@ class UDPLink(Link):
:returns: Node where the capture should run
"""
# use the local node first to save bandwidth
# use the local node first to save bandwidth
for node in self._nodes:
if node["node"].compute.id == "local" and node["node"].node_type not in [""]: # FIXME
return node
+1
View File
@@ -127,3 +127,4 @@ def wait_for_named_pipe_creation(pipe_path, timeout=60):
else:
return
raise asyncio.TimeoutError()