Skip to content

fix(ui): force stdlib asyncio policy so 3-D Twin tab works under uvloop#17

Merged
harsh-pandhe merged 1 commit into
mainfrom
fix/streamlit-uvloop
May 20, 2026
Merged

fix(ui): force stdlib asyncio policy so 3-D Twin tab works under uvloop#17
harsh-pandhe merged 1 commit into
mainfrom
fix/streamlit-uvloop

Conversation

@harsh-pandhe
Copy link
Copy Markdown
Owner

Summary

`make ui` crashed every time the 3-D Twin tab tried to render:

```
Process Process-N:
...
ValueError: Can't patch loop of type <class 'uvloop.Loop'>
```

Root cause

uvicorn ships uvloop as a transitive dependency of Streamlit. If the parent Streamlit process's event-loop policy is uvloop's, `multiprocessing.Process` (forked by stpyvista's trame backend to render the 3-D scene) inherits it. The subprocess then calls `asyncio.run` → `nest_asyncio2._patch_loop`, which only knows how to patch stdlib loops, and bails on `uvloop.Loop`.

Fix

  • Reset `asyncio.set_event_loop_policy(asyncio.DefaultEventLoopPolicy())` at app import time. Streamlit's tornado server is unaffected; the child process now spawns a stdlib loop that `nest_asyncio2` can patch.
  • Wrap the `stpyvista(...)` call in try/except so any future backend crash degrades to a clear warning + `st.exception` panel rather than a silent subprocess traceback. The rest of the tab (sliders, compliance badges, GeoJSON import) stays interactive.

Test plan

  • `make ui` no longer prints the uvloop traceback in the terminal
  • 3-D Twin tab renders the interactive PyVista scene
  • No other code paths touched; the 157-test suite is unaffected

🤖 Generated with Claude Code

`make ui` crashed every time it tried to render the 3-D Twin tab:

  Process Process-1:
  ...
  ValueError: Can't patch loop of type <class 'uvloop.Loop'>

uvicorn ships uvloop as a transitive dependency of Streamlit. If the
parent Streamlit process's event-loop policy is uvloop's,
`multiprocessing.Process` (forked by stpyvista's trame backend to
render the 3-D scene) inherits it. The subprocess then calls
`asyncio.run` → `nest_asyncio2._patch_loop`, which only knows how to
patch stdlib loops, and bails on `uvloop.Loop`.

Fix:
- Reset `asyncio.set_event_loop_policy(asyncio.DefaultEventLoopPolicy())`
  at app import time. Streamlit's tornado server is unaffected; the
  child process now spawns a stdlib loop that nest_asyncio2 can patch.
- Wrap the `stpyvista(...)` call in try/except so any future backend
  crash degrades to a clear warning + an `st.exception` panel rather
  than a silent subprocess traceback, and the rest of the tab (sliders,
  compliance badges) stays interactive.

No other code paths touched; tests unaffected.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
@harsh-pandhe harsh-pandhe merged commit adc2971 into main May 20, 2026
2 checks passed
@harsh-pandhe harsh-pandhe deleted the fix/streamlit-uvloop branch May 20, 2026 05:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant