From c330caf4da33cbc9c5fd2e5807e08ca3f851d84e Mon Sep 17 00:00:00 2001 From: Yash Raj Pandey Date: Tue, 20 Oct 2026 13:18:18 +0200 Subject: [PATCH] Fix #2496: an engine program that cannot be started is a 503 with the reason, not a dropped connection (from PR #2497) --- serve/server.py | 17 ++++++++++++++--- serve/test_ready_timeout.py | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+), 3 deletions(-) diff --git a/serve/server.py b/serve/server.py index 807ea898..82a04856 100644 --- a/serve/server.py +++ b/serve/server.py @@ -1015,8 +1015,15 @@ class StrataEngine: pin["preexec_fn"] = lambda: os.sched_setaffinity(0, cpus) end_leftover_engines(exe) # one an earlier run lost still holds the GPU and the memory spawned_at = time.monotonic() - self.proc = popen("the Strata engine", [exe, "--serve", *args], cwd=cwd, stdin=subprocess.PIPE, - stdout=subprocess.PIPE, stderr=self.log, text=True, encoding="utf-8", bufsize=1, env=env, **pin) + try: + self.proc = popen("the Strata engine", [exe, "--serve", *args], cwd=cwd, stdin=subprocess.PIPE, + stdout=subprocess.PIPE, stderr=self.log, text=True, encoding="utf-8", bufsize=1, env=env, + **pin) + except OSError: # nothing started: the narrator stops, the log handle goes back + loading.set() + if log: + self.log.close() + raise # the READY read has a timeout (#1317): the lines come from a thread, up to READY, so the rest of the stream # stays for _pump (one reader at a time) ready_q: queue.Queue = queue.Queue() @@ -3963,9 +3970,13 @@ class Service: "(a minute or two) ...", flush=True) try: self.engine.restart() - except Exception: + except Exception as e: if vision_started: self.vision.unload() + # the program could not be started at all (missing, not executable, or blocked by Windows - #735): the + # request path answers EngineDied with a 503, but takes a bare OSError for a client that went away (#2496) + if isinstance(e, OSError): + raise EngineDied(f"the engine could not be started: {e}") from e raise print("[strata] the engine is running again", flush=True) if self.restore_after_load: # an image job unloaded it: its conversation comes back diff --git a/serve/test_ready_timeout.py b/serve/test_ready_timeout.py index 78ef33d0..009d9ecc 100644 --- a/serve/test_ready_timeout.py +++ b/serve/test_ready_timeout.py @@ -104,5 +104,40 @@ class ReadyTimeout(unittest.TestCase): self.assertFalse(svc.engine.starting) +class SpawnFailure(unittest.TestCase): + """The engine program cannot be started at all (missing, not executable, or blocked by Windows - #735): the + request that was loading it gets a 503 with the reason, not a dropped connection.""" + BODIES = {'/v1/chat/completions': {'messages': [{'role': 'user', 'content': 'hi'}], 'max_tokens': 8}, + '/v1/messages': {'model': 'strata', 'max_tokens': 8, 'messages': [{'role': 'user', 'content': 'hi'}]}, + '/v1/responses': {'input': 'hi', 'max_output_tokens': 8}} + + def test_missing_engine_answers_503_and_leaves_nothing_running(self): + tmp = tempfile.TemporaryDirectory(); self.addCleanup(tmp.cleanup) + root = Path(tmp.name) + eng = StrataEngine(str(root / 'no-such-engine'), [], log=str(root / 'engine.log'), lazy=True) + svc = Service(eng, ByteTokenizer(), ChatTemplate(Path(__file__).parent / 'chat_template.jinja')) + httpd = serve(svc, port=0) + self.addCleanup(httpd.shutdown); self.addCleanup(httpd.server_close) + base = f'http://127.0.0.1:{httpd.server_address[1]}' + before = set(threading.enumerate()) + cases = list(self.BODIES.items()) + [('/v1/chat/completions', + {**self.BODIES['/v1/chat/completions'], 'stream': True})] + for path, body in cases: + with self.subTest(path=path, stream=body.get('stream', False)): + req = urllib.request.Request(base + path, data=json.dumps(body).encode(), + headers={'Content-Type': 'application/json'}) + with self.assertRaises(urllib.error.HTTPError) as cm: + urllib.request.urlopen(req, timeout=30) + self.assertEqual(cm.exception.code, 503) + self.assertIn('the engine could not be started', cm.exception.read().decode()) + self.assertTrue(eng.log.closed) # our handle on the engine log is given back + # the start narrator ("still starting ... please wait") ends with the failed start + deadline = time.monotonic() + 5 + while (narrators := [t for t in set(threading.enumerate()) - before if '(narrate_start)' in t.name]) \ + and time.monotonic() < deadline: + time.sleep(0.1) + self.assertEqual(narrators, []) + + if __name__ == '__main__': unittest.main()