okami.mom/strata

git clone https://git.nkp.pet/okami.mom/strata

Niko1221Merge f-0215-stopdrain: serve: a stop without --shutdown-timeout refuses new requests while the engine closes (#6055)5c0c80d8b

main
2.8 KiB67 linesraw
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
22    def __getattr__(self, name):
23        return getattr(real_time, name)
24
25    @staticmethod
26    def sleep(seconds):
27        if threading.current_thread() is threading.main_thread() and seconds == 1:
28            raise KeyboardInterrupt
29        real_time.sleep(seconds)
30
31
32class StopClosesAdmission(unittest.TestCase):
33    def run_stop(self, extra):
34        drains, states = [], []
35        original = server.Service.begin_drain
36
37        def begin_drain(svc):
38            drains.append(svc)
39            pending = original(svc)
40            states.append(svc.reserve_inference())        # what a request arriving now gets: None = 503 server_draining
41            return pending
42
43        env = {k: v for k, v in os.environ.items() if k not in ("STRATA_API_KEY", "STRATA_TELEMETRY_KEY")}
44        with mock.patch.dict(os.environ, env, clear=True), \
45                mock.patch.object(server, "time", _Clock()), \
46                mock.patch.object(server.lifeline, "install_stop_handlers", lambda *a, **k: None), \
47                mock.patch.object(server.Service, "begin_drain", begin_drain), \
48                contextlib.redirect_stdout(io.StringIO()) as out, contextlib.redirect_stderr(io.StringIO()):
49            code = server.main(["--engine", "mock", "--port", "0", *extra])
50        return code, drains, states, out.getvalue()
51
52    def test_stop_without_shutdown_timeout_refuses_new_requests(self):
53        code, drains, states, out = self.run_stop([])
54        self.assertIn("[strata] stopped", out)
55        self.assertEqual(len(drains), 1, "the stop closes admission even without --shutdown-timeout")
56        self.assertEqual(states, [None])
57        self.assertNotIn("draining", out)                 # no wait and no drain line: only admission is closed
58        self.assertEqual(code, 0)                         # a clean stop: nothing was in flight, so nothing undrained
59
60    def test_stop_with_shutdown_timeout_is_unchanged(self):
61        code, drains, states, _out = self.run_stop(["--shutdown-timeout", "0"])
62        self.assertEqual(len(drains), 1)
63        self.assertEqual(states, [None])
64
65
66if __name__ == "__main__":
67    unittest.main()