fix: close bare open() in host.py and ResourceWarning in test_host_thread_cli

- host.py: 7 one-shot reads switched to with open (run_health_check
  pidfile, daemon-status pid/log, daemon-stop pid, daemon old pid,
  interrupt poll state, parallel jobs); read content, encoding args
  and error semantics unchanged.
- tests/test_host_thread_cli.py: test_old_archive_unchanged state
  read switched to with open; added a warnings-capture assertion
  scoped to the state read so a regression fails the test.
- Intentionally untouched: daemonize log handle (daemon-lifetime
  dup2 target) and the two _load_config_file / init sites owned
  by PR #266.
This commit is contained in:
host committed 2026-11-04 20:56:06 +00:00
1 parent 1462195872
commit 32e2b70f20
2 files changed
+29 -9

No files matched your search

+15 -1
View File
@@ -1,9 +1,11 @@
import gc
import io
import json
import os
import sys
import tempfile
import unittest
import warnings
from contextlib import redirect_stdout
from unittest import mock
import host
@@ -246,7 +248,19 @@ class ThreadCliTest(unittest.TestCase):
code = e.code if isinstance(e.code, int) else 1
self.assertEqual(code, 0)
self.assertIn("archived t1", out.getvalue())
saved = json.loads(Path(state_path).read_text(encoding="utf-8"))
# 第 4 项回归:此处裸 open() 曾在 GC 时报 ResourceWarning;
# 只捕获本段读 state 的警告断言(_load_config_file 的
# 警告属 #266 轨,不在此断言范围内)。
with warnings.catch_warnings(record=True) as caught:
warnings.simplefilter("always", ResourceWarning)
with open(state_path, encoding="utf-8") as f:
saved = json.load(f)
gc.collect()
self.assertEqual(
[w for w in caught
if issubclass(w.category, ResourceWarning)
and "host-state.json" in str(w.message)],
[])
self.assertEqual(saved["tasks"]["t1"]["status"], "archived")
calls = self._calls_before_close(fake)
self.assertEqual(calls, [("archive", "th-1")])