"""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()