mirror of
https://github.com/headroomlabs-ai/headroom.git
synced 2026-11-24 19:52:53 +08:00
fix(proxy): keep relay tasks when the same WS handle re-registers (#4904)
## Description
`WebSocketSessionRegistry.register()` is documented as idempotent by
`session_id`, but a second call with the **same handle object** dropped
the relay tasks attached to it:
```python
reg = WebSocketSessionRegistry()
handle = WSSessionHandle(session_id="s1", request_id="r1")
reg.register(handle)
reg.attach_tasks("s1", [task1, task2])
# active_relay_task_count() == 2
reg.register(handle) # documented idempotent re-registration
# active_relay_task_count() == 0, handle.relay_tasks == []
```
The replace path subtracted the old handle's task count and cleared its
list, then re-added `len(handle.relay_tasks)` — already zero after the
clear. Live task references were lost, the `active_relay_tasks` gauge
drifted, and `/debug/ws-sessions` lost the task names.
No tracking issue: this is a small bug fix, which CONTRIBUTING routes
straight to a PR with repro + test.
## Type of Change
- [x] Bug fix (non-breaking change that fixes an issue)
- [ ] New feature (non-breaking change that adds functionality)
- [ ] Breaking change (fix or feature that would cause existing
functionality to change)
- [ ] Documentation update
- [ ] Performance improvement
- [ ] Code refactoring (no functional changes)
## Changes Made
- `register()` returns early when the registered handle is the same
object, so a documented-idempotent re-registration is a true no-op.
- Re-registration with a *different* handle keeps the existing
replace-and-release behavior.
- The task-count subtraction is clamped at zero.
- Added `test_re_registering_the_same_handle_keeps_its_relay_tasks` in
`tests/test_ws_session_registry.py`.
## Testing
- [x] Unit tests pass (`pytest tests/test_ws_session_registry.py`)
- [x] Linting passes (`ruff check` on the changed files)
- [ ] Type checking passes (`mypy headroom`) — not run; the change is a
control-flow guard with no type surface change
- [x] New tests added for new functionality
- [ ] Manual testing performed — no live WS relay needed for an
in-memory registry fix
### Test Output
```text
$ python -m pytest tests/test_ws_session_registry.py -q
9 passed in 0.64s
$ python -m ruff check headroom/proxy/ws_session_registry.py tests/test_ws_session_registry.py
All checks passed!
$ python -m ruff format --check headroom/proxy/ws_session_registry.py tests/test_ws_session_registry.py
2 files already formatted
```
Before the fix the new test fails (task count 0 after re-registration);
it passes with the guard.
## Real Behavior Proof
- Environment: Windows 11, Python 3.12 venv, repo at `main` + this
branch
- Exact command / steps: `pytest tests/test_ws_session_registry.py -q`
- Observed result: 9 passed; the new test fails on `main` with
`active_relay_task_count() == 0`
- Not tested: a live Codex WS session through the proxy (the registry is
in-memory and network-free by design)
## Runtime Rollout Safety
- Rollout-managed feature(s): none
- Minimum rollout channel: default
- Stable/default behavior changed: same-handle `register()` now
preserves attached tasks instead of clearing them; different-handle
registration is unchanged
- Kill switch / disable path: none needed — revert the commit
- Unsafe override required: no
- Qualification impact: none
- Rollback path: `git revert` the commit
## Review Readiness
- [x] I have performed a self-review
- [x] This PR is ready for human review
## Checklist
- [x] My code follows the project's style guidelines
- [x] I have added tests that prove the fix is effective
- [ ] Documentation updated — not needed; behavior now matches the
existing docstring
This commit is contained in:
@@ -129,10 +129,15 @@ class WebSocketSessionRegistry:
|
||||
active session at a time.
|
||||
"""
|
||||
existing = self._sessions.get(handle.session_id)
|
||||
if existing is handle:
|
||||
# Same handle re-registered: keep the tasks attached to it. The
|
||||
# call is already a no-op for the session, and clearing here would
|
||||
# drop live task references and under-count the Prometheus gauge.
|
||||
return
|
||||
if existing is not None:
|
||||
# Re-registration: release any tasks we were tracking on the
|
||||
# old handle before replacing it.
|
||||
self._active_relay_tasks -= len(existing.relay_tasks)
|
||||
# Re-registration with a new handle: release any tasks we were
|
||||
# tracking on the old handle before replacing it.
|
||||
self._active_relay_tasks = max(0, self._active_relay_tasks - len(existing.relay_tasks))
|
||||
existing.relay_tasks.clear()
|
||||
self._sessions[handle.session_id] = handle
|
||||
self._active_relay_tasks += len(handle.relay_tasks)
|
||||
|
||||
@@ -153,6 +153,34 @@ def test_deregister_releases_task_references():
|
||||
assert reg.active_relay_task_count() == 0
|
||||
|
||||
|
||||
def test_re_registering_the_same_handle_keeps_its_relay_tasks():
|
||||
"""``register`` is idempotent by ``session_id``; a second call with the same
|
||||
handle must not drop the tasks already attached to it or mis-count the gauge."""
|
||||
reg = WebSocketSessionRegistry()
|
||||
handle = _make_handle()
|
||||
|
||||
class _Task:
|
||||
def done(self) -> bool:
|
||||
return False
|
||||
|
||||
def cancel(self) -> bool:
|
||||
return True
|
||||
|
||||
def get_name(self) -> str:
|
||||
return "t"
|
||||
|
||||
task = _Task()
|
||||
reg.register(handle)
|
||||
reg.attach_tasks("sess-1", [task])
|
||||
assert reg.active_relay_task_count() == 1
|
||||
|
||||
reg.register(handle)
|
||||
|
||||
assert handle.relay_tasks == [task]
|
||||
assert reg.active_relay_task_count() == 1
|
||||
assert reg.active_count() == 1
|
||||
|
||||
|
||||
def test_attach_tasks_on_missing_session_is_noop():
|
||||
reg = WebSocketSessionRegistry()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user