Add reference counting to ModelInstance for parallel test safety (#16672)

This commit is contained in:
Simo Lin
2026-01-07 08:28:15 -08:00
committed by GitHub
parent b5a94f8a8e
commit 7385834c8d
3 changed files with 100 additions and 6 deletions
+24 -2
View File
@@ -151,6 +151,7 @@ def model_client(request: pytest.FixtureRequest, model_pool: "ModelPool"):
def test_chat(model_client):
response = model_client.chat.completions.create(...)
"""
import openai
from infra import PARAM_MODEL
marker = request.node.get_closest_marker(PARAM_MODEL)
@@ -163,10 +164,23 @@ def model_client(request: pytest.FixtureRequest, model_pool: "ModelPool"):
model_id = marker.args[0]
try:
return model_pool.get_client(model_id)
instance = model_pool.get(model_id)
except KeyError:
pytest.skip(f"Model {model_id} not available in model pool")
# Acquire reference to prevent eviction during test
instance.acquire()
client = openai.OpenAI(
base_url=f"{instance.base_url}/v1",
api_key="not-used",
)
yield client
# Release reference to allow eviction
instance.release()
@pytest.fixture
def model_base_url(request: pytest.FixtureRequest, model_pool: "ModelPool") -> str:
@@ -189,6 +203,14 @@ def model_base_url(request: pytest.FixtureRequest, model_pool: "ModelPool") -> s
model_id = marker.args[0]
try:
return model_pool.get_base_url(model_id)
instance = model_pool.get(model_id)
except KeyError:
pytest.skip(f"Model {model_id} not available in model pool")
# Acquire reference to prevent eviction during test
instance.acquire()
yield instance.base_url
# Release reference to allow eviction
instance.release()