Skip to content

fix: release lock before streaming and add kernel interrupt support#236

Draft
devin-ai-integration[bot] wants to merge 1 commit intomainfrom
devin/1774369859-fix-lock-orphan-and-interrupt
Draft

fix: release lock before streaming and add kernel interrupt support#236
devin-ai-integration[bot] wants to merge 1 commit intomainfrom
devin/1774369859-fix-lock-orphan-and-interrupt

Conversation

@devin-ai-integration
Copy link

Summary

Fixes #213asyncio.Lock in messaging.py not released on client disconnect, causing cascading timeouts.

Two changes:

  1. Lock scope narrowing (template/server/messaging.py): Splits execute() into Phase A (under lock: prepare env vars, send request, schedule cleanup task) and Phase B (no lock: stream results). When a client disconnects mid-stream, the lock is already released — no orphaned lock, no cascade. Addresses the env-var cleanup race flagged in PRs fix: release lock before streaming to prevent orphan on client disconnect #234/fix: release asyncio.Lock before streaming to prevent orphan on client disconnect #235 by creating _cleanup_task while still holding the lock.

  2. Kernel interrupt endpoint (template/server/main.py, Python SDK, JS SDK): Adds POST /contexts/{id}/interrupt that proxies to Jupyter's kernel interrupt API. Exposed as interrupt_code_context() / interruptCodeContext() in SDKs. Allows stopping long-running code without restarting the kernel (state preserved).

Review & Testing Checklist for Human

  • Verify concurrent websocket sends are safe with websockets==12.0: After the lock is released, _cleanup_env_vars may call self._ws.send() concurrently with the next execute()'s self._ws.send() (under lock). The assumption is websockets 12.x handles this internally — confirm this or add explicit serialization.
  • Verify cleanup task doesn't re-introduce blocking: The cleanup task is await-ed at the start of the next execute(). If the kernel is still busy with the previous code, this await blocks. Confirm this is acceptable behavior vs. the old cascading-lock behavior.
  • Test the lock fix end-to-end on a live sandbox: Run the reproduction from the issue — sleep(30) with a 5s SDK timeout, then immediately run print('hello') on the same context. The second call should not be blocked by an orphaned lock.
  • Test interrupt preserves kernel state: After calling interrupt_code_context on a long-running execution, verify variables/imports from prior cells survive.
  • Confirm async Python SDK auth header consistency: The async interrupt_code_context doesn't send X-Access-Token — this matches the existing async restart_code_context pattern, but differs from the sync version which does send it. Pre-existing inconsistency, but worth noting.

Notes

Link to Devin session: https://app.devin.ai/sessions/d709ebe9b3e14cea89be89c9c2faa29e
Requested by: @mlejva

Fixes #213 — asyncio.Lock in messaging.py not released on client disconnect,
causing cascading timeouts.

Changes:
- Narrow lock scope in ContextWebSocket.execute() to only cover the
  prepare+send phase (Phase A), releasing it before result streaming
  (Phase B). This prevents orphaned locks on client disconnect.
- Schedule env var cleanup task under the lock (before release) to
  avoid the race condition flagged in PRs #234/#235.
- Add POST /contexts/{id}/interrupt endpoint that calls Jupyter's
  kernel interrupt API, allowing clients to stop long-running code
  without restarting the kernel (preserves state).
- Add interrupt_code_context/interruptCodeContext to Python and JS SDKs.

Co-Authored-By: vasek <vasek.mlejnsky@gmail.com>
@devin-ai-integration
Copy link
Author

🤖 Devin AI Engineer

I'll be helping with this pull request! Here's what you should know:

✅ I will automatically:

  • Address comments on this PR. Add '(aside)' to your comment to have me ignore it.
  • Look at CI failures and help fix them

Note: I can only respond to comments from users who have write access to this repository.

⚙️ Control Options:

  • Disable automatic comment and CI monitoring

@mishushakov mishushakov marked this pull request as draft March 24, 2026 19:00
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.

asyncio.Lock in messaging.py not released on client disconnect → cascading timeouts

1 participant