marimo-team / marimo

A reactive notebook for Python — run reproducible experiments, execute as a script, deploy as an app, and version with git.
https://marimo.io
Apache License 2.0
8.06k stars 290 forks source link

Cannot interrupt async cell #2684

Open davidbrochart opened 1 month ago

davidbrochart commented 1 month ago

Describe the bug

The following code:

import asyncio

await asyncio.sleep(10)

cannot be interrupted: the cell runs until complete and then shows a CancelledError.

Environment

{
  "marimo": "0.9.11",
  "OS": "Linux",
  "OS Version": "6.8.0-47-generic",
  "Processor": "x86_64",
  "Python Version": "3.12.3",
  "Binaries": {
    "Browser": "130.0.6723.58",
    "Node": "v22.9.0"
  },
  "Dependencies": {
    "click": "8.1.3",
    "docutils": "0.21.2",
    "itsdangerous": "2.2.0",
    "jedi": "0.19.1",
    "markdown": "3.7",
    "narwhals": "1.10.0",
    "packaging": "24.1",
    "psutil": "6.1.0",
    "pygments": "2.18.0",
    "pymdown-extensions": "10.11.2",
    "pyyaml": "6.0.2",
    "ruff": "0.6.9",
    "starlette": "0.41.0",
    "tomlkit": "0.13.2",
    "typing-extensions": "4.12.2",
    "uvicorn": "0.32.0",
    "websockets": "12.0"
  },
  "Optional Dependencies": {
    "duckdb": "1.1.2"
  }
}

Code to reproduce

import asyncio

await asyncio.sleep(10)
akshayka commented 1 month ago

Thanks for the bug report. This is a known issue, but I'm not sure how to solve it. I believe ipykernel suffers from the same problem — actually I see you reported it there too :)

If you have

import asyncio

await asyncio.sleep(10)
print(1)

and interrupt the sleep, the cell doesn't actually run until complete — 1 won't be printed. But the sleep() can't be interrupted.

akshayka commented 1 month ago

Actually, I think I know how to fix this, by adding the interrupt handler to the event loop when a coroutine is being awaited.