okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
2503b206b
main
1"""The picture reader's READY pipe (serve/server.py Vision._start_reader / _await_ready). 2 3A crashing reader can start a debugger (ggml's abort path forks gdb) and the debugger writes to the reader's stdout, the pipe the server reads the 4READY line from. The first line of that pipe is then not READY. The server used to kill the reader on any first line that was not READY, while the 5debugger was still attached to it. Now: READY and ERR lines are answers; any other line is a stray line, kept and logged, and the wait goes on with 6ONE deadline. A reader that is only printing is never killed. A reader whose process has ended counts as ended after READY_GRACE_S even when a 7debugger or a child still holds the pipe open. The readers here are small Python scripts run as real processes. 8 9python -m serve.test_vision_ready_pipe 10""" 11import contextlib 12import io 13import subprocess 14import sys 15import tempfile 16import textwrap 17import time 18import unittest 19from pathlib import Path 20from unittest import mock 21 22import serve .server as server 23 24 25def plain_popen (what ,args ,** kw ):# the real process, without the lifeline's anchor and watcher 26kw .pop ("role" ,None ) 27return subprocess .Popen (args ,** kw ) 28 29 30class ReadyPipe (unittest .TestCase ): 31def setUp (self ): 32self .tmp = tempfile .TemporaryDirectory () 33self .addCleanup (self .tmp .cleanup ) 34self .procs = [] 35self .addCleanup (self .stop_all ) 36 37def stop_all (self ): 38for p in self .procs : 39if p .poll ()is None : 40p .kill () 41p .wait (timeout = 10 ) 42for stream in (p .stdin ,p .stdout ): 43with contextlib .suppress (OSError ,ValueError ): 44stream .close () 45 46def reader (self ,body :str ): 47"""A Vision whose reader is a script with `body`; returns it (nothing started yet).""" 48script = Path (self .tmp .name )/ ("reader%d.py" % len (list (Path (self .tmp .name ).glob ("reader*.py" )))) 49script .write_text ("import os, subprocess, sys, time\n" + textwrap .dedent (body ),encoding = "utf-8" ) 50v = server .Vision .__new__ (server .Vision ) 51v .spawn = ([sys .executable ,"-u" ,str (script )],None ,None ) 52v .dir = Path (self .tmp .name ) 53v .stopped ,v .proc = True ,None 54return v 55 56def start (self ,v ): 57"""v._start_reader() with the process kept for cleanup; (the error or None, the text it printed, seconds taken).""" 58out ,error ,t0 = io .StringIO (),None ,time .monotonic () 59real = plain_popen 60 61def keep (what ,args ,** kw ): 62p = real (what ,args ,** kw ) 63self .procs .append (p ) 64return p 65 66with mock .patch .object (server ,"popen" ,keep ),contextlib .redirect_stdout (out ): 67try : 68v ._start_reader () 69except RuntimeError as e : 70error = e 71return error ,out .getvalue (),time .monotonic ()- t0 72 73def test_a_stray_line_before_READY_does_not_kill_the_reader (self ): 74v = self .reader (""" 75print("[New LWP 477206]", flush=True) 76time.sleep(1.0) 77print("READY 2560", flush=True) 78sys.stdin.readline() 79""" ) 80error ,said ,_ = self .start (v ) 81self .assertIsNone (error ) 82self .assertFalse (v .stopped ) 83self .assertIsNone (v .proc .poll (),"a reader that was only printing is still running" ) 84self .assertIn ("[New LWP 477206]" ,said )# logged 85self .assertIn ("printed on its output" ,said ) 86 87def test_the_encode_answers_after_READY_are_not_taken_by_the_start (self ): 88v = self .reader (""" 89print("noise", flush=True) 90print("READY 2560", flush=True) 91print("OK 7 1 1 1", flush=True) 92sys.stdin.readline() 93""" ) 94error ,_ ,_ = self .start (v ) 95self .assertIsNone (error ) 96self .assertEqual (v ._readline (5.0 ,"x" ),"OK 7 1 1 1\n" ) 97 98def test_many_stray_lines_are_bounded_in_the_log_and_the_message (self ): 99v = self .reader (""" 100for i in range(200): 101print("stray %d" % i, flush=True) 102sys.exit(1) 103""" ) 104error ,said ,_ = self .start (v ) 105self .assertIsNotNone (error ) 106self .assertLessEqual (said .count ("printed on its output" ),server .Vision .STRAY_KEEP ) 107self .assertIn ("stray 199" ,str (error ))# the last ones are in the message 108self .assertNotIn ("stray 0 " ,str (error )+ " " ) 109 110def test_a_stray_line_then_the_end_names_the_exit_and_the_line (self ): 111v = self .reader (""" 112print("[New LWP 5]", flush=True) 113sys.exit(1) 114""" ) 115error ,_ ,took = self .start (v ) 116self .assertIsNotNone (error ) 117text = str (error ) 118self .assertIn ("did not start" ,text ) 119self .assertIn ("exit code 1" ,text ) 120self .assertIn ("[New LWP 5]" ,text ) 121self .assertLess (took ,10 ) 122 123def test_a_stray_line_then_ERR_gives_the_ERR_line (self ): 124v = self .reader (""" 125print("[New LWP 5]", flush=True) 126print("ERR cannot load the vision encoder x.gguf", flush=True) 127sys.exit(1) 128""" ) 129error ,_ ,_ = self .start (v ) 130text = str (error ) 131self .assertIn ("ERR cannot load the vision encoder" ,text ) 132self .assertIn ("[New LWP 5]" ,text ) 133 134def test_a_reader_that_only_prints_is_ended_at_the_deadline_with_its_lines_in_the_message (self ): 135v = self .reader (""" 136print("[New LWP 5]", flush=True) 137time.sleep(60) 138""" ) 139with mock .patch .object (server ,"VISION_READY_S" ,1.5 ): 140error ,_ ,took = self .start (v ) 141self .assertIsNotNone (error ) 142self .assertIn ("said nothing for" ,str (error )) 143self .assertIn ("[New LWP 5]" ,str (error )) 144self .assertGreaterEqual (took ,1.4 ) 145self .assertLess (took ,8 ) 146self .assertIsNotNone (self .procs [- 1 ].wait (timeout = 10 ),"it was killed at the deadline (not before: the wait above took the whole deadline)" ) 147self .assertTrue (v .stopped ) 148self .assertIsNone (v .proc ) 149 150def test_a_reader_that_ended_while_a_child_holds_its_pipe_is_judged_after_the_grace (self ): 151v = self .reader (""" 152subprocess.Popen([sys.executable, "-c", "import time; time.sleep(5)"], stdout=sys.stdout) # the "debugger": holds the pipe 153print("[New LWP 5]", flush=True) 154os._exit(3) 155""" ) 156with mock .patch .object (server .Vision ,"READY_GRACE_S" ,0.5 ): 157error ,_ ,took = self .start (v ) 158self .assertIsNotNone (error ) 159self .assertIn ("exit code 3" ,str (error )) 160self .assertLess (took ,4.5 ,"the pipe's end (5 s) is not waited for: the process's end is what counts" ) 161 162def test_a_silent_reader_that_dies_is_the_same_as_before (self ): 163v = self .reader (""" 164sys.exit(7) 165""" ) 166error ,_ ,_ = self .start (v ) 167self .assertIn ("exit code 7" ,str (error )) 168self .assertIn ("without saying anything" ,str (error )) 169 170def test_the_cause_in_a_stray_line_is_named (self ): 171v = self .reader (""" 172print("cudaMalloc failed: CUDA-capable device(s) is/are busy or unavailable", flush=True) 173sys.exit(1) 174""" ) 175error ,_ ,_ = self .start (v ) 176self .assertIn ("Exclusive_Process" ,str (error )) 177 178def test_no_deadline_means_wait_for_ever_not_fail_at_once (self ): 179v = self .reader (""" 180time.sleep(0.6) 181print("READY 2560", flush=True) 182sys.stdin.readline() 183""" ) 184with mock .patch .object (server ,"VISION_READY_S" ,None ): 185error ,_ ,_ = self .start (v ) 186self .assertIsNone (error ) 187 188 189if __name__ == "__main__" : 190unittest .main ()