This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Interrupt handling for wasm32-emscripten builds without pthreads
Type: enhancement Stage: resolved
Components: Interpreter Core Versions: Python 3.11
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: brett.cannon, christian.heimes, hoodmane
Priority: normal Keywords: patch

Created on 2022-03-30 21:55 by hoodmane, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 32209 merged hoodmane, 2022-03-31 05:00
Messages (3)
msg416403 - (view) Author: Hood Chatham (hoodmane) Date: 2022-03-30 21:55
In Pyodide, we need to patch the interpreter to allow keyboard interrupts. We build Python without pthreads support because Emscripten doesn't currently support using pthreads and dynamic linking in the same build. It is still possible to handle UI at the same time as running Python code by running Python in a web worker. However, the web assembly memory is private to the webworker and cannot be modified from the main thread. The standard way that keyboard interrupts are handled is that the operating system preempts the task to run a signal handler. This can't happen in Emscripten because there is no operating system.

Instead, in Emscripten we create a SharedArrayBuffer, share it with the main thread, and then write the signal into this shared memory from the main thread. We patch the main loop to periodically poll this SharedArrayBuffer and if an interrupt has been requested it calls `PyErr_SetInterruptEx` to signal the interrupt. I've set the polling rate to once every 50 interpreter operations, which seems to be reasonably responsive and have a negligible performance cost. 

One interesting feature of this setup is that it is impossible to create a pointer to the shared memory so it cannot be read directly from C (instead we check it from an `EM_ASM` block).
msg416644 - (view) Author: Christian Heimes (christian.heimes) * (Python committer) Date: 2022-04-03 20:59
New changeset 087d0fa5b97796560c0d8ceab4f0360fd54baf4f by Hood Chatham in branch 'main':
bpo-47176: Interrupt handling for wasm32-emscripten builds without pthreads (GH-32209)
https://github.com/python/cpython/commit/087d0fa5b97796560c0d8ceab4f0360fd54baf4f
msg416645 - (view) Author: Christian Heimes (christian.heimes) * (Python committer) Date: 2022-04-03 21:04
Thanks for your patch!
History
Date User Action Args
2022-04-11 14:59:57adminsetgithub: 91332
2022-04-03 21:04:04christian.heimessetstatus: open -> closed
resolution: fixed
messages: + msg416645

stage: patch review -> resolved
2022-04-03 20:59:23christian.heimessetmessages: + msg416644
2022-03-31 17:55:08brett.cannonsetnosy: + brett.cannon
2022-03-31 08:33:58christian.heimeslinkissue40280 dependencies
2022-03-31 08:33:38christian.heimessetnosy: + christian.heimes
2022-03-31 05:01:52hoodchathamsetversions: + Python 3.11
2022-03-31 05:01:22hoodchathamsetversions: - Python 3.10
2022-03-31 05:00:07hoodmanesetkeywords: + patch
stage: patch review
pull_requests: + pull_request30284
2022-03-30 21:55:22hoodmanecreate