Skip to content

armgit / Cairn

Public Imported from GitHub
0

Cairn is a general-purpose AI state-space search engine, validated first on autonomous penetration testing. Sometimes misspelled as Carin.

feat: add stream closing logic in ManagedProcess to prevent resource leaks

leixiao2fca66b096c6
diff --git a/cairn/src/cairn/dispatcher/runtime/process.py b/cairn/src/cairn/dispatcher/runtime/process.py
index cc37e7c..b3af0d7 100644
--- a/cairn/src/cairn/dispatcher/runtime/process.py
+++ b/cairn/src/cairn/dispatcher/runtime/process.py
@@ -1,5 +1,6 @@
from __future__ import annotations
+from contextlib import suppress
from dataclasses import dataclass
import logging
import threading
@@ -99,6 +100,7 @@ class ManagedProcess:
def _read_stream(self) -> None:
assert self._exec_id is not None
+ stream: Any | None = None
try:
stream = self._api.exec_start(
self._exec_id,
@@ -116,9 +118,24 @@ class ManagedProcess:
except DockerException as exc:
self._read_error = str(exc)
finally:
+ self._close_stream(stream)
self._returncode = self._resolve_exit_code()
self._done.set()
+ @staticmethod
+ def _close_stream(stream: Any | None) -> None:
+ if stream is None:
+ return
+ close = getattr(stream, "close", None)
+ if callable(close):
+ with suppress(Exception):
+ close()
+ response = getattr(stream, "_response", None)
+ response_close = getattr(response, "close", None)
+ if callable(response_close):
+ with suppress(Exception):
+ response_close()
+
def _resolve_exit_code(self) -> int:
assert self._exec_id is not None
deadline = time.monotonic() + EXEC_KILL_JOIN_TIMEOUT_SECONDS