Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 26 additions & 1 deletion src/openenv/core/env_server/http_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,9 @@ def __init__(
self._session_modes: Dict[str, Optional[str]] = {}
self._session_websocket_attachments: set[str] = set()
self._session_pending_closes: set[str] = set()
# WebSocket messages being handled, per session; such sessions are never
# idle-reaped. A count, since an attached socket can share the session.
self._session_operations_in_flight: Dict[str, int] = {}
self._session_lock = asyncio.Lock()

# Create thread pool for running sync code in async context
Expand Down Expand Up @@ -734,6 +737,7 @@ async def _reap_idle_sessions(self) -> None:
for sid, info in self._session_info.items():
if (
sid not in self._session_websocket_attachments
and sid not in self._session_operations_in_flight
and now - info.last_activity_at > timeout
):
stale_ids.append(sid)
Expand All @@ -750,6 +754,7 @@ async def _reap_idle_sessions(self) -> None:
if (
info is None
or sid in self._session_websocket_attachments
or sid in self._session_operations_in_flight
or (now - info.last_activity_at) <= timeout
):
continue
Expand Down Expand Up @@ -2097,6 +2102,19 @@ async def websocket_endpoint(websocket: WebSocket):
# Receive message from client
raw_message = await websocket.receive_text()

# The reaper or shutdown may have destroyed this
# session (closing its env and freeing its slot).
if session_id not in self._sessions:
error_resp = WSErrorResponse(
data={
"message": "Session expired or was closed; "
"open a new connection",
"code": WSErrorCode.SESSION_ERROR,
}
)
await websocket.send_text(error_resp.model_dump_json())
break

try:
message_dict = json.loads(raw_message)
except json.JSONDecodeError as e:
Expand Down Expand Up @@ -2169,6 +2187,9 @@ async def websocket_endpoint(websocket: WebSocket):
"Rubric introspection unavailable"
)

in_flight = self._session_operations_in_flight
in_flight[session_id] = in_flight.get(session_id, 0) + 1
self._update_session_activity(session_id)
try:
match msg_type:
case "reset":
Expand Down Expand Up @@ -2206,7 +2227,6 @@ async def websocket_endpoint(websocket: WebSocket):
value=msg.data.get("seed"),
accepted="seed" in valid_kwargs,
)
self._update_session_activity(session_id)

response = WSObservationResponse(
data=serialize_observation(observation),
Expand Down Expand Up @@ -2338,6 +2358,11 @@ async def websocket_endpoint(websocket: WebSocket):
}
)
await websocket.send_text(error_resp.model_dump_json())
finally:
in_flight[session_id] -= 1
if in_flight[session_id] == 0:
del in_flight[session_id]
self._update_session_activity(session_id)

except WebSocketDisconnect:
pass
Expand Down
172 changes: 172 additions & 0 deletions tests/core/test_production_mode_routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -2423,6 +2423,178 @@ def state(self):
assert server._reaper_task is None


class TestWebSocketSessionReaper:
"""The idle-session reaper running against plain `/ws` sessions.

The reaper polls every `max(timeout / 4, 5)` seconds, so with a 1 s
timeout its first check runs 5 s after app startup.
"""

@staticmethod
def make_server(env_cls):
from openenv.core.env_server.types import ConcurrencyConfig

server = HTTPEnvServer(
env_cls,
MinimalAction,
Observation,
concurrency_config=ConcurrencyConfig(
max_concurrent_envs=1, session_timeout=1.0
),
)
app = FastAPI()
server.register_routes(app)
return app, server

@staticmethod
def wait_until_reaped(server, deadline_s=8.0):
import time

deadline = time.monotonic() + deadline_s
while server.active_sessions and time.monotonic() < deadline:
time.sleep(0.05)
assert server.active_sessions == 0

def test_step_spanning_reaper_check_is_not_reaped(self):
import asyncio

envs = []

class SlowStepEnvironment(MinimalEnvironment):
def __init__(self):
super().__init__()
self.busy = False
self.closed_mid_step = False
envs.append(self)

async def step_async(self, action, **kwargs):
self.busy = True
# Longer than the timeout, and spans the reaper's first check.
await asyncio.sleep(5.5)
self.busy = False
return MinimalObservation(response="slow", reward=1.0)

def close(self):
if self.busy:
self.closed_mid_step = True

app, server = self.make_server(SlowStepEnvironment)
with TestClient(app) as client, client.websocket_connect("/ws") as ws:
ws.send_json({"type": "reset", "data": {}})
assert ws.receive_json()["type"] == "observation"

ws.send_json({"type": "step", "data": {"message": "slow"}})
response = ws.receive_json()

assert response["type"] == "observation"
assert response["data"]["observation"]["response"] == "slow"
assert not envs[0].closed_mid_step
assert server.active_sessions == 1

def test_reaped_websocket_session_stops_serving(self):
from starlette.websockets import WebSocketDisconnect

app, server = self.make_server(MinimalEnvironment)
with TestClient(app) as client:
with client.websocket_connect("/ws") as ws:
ws.send_json({"type": "reset", "data": {}})
assert ws.receive_json()["type"] == "observation"
self.wait_until_reaped(server)

ws.send_json({"type": "step", "data": {"message": "hi"}})
response = ws.receive_json()
assert response["type"] == "error"
assert response["data"]["code"] == "SESSION_ERROR"
with pytest.raises(WebSocketDisconnect):
ws.receive_json()

# The released slot is free for a new session.
with client.websocket_connect("/ws") as ws2:
ws2.send_json({"type": "reset", "data": {}})
assert ws2.receive_json()["type"] == "observation"
assert server.active_sessions == 1

def test_idle_session_is_reaped_after_failed_step(self):
envs = []

class FailingStepEnvironment(MinimalEnvironment):
def __init__(self):
super().__init__()
self.closed = False
envs.append(self)

def step(self, action):
raise RuntimeError("step failed")

def close(self):
self.closed = True

app, server = self.make_server(FailingStepEnvironment)
with TestClient(app) as client, client.websocket_connect("/ws") as ws:
ws.send_json({"type": "reset", "data": {}})
assert ws.receive_json()["type"] == "observation"
ws.send_json({"type": "step", "data": {"message": "hi"}})
assert ws.receive_json()["data"]["code"] == "EXECUTION_ERROR"

self.wait_until_reaped(server)
assert envs[0].closed

def test_second_socket_on_session_keeps_step_in_flight(self):
import asyncio
import time

envs = []

class SlowStepEnvironment(MinimalEnvironment):
def __init__(self):
super().__init__()
self.busy = False
self.closed_mid_step = False
envs.append(self)

async def step_async(self, action, **kwargs):
self.busy = True
await asyncio.sleep(5.5)
self.busy = False
return MinimalObservation(response="slow", reward=1.0)

def close(self):
if self.busy:
self.closed_mid_step = True

app, server = self.make_server(SlowStepEnvironment)
with TestClient(app) as client, client.websocket_connect("/ws") as ws:
ws.send_json({"type": "reset", "data": {}})
assert ws.receive_json()["type"] == "observation"
# A /ws client can learn its session ID and attach a second socket.
ws.send_json(
{
"type": "mcp",
"data": {
"jsonrpc": "2.0",
"id": 1,
"method": "openenv/session/create",
"params": {},
},
}
)
session_id = ws.receive_json()["data"]["result"]["session_id"]

ws.send_json({"type": "step", "data": {"message": "slow"}})
deadline = time.monotonic() + 2
while not envs[0].busy and time.monotonic() < deadline:
time.sleep(0.01)
# The second socket's message ends while the step is still running.
with client.websocket_connect(f"/ws?session_id={session_id}") as ws2:
ws2.send_json({"type": "state"})
assert ws2.receive_json()["type"] == "state"

response = ws.receive_json()
assert response["data"]["observation"]["response"] == "slow"
assert not envs[0].closed_mid_step
assert server.active_sessions == 1


class TestWebSocketMCP:
"""Tests for WebSocket MCP message handling."""

Expand Down
Loading