diff --git a/osbuild/sources.py b/osbuild/sources.py index 9e9cd574..78c72858 100644 --- a/osbuild/sources.py +++ b/osbuild/sources.py @@ -14,9 +14,9 @@ class SourcesServer: self.output = output self.options = options or {} self.secrets = secrets or {} - self.event_loop = asyncio.new_event_loop() - self.thread = threading.Thread(target=self._run_event_loop) self.barrier = threading.Barrier(2) + self.event_loop = None + self.thread = None def _run_source(self, source, checksums): msg = { @@ -53,13 +53,25 @@ class SourcesServer: self.event_loop.remove_reader(server) def __enter__(self): + # We are not re-entrant, so complain if re-entered. + assert self.event_loop is None + + self.event_loop = asyncio.new_event_loop() + self.thread = threading.Thread(target=self._run_event_loop) + + self.barrier.reset() self.thread.start() self.barrier.wait() + return self def __exit__(self, *args): self.event_loop.call_soon_threadsafe(self.event_loop.stop) self.thread.join() + self.event_loop.close() + + self.thread = None + self.event_loop = None def get(source, checksums, api_path="/run/osbuild/api/sources"):