Skip to content

Qwen3.8-Flash-Next on any consumer hardware: one-click install for Windows / Linux. Strata inference engine, OpenAI/Anthropic API on localhost, optional image input.

C++

serve: answer the request that hit the READY timeout (#1527)

nekomario2832a2439de059
diff --git a/serve/server.py b/serve/server.py
index 553f440c8..3335669ea 100644
--- a/serve/server.py
+++ b/serve/server.py
@@ -690,8 +690,10 @@ class StrataEngine:
pass
why = (f"the engine did not report READY within {ENGINE_READY_S:.0f} s" if timed_out else
"the engine exited before it was ready")
- raise RuntimeError(why + (f" (see {log})" if log else "") +
- start_failure_hint(log, log_start) + start_log_tail(log, log_start))
+ # EngineDied, not bare RuntimeError: a request waiting on this load must end with a 503
+ # through the request path's handler - a RuntimeError there left the turn with no answer (#1527)
+ raise EngineDied(why + (f" (see {log})" if log else "") +
+ start_failure_hint(log, log_start) + start_log_tail(log, log_start))
self.known_ctx = self.max_context # survives a failed restart: requests keep their limit and restart it
# (from PR #41, midhatn) a locally built engine can sit next to another release's BUILD.json: engines that
# report their own version (INFO engine=, 0.1.8+) win, the manifest stays the fallback for older ones
@@ -871,8 +873,10 @@ class StrataEngine:
time.sleep(self.RESTART_RETRY_S)
finally:
self.starting = False
- with self.slot_cv: # #1012: wake the requests that waited through it: they go on with the
- self.slot_cv.notify_all() # new engine, or (it did not start) end with a clean EngineDied
+ # a lazy first start never reached the post-READY __init__ where slot_cv is made (#1527)
+ if "slot_cv" in self.__dict__:
+ with self.slot_cv: # #1012: wake the requests that waited through it: they go on with the
+ self.slot_cv.notify_all() # new engine, or (it did not start) end with a clean EngineDied
self.info = {**info, **self.info}
def _parse_done(self, line):
diff --git a/serve/test_ready_timeout.py b/serve/test_ready_timeout.py
new file mode 100644
index 000000000..f469c9c94
--- /dev/null
+++ b/serve/test_ready_timeout.py
@@ -0,0 +1,106 @@
+"""#1527: the READY-read timeout (#1317) ends the engine, and the request that was loading it must end too -
+a 503 with the reason, not a connection that never answers (or a misleading 400)."""
+import os
+from pathlib import Path
+import sys
+import tempfile
+import threading
+import time
+import unittest
+import json
+import urllib.request
+from unittest import mock
+sys.path.insert(0, str(Path(__file__).resolve().parents[1]))
+import serve.server as server # noqa: E402
+from serve.server import StrataEngine, Service, ChatTemplate, ByteTokenizer, serve # noqa: E402
+
+# never prints READY: the READY read times out and the process is killed
+SILENT = r'''
+import time
+time.sleep(3600)
+'''
+
+# first run answers READY + one request; after --hang is touched a restart stays silent forever
+HANG_ON_RESTART = r'''
+import sys, time
+from pathlib import Path
+hang = Path(sys.argv[sys.argv.index('--hang') + 1])
+if hang.exists():
+ time.sleep(3600)
+print('READY 4096 stop', flush=True)
+for line in sys.stdin:
+ if line.startswith('QUIT'):
+ break
+ if line.startswith(('GEN', 'BGEN')):
+ print('T 90\nDONE 1 1 0 1 stop', flush=True)
+'''
+
+
+class ReadyTimeout(unittest.TestCase):
+ def setUp(self):
+ self.tmp = tempfile.TemporaryDirectory(); self.addCleanup(self.tmp.cleanup)
+ self.root = Path(self.tmp.name)
+ self._patchers = [mock.patch.object(server, 'ENGINE_READY_S', 0.5),
+ mock.patch.object(StrataEngine, 'RESTART_RETRY_S', 0.0)]
+ for p in self._patchers:
+ p.start(); self.addCleanup(p.stop)
+
+ def make_script(self, name, code):
+ py = self.root / (name + '.py')
+ if os.name == 'nt':
+ py.write_text(code); script = self.root / (name + '.cmd')
+ script.write_text(f'@"{sys.executable}" "{py}" %*\r\n')
+ else:
+ script = py; py.write_text('#!' + sys.executable + '\n' + code); py.chmod(0o700)
+ return str(script)
+
+ def service(self, script, extra_args=()):
+ eng = StrataEngine(script, list(extra_args), lazy=True)
+ self.addCleanup(eng.close)
+ return Service(eng, ByteTokenizer(), ChatTemplate(Path(__file__).parent / 'chat_template.jinja'))
+
+ def post(self, base):
+ body = {'messages': [{'role': 'user', 'content': 'hi'}], 'max_tokens': 8}
+ req = urllib.request.Request(base + '/v1/chat/completions', data=json.dumps(body).encode(),
+ headers={'Content-Type': 'application/json'})
+ with urllib.request.urlopen(req, timeout=30) as r:
+ return r.status, r.read()
+
+ def assert503(self, base):
+ t0 = time.monotonic()
+ try:
+ self.post(base)
+ self.fail('a request on an engine that never reports READY must not succeed')
+ except urllib.error.HTTPError as e:
+ self.assertEqual(e.code, 503)
+ body = e.read().decode()
+ self.assertIn('did not report READY', body)
+ self.assertIn('next request restarts it', body)
+ self.assertLess(time.monotonic() - t0, 60)
+
+ def test_ready_timeout_on_lazy_load_answers_the_request(self):
+ # lazy start: restart()'s own bookkeeping must not crash on the missing slot_cv either (#1527)
+ svc = self.service(self.make_script('silent', SILENT))
+ 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]}'
+ self.assert503(base)
+ self.assertFalse(svc.engine.starting)
+
+ def test_ready_timeout_on_restart_answers_the_request(self):
+ # the reporter's case: the engine had answered once, then a restart hangs on the READY read
+ hang = self.root / 'hang'
+ svc = self.service(self.make_script('hang_on_restart', HANG_ON_RESTART), ('--hang', str(hang)))
+ 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]}'
+ status, _ = self.post(base) # first request: the engine works
+ self.assertEqual(status, 200)
+ svc.engine.proc.kill(); svc.engine.proc.wait(10) # it dies between requests
+ hang.touch() # and its restart never reports READY
+ self.assert503(base)
+ self.assertFalse(svc.engine.starting)
+
+
+if __name__ == '__main__':
+ unittest.main()