Skip to content

Commit 38709f8

Browse files
committed
fix(daemon): poll() before shutdown, dirty worker eviction, idle pool size 3->1
1 parent a35c833 commit 38709f8

1 file changed

Lines changed: 23 additions & 5 deletions

File tree

src/omnipkg/isolation/worker_daemon.py

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2857,8 +2857,14 @@ def force_shutdown(self):
28572857
with self.lock:
28582858
if self.process:
28592859
try:
2860-
self.process.stdin.write(json.dumps({"type": "shutdown"}) + "\n")
2861-
self.process.stdin.flush()
2860+
# First check if already dead (workers that ran run_cli call sys.exit)
2861+
if self.process.poll() is None:
2862+
# Still running — try graceful shutdown
2863+
try:
2864+
self.process.stdin.write(json.dumps({"type": "shutdown"}) + "\n")
2865+
self.process.stdin.flush()
2866+
except Exception:
2867+
pass
28622868
self.process.wait(timeout=2)
28632869
except Exception:
28642870
try:
@@ -2917,7 +2923,7 @@ def __init__(self, max_workers: int = 10, max_idle_time: int = 300, warmup_specs
29172923
# via a venv symlink so sys.executable resolves to the framework Python,
29182924
# causing workers to spawn without venv context and crash on omnipkg import.
29192925
_own_exe = _normalize_exe(_venv_python_exe())
2920-
self.idle_config: Dict[str, int] = {_own_exe: 3}
2926+
self.idle_config: Dict[str, int] = {_own_exe: 1}
29212927

29222928
# 🚀 AUTO-DISCOVERY: Find other managed interpreters and keep 1 idle for them
29232929
if self.cm:
@@ -4273,8 +4279,20 @@ def _execute_code(
42734279
# ═══════════════════════════════════════════════════════════════
42744280
with self.pool_lock:
42754281
if worker_key in self.workers:
4276-
self.stats["cache_hits"] += 1
4277-
worker_info = self.workers[worker_key]
4282+
# ── DIRTY WORKER EVICTION (untagged only) ──────────────────
4283+
if self.workers[worker_key].get("dirty") and not worker_tag:
4284+
_old = self.workers.pop(worker_key)
4285+
threading.Thread(
4286+
target=_old["worker"].force_shutdown, daemon=True
4287+
).start()
4288+
safe_print(
4289+
f" 🔄 [DAEMON] Evicted dirty worker on reuse: {worker_key}",
4290+
file=sys.stderr,
4291+
)
4292+
# ── END DIRTY EVICTION ──────────────────────────────────────
4293+
else:
4294+
self.stats["cache_hits"] += 1
4295+
worker_info = self.workers[worker_key]
42784296

42794297
if worker_key in self.workers:
42804298
worker_info["last_used"] = time.time()

0 commit comments

Comments
 (0)