[Bug-1229] WS handler: persistent event loop per container

Story 1229Epic None2 files · +31/−1

PR #316 · pramaan-io/pramaan-functions · merged 2026-06-12

Why this matters

One container, one async world — or none at all.

Bug twelve-twenty-nine

Every connection lives in its own async world — they cannot coordinate.

The fix — One container, one loop — every WS connection shares the same async world.

How it flows

sequenceDiagram
  autonumber
  participant container as Container
  participant handler as WS Handler
  participant connections as WS Clients
  container->>handler: container boots
  handler->>handler: first _event_loop() call
  handler->>handler: _persistent_loop created
  connections->>handler: client connects
  handler->>handler: _event_loop() returns same loop
  handler-->>connections: all pulse on one loop

src/app_api/router_ws.py

Methodin → outwhat it does
_event_loop() → asyncio.AbstractEventLoopreturns the persistent loop, creating it once on the very first call

What changed, file by file

WebSocket router — adds a container-level singleton asyncio loop via the persistent-loop field and the event-loop getter, fixing Bug twelve-twenty-nine.

new methodsignature
_event_loop() → asyncio.AbstractEventLoop
New fields: _persistent_loop : asyncio.AbstractEventLoop | None

Files changed (2)

File±
src/app_api/router_ws.py ★+20 −1
tests/unit/test_router_ws.py+11