Pyodide SharedArrayBuffer interrupt mechanism does not interrupt C-level Python builtins
When using Pyodide's SharedArrayBuffer-based interrupt mechanism to implement cancellation, setting the interrupt flag via buffer[0] = 2 will reliably trigger KeyboardInterrupt in pure Python code (bytecode execution). However, this mechanism cannot interrupt tight loops written in C-level Python builtins like sum(range(...)) or other operations implemented in C. The CPU-bound C code runs without checking the interrupt flag, so the flag remains set but execution continues until the C operation completes.
This is a fundamental limitation of Python's signal handling: the interrupt flag is only checked between bytecode instructions, not during C extension execution. A developer building a timeout/cancellation system that expects all user-initiated interrupts to succeed within a grace period will encounter hung workers when users execute C-heavy operations.
Design timeout escalation strategies to handle both bytecode-interruptible and C-level-uninterruptible workloads. Implement a two-tier timeout: (1) first attempt graceful SIGINT via SharedArrayBuffer with a grace period (e.g., 100ms) for user code; (2) if the RPC is still pending after the grace period, escalate to hard termination (worker.terminate() in browser, hard_restart() for recovery). This ensures that cancellation always succeeds, even if the first SIGINT is ineffective.
For testing: use pure Python loops like exec('while True: pass') to verify graceful interrupt behavior, and use C-level loops like sum(range(10**11)) to verify that your hard-termination fallback triggers correctly.