diff --git a/serve/server.py b/serve/server.py index 327c5ece..fc3b45d4 100644 --- a/serve/server.py +++ b/serve/server.py @@ -389,6 +389,12 @@ def btrace(*a): EOS_IDS = {248044, 248046} # <|endoftext|>, <|im_end|>: the engine's default --eos-ids +# The engine's own words for a verifier that cannot go on (its GPU waits were released, or an earlier window never +# finished, #267): the process may stay alive and even print DONE, but it must not take another request. +FATAL_PREFIXES = ("ERR verify: timed out at layer ", "ERR verify batch: timed out at layer ", + "ERR verify: an earlier window never finished", "ERR verify batch: an earlier window never finished") + + class StrataEngine: """The resident engine: `strata --serve` reads `GEN ` lines and streams `T ` lines, then `DONE ...`. Requests are serialized by the service's FIFO, so one pipe is enough. @@ -519,6 +525,18 @@ class StrataEngine: proc, lines = self.proc, self.lines # this process's: a restart replaces both (#344) slot_q = self.slot_q for line in proc.stdout: + # checked before batch routing: a fatal line is never a slot's own + if line.startswith(FATAL_PREFIXES): + # release_gpu_waits invalidates the verifier, even if the native + # process stays alive and prints DONE afterwards. + if self.proc is proc: + self.silent_note = "Unrecoverable native verification failure: " + line[4:].strip() + self.ended = True + try: + proc.terminate() + except OSError: + pass + break # publish EOF, never the trailing DONE if line.startswith(("BT ", "BDONE ")) and slot_q: # --batch: a batch slot's own lines try: slot_q[int(line.split()[1])].put(line) @@ -876,6 +894,8 @@ class StrataEngine: with a much shorter prompt (#656: `BYIELD `; the part read waits in a slot and the read goes on after). A consumer that stops early leaves the engine in step: the solo request is STOPped and read to its DONE, an admission to its BADM, a slot is BSTOPped and freed at its BDONE.""" + if not self.alive(): + raise EngineDied("the engine is unavailable; this request was not sent") self.progress = None keys = self.sampling_keys(sampling or {}) out: list[int] = [] @@ -1116,6 +1136,8 @@ class StrataEngine: """Yields token ids, and None as a heartbeat every 10 s while the engine is quiet (reading a long prompt): the HTTP layer turns it into an SSE comment, which keeps clients' watchdogs calm and notices a client that has gone. A consumer that stops early (or `cancel`) makes the engine STOP, so it does not run to max_new.""" + if not self.alive(): + raise EngineDied("the engine is unavailable; this request was not sent") if getattr(self, "batch", 0): yield from self.generate_batched(ids, max_new, sampling, cancel, embeddings) return diff --git a/serve/test_fatal_recovery.py b/serve/test_fatal_recovery.py new file mode 100644 index 00000000..bdd378bc --- /dev/null +++ b/serve/test_fatal_recovery.py @@ -0,0 +1,116 @@ +"""Real pipe/process protocol regression for a native ERR that leaves its process alive.""" +import os +from pathlib import Path +import sys +import tempfile +import threading +import time +import unittest +import json +import urllib.request +from serve.server import StrataEngine, Service, ChatTemplate, EngineDied, serve +from serve.test_server import ByteTokenizer + +FAKE=r''' +import sys +from pathlib import Path +state=Path(sys.argv[sys.argv.index('--state')+1]) +mode=sys.argv[sys.argv.index('--mode')+1] +print('READY 4096 stop',flush=True) +for line in sys.stdin: + if line.startswith('QUIT'):break + if line.startswith(('GEN','BGEN')): + if not state.exists(): + state.write_text('first request was attempted') + if mode=='invalid':print('ERR prompt exceeds context',flush=True) + else: + kind='verify batch' if mode=='batch' else 'verify' + print(f'ERR {kind}: timed out at layer 18; its GPU waits were released and the GPU finished (#267)',flush=True) + print('DONE 0 1 0 1 error',flush=True) + continue + print('T 90\nDONE 1 1 0 1 stop',flush=True) +''' + +class FatalRecovery(unittest.TestCase): + def setUp(self): + self.tmp=tempfile.TemporaryDirectory();self.addCleanup(self.tmp.cleanup) + self.root=Path(self.tmp.name);py=self.root/'native.py' + if os.name=='nt': # no shebangs on Windows: a .cmd runs the same fake engine + py.write_text(FAKE);self.script=self.root/'native.cmd' + self.script.write_text(f'@"{sys.executable}" "{py}" %*\r\n') + else: + self.script=py;py.write_text('#!'+sys.executable+'\n'+FAKE);py.chmod(0o700) + def engine(self,mode='fatal'): + e=StrataEngine(str(self.script),['--state',str(self.root/'state'),'--mode',mode]) + self.addCleanup(e.close);return e + def service(self,e): + return Service(e,ByteTokenizer(),ChatTemplate(Path(__file__).parent/'chat_template.jinja')) + def wait_dead(self,e): + end=time.monotonic()+2 + while e.alive() and time.monotonic()