Fix router HTTP timeout connection cleanup#1115
Conversation
📝 WalkthroughWalkthroughAdds a ChangesRouter timeout cleanup and tests
Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 minutes Poem
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
🧹 Nitpick comments (1)
src/service/router/router.py (1)
102-107: ⚡ Quick winAdd return type annotation to the helper function.
The
_remove_router_connectionfunction is missing a return type annotation. As per coding guidelines, type annotations should be added where they improve clarity.📝 Proposed fix
-def _remove_router_connection(key: str): +def _remove_router_connection(key: str) -> None: connection = connections.pop(key, None) if connection and connection.wait_close: connection.wait_close.set()🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@src/service/router/router.py` around lines 102 - 107, The _remove_router_connection function is missing a return type annotation. Add the return type annotation -> None to the function signature since the function does not return any value, only performs operations on the connection object retrieved from the connections dictionary.Source: Coding guidelines
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@src/service/router/test_router.py`:
- Around line 34-37: The cookies dictionary is currently defined as a class
attribute in the _FakeRequest class, which causes all instances to share the
same mutable dictionary. Move the cookies dictionary definition from the class
level into an __init__ method of the _FakeRequest class to make it an instance
attribute instead, ensuring each instance has its own independent copy of the
cookies dictionary.
---
Nitpick comments:
In `@src/service/router/router.py`:
- Around line 102-107: The _remove_router_connection function is missing a
return type annotation. Add the return type annotation -> None to the function
signature since the function does not return any value, only performs operations
on the connection object retrieved from the connections dictionary.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Enterprise
Run ID: 256030e3-d13e-450f-aacb-e83df1dc01dd
📒 Files selected for processing (3)
src/service/router/BUILDsrc/service/router/router.pysrc/service/router/test_router.py
| cookies = { | ||
| '_osmo_router_affinity': 'sticky-session', | ||
| 'ignored': 'cookie', | ||
| } |
There was a problem hiding this comment.
Use instance attribute for mutable cookies dictionary.
The cookies dictionary is defined as a class attribute, which means all instances of _FakeRequest share the same dict. If multiple instances were created and one modified cookies, all would see the change. While this test only creates one instance, it's better practice to make mutable data instance attributes.
🔧 Proposed fix
class _FakeRequest:
- cookies = {
- '_osmo_router_affinity': 'sticky-session',
- 'ignored': 'cookie',
- }
+ def __init__(self):
+ self.cookies = {
+ '_osmo_router_affinity': 'sticky-session',
+ 'ignored': 'cookie',
+ }
async def body(self):
return b''🧰 Tools
🪛 Ruff (0.15.17)
[warning] 34-37: Mutable default value for class attribute
(RUF012)
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
In `@src/service/router/test_router.py` around lines 34 - 37, The cookies
dictionary is currently defined as a class attribute in the _FakeRequest class,
which causes all instances to share the same mutable dictionary. Move the
cookies dictionary definition from the class level into an __init__ method of
the _FakeRequest class to make it an instance attribute instead, ensuring each
instance has its own independent copy of the cookies dictionary.
Source: Linters/SAST tools
Summary
Fix a router webserver HTTP timeout path that left pending `PORTFORWARD-*` entries in the in-memory connection registry when the backend never connected.
Details
`webserver_http_request()` allocates a router connection before asking the control websocket to open the backend port-forward. If the backend connection times out, the request returns `504` but previously left the pending entry behind. Repeated backend timeouts could accumulate stale connection records.
This change removes the pending connection on timeout and on the defensive early failure path after allocation. The backend websocket cleanup now uses an idempotent `pop()` as well, so a cleanup race does not turn into a `KeyError`.
Validation
Summary by CodeRabbit
Bug Fixes
Tests