Skip to content

Commit

Permalink
[Serving][Fix] Fix problems in PopenServer (#2032)
Browse files Browse the repository at this point in the history
This PR fixes several problems in the PopenServer:

- Add check for the server is not started and the request returns a fail
number, e.g. 502. And changed the retry time to 0.1s.

- Add a `__enter__` and `__exit__` method for PopenServer.
When the program is interrupted, using with clause (`__enter__`
and `__exit__`) can ensure the server always terminates. When
using `start()` and `terminate()`, the server  may still be staying
in the background even though the parent process ends.
  • Loading branch information
Ubospica authored Mar 26, 2024
1 parent 1c975de commit 8796fb4
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 7 deletions.
20 changes: 17 additions & 3 deletions python/mlc_llm/serve/server/popen_server.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""The MLC LLM server launched in a subprocess."""

import subprocess
import sys
import time
Expand Down Expand Up @@ -64,13 +65,17 @@ def start(self) -> None:
openai_v1_models_url = "http://127.0.0.1:8000/v1/models"
query_result = None
timeout = 60
attempts = 0
attempts = 0.0
while query_result is None and attempts < timeout:
try:
query_result = requests.get(openai_v1_models_url, timeout=60)
if query_result.status_code != 200:
query_result = None
attempts += 0.1
time.sleep(0.1)
except: # pylint: disable=bare-except
attempts += 1
time.sleep(1)
attempts += 0.1
time.sleep(0.1)

# Check if the subprocess terminates unexpectedly or
# the queries reach the timeout.
Expand Down Expand Up @@ -117,3 +122,12 @@ def kill_child_processes():
except subprocess.TimeoutExpired:
pass
self._proc = None

def __enter__(self):
"""Start the server."""
self.start()
return self

def __exit__(self, exc_type, exc_val, exc_tb):
"""Terminate the server."""
self.terminate()
6 changes: 2 additions & 4 deletions tests/python/serve/server/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@ def launch_server(served_model): # pylint: disable=redefined-outer-name
model_lib_path=served_model[1],
enable_tracing=True,
)
server.start()
yield

# Fixture teardown code.
server.terminate()
with server:
yield

0 comments on commit 8796fb4

Please sign in to comment.