Fix #2496: an engine program that cannot be started is a 503 with the reason, not a dropped connection (from PR #2497)

This commit is contained in:
Yash Raj Pandey authored and Niko1221 committed 2026-10-20 14:13:34 +02:00
1 parent 7adced3db5
commit c330caf4da
2 files changed
+49 -3

No files matched your search

+14 -3
View File
@@ -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
+35
View File
@@ -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()