okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
1306424be
main
1"""serve/test_fatal_gpu_restart.py: #4350. After a fatal GPU error the DeepSeek engine ends itself (ERR fatal GPU error: ...) and the 2server starts it again on the next request; three such deaths within ten minutes stop the restarts with a plain 503 text. 3CPU only, no engine: the server's bookkeeping is called on a stand-in.""" 4import sys 5import time 6import types 7import unittest 8from pathlib import Path 9 10sys .path .insert (0 ,str (Path (__file__ ).resolve ().parents [1 ])) 11from serve import server as S # noqa: E402 12 13 14def stand_in (pid ,last_err ): 15return types .SimpleNamespace (engine = types .SimpleNamespace (last_err = last_err ,proc = types .SimpleNamespace (pid = pid ))) 16 17 18class FatalGpuRestart (unittest .TestCase ): 19def note (self ,svc ,code = 3 ): 20S .Service ._note_fatal_gpu_exit .__get__ (svc )(code ) 21 22def make (self ,pid ,err = "fatal GPU error: cudaStreamSynchronize(s): an illegal memory access was encountered" ): 23svc = stand_in (pid ,err ) 24svc .FATAL_GPU_MAX = S .Service .FATAL_GPU_MAX 25svc .FATAL_GPU_WINDOW_S = S .Service .FATAL_GPU_WINDOW_S 26return svc 27 28def test_one_fatal_death_is_restarted_with_one_plain_line (self ): 29svc = self .make (100 ) 30self .note (svc )# no exception: the caller goes on to restart the engine 31 32def test_an_ordinary_death_is_not_counted (self ): 33svc = self .make (100 ,err = "the engine ran out of memory" ) 34for pid in range (10 ): 35svc .engine .proc .pid = pid 36self .note (svc ) 37self .assertFalse (svc .__dict__ .get ("_fatal_gpu_times" )) 38 39def test_the_same_death_seen_by_many_requests_counts_once (self ): 40svc = self .make (100 ) 41for _ in range (8 ): 42self .note (svc ) 43self .assertEqual (len (svc ._fatal_gpu_times ),1 ) 44 45def test_three_fatal_deaths_in_ten_minutes_stop_the_restarts_and_say_so (self ): 46svc = self .make (1 ) 47self .note (svc ) 48svc .engine .proc .pid = 2 49self .note (svc ) 50svc .engine .proc .pid = 3 51with self .assertRaises (S .EngineDied )as cm : 52self .note (svc ) 53self .assertIn ("3 times in 10 minutes" ,str (cm .exception )) 54self .assertIn ("not started again" ,str (cm .exception )) 55with self .assertRaises (S .EngineDied ):# the same dead engine asked again: still refused 56self .note (svc ) 57 58def test_after_the_window_it_tries_again (self ): 59svc = self .make (1 ) 60self .note (svc ) 61svc .engine .proc .pid = 2 62self .note (svc ) 63svc ._fatal_gpu_times [:]= [t - S .Service .FATAL_GPU_WINDOW_S - 1 for t in svc ._fatal_gpu_times ] 64svc .engine .proc .pid = 3 65self .note (svc )# the two old ones aged out: only this one counts 66 67def test_the_request_in_flight_fails_as_an_engine_death_not_a_bad_request (self ): 68eng = types .SimpleNamespace (last_err = None ,ended = False ) 69with self .assertRaises (S .EngineDied )as cm : 70S .StrataEngine ._raise_if_fatal_gpu (eng ,"ERR fatal GPU error: cudaStreamSynchronize(s): an illegal memory access was encountered" ) 71self .assertIn ("illegal memory access" ,str (cm .exception )) 72self .assertTrue (eng .ended )# the next request starts the engine at once 73self .assertTrue (eng .last_err .startswith ("fatal GPU error" )) 74 75def test_an_ordinary_engine_error_stays_a_bad_request (self ): 76eng = types .SimpleNamespace (last_err = None ,ended = False ) 77S .StrataEngine ._raise_if_fatal_gpu (eng ,"ERR the prompt (9 tokens) and max_new 5 are more than the context 8" )# no exception 78self .assertFalse (eng .ended ) 79 80 81if __name__ == "__main__" : 82unittest .main ()