okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
5c0c80d8b
main
1"""#6055: a server stopped without --shutdown-timeout refuses new inference while the engine closes. 2 3Before, admission stayed open during the stop (begin_drain ran only with --shutdown-timeout), so a chat request that 4arrived in those seconds was accepted, the engine closed under it, and it came back as 200 with 0 tokens and 5finish_reason "length". Now the stop always closes admission first: such a request gets 503 server_draining. 6""" 7 8import contextlib 9import io 10import os 11import threading 12import time as real_time 13import unittest 14from unittest import mock 15 16from serve import server 17 18 19class _Clock : 20"""serve.server's `time`, except that the main thread's idle `time.sleep(1)` is a Ctrl+C (the stop).""" 21 22def __getattr__ (self ,name ): 23return getattr (real_time ,name ) 24 25@staticmethod 26def sleep (seconds ): 27if threading .current_thread ()is threading .main_thread ()and seconds == 1 : 28raise KeyboardInterrupt 29real_time .sleep (seconds ) 30 31 32class StopClosesAdmission (unittest .TestCase ): 33def run_stop (self ,extra ): 34drains ,states = [], [] 35original = server .Service .begin_drain 36 37def begin_drain (svc ): 38drains .append (svc ) 39pending = original (svc ) 40states .append (svc .reserve_inference ())# what a request arriving now gets: None = 503 server_draining 41return pending 42 43env = {k :v for k ,v in os .environ .items ()if k not in ("STRATA_API_KEY" ,"STRATA_TELEMETRY_KEY" )} 44with mock .patch .dict (os .environ ,env ,clear = True ), \ 45mock .patch .object (server ,"time" ,_Clock ()), \ 46mock .patch .object (server .lifeline ,"install_stop_handlers" ,lambda * a ,** k :None ), \ 47mock .patch .object (server .Service ,"begin_drain" ,begin_drain ), \ 48contextlib .redirect_stdout (io .StringIO ())as out ,contextlib .redirect_stderr (io .StringIO ()): 49code = server .main (["--engine" ,"mock" ,"--port" ,"0" ,* extra ]) 50return code ,drains ,states ,out .getvalue () 51 52def test_stop_without_shutdown_timeout_refuses_new_requests (self ): 53code ,drains ,states ,out = self .run_stop ([]) 54self .assertIn ("[strata] stopped" ,out ) 55self .assertEqual (len (drains ),1 ,"the stop closes admission even without --shutdown-timeout" ) 56self .assertEqual (states , [None ]) 57self .assertNotIn ("draining" ,out )# no wait and no drain line: only admission is closed 58self .assertEqual (code ,0 )# a clean stop: nothing was in flight, so nothing undrained 59 60def test_stop_with_shutdown_timeout_is_unchanged (self ): 61code ,drains ,states ,_out = self .run_stop (["--shutdown-timeout" ,"0" ]) 62self .assertEqual (len (drains ),1 ) 63self .assertEqual (states , [None ]) 64 65 66if __name__ == "__main__" : 67unittest .main ()