Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Interrupt handling for wasm32-emscripten builds without pthreads #91332

Closed
hoodmane mannequin opened this issue Mar 30, 2022 · 3 comments
Closed

Interrupt handling for wasm32-emscripten builds without pthreads #91332

hoodmane mannequin opened this issue Mar 30, 2022 · 3 comments
Labels
3.11 bug and security fixes interpreter-core (Objects, Python, Grammar, and Parser dirs) type-feature A feature request or enhancement

Comments

@hoodmane
Copy link
Mannequin

hoodmane mannequin commented Mar 30, 2022

BPO 47176
Nosy @brettcannon, @tiran, @hoodmane
PRs
  • bpo-47176: Interrupt handling for wasm32-emscripten builds without pthreads #32209
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = None
    closed_at = <Date 2022-04-03.21:04:04.313>
    created_at = <Date 2022-03-30.21:55:22.259>
    labels = ['interpreter-core', 'type-feature', '3.11']
    title = 'Interrupt handling for wasm32-emscripten builds without pthreads'
    updated_at = <Date 2022-04-03.21:04:04.312>
    user = 'https://github.com/hoodmane'

    bugs.python.org fields:

    activity = <Date 2022-04-03.21:04:04.312>
    actor = 'christian.heimes'
    assignee = 'none'
    closed = True
    closed_date = <Date 2022-04-03.21:04:04.313>
    closer = 'christian.heimes'
    components = ['Interpreter Core']
    creation = <Date 2022-03-30.21:55:22.259>
    creator = 'hoodmane'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 47176
    keywords = ['patch']
    message_count = 3.0
    messages = ['416403', '416644', '416645']
    nosy_count = 3.0
    nosy_names = ['brett.cannon', 'christian.heimes', 'hoodmane']
    pr_nums = ['32209']
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'enhancement'
    url = 'https://bugs.python.org/issue47176'
    versions = ['Python 3.11']

    @hoodmane
    Copy link
    Mannequin Author

    hoodmane mannequin commented Mar 30, 2022

    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).

    @hoodmane hoodmane mannequin added 3.10 only security fixes interpreter-core (Objects, Python, Grammar, and Parser dirs) type-feature A feature request or enhancement 3.11 bug and security fixes and removed 3.10 only security fixes labels Mar 30, 2022
    @tiran
    Copy link
    Member

    tiran commented Apr 3, 2022

    New changeset 087d0fa by Hood Chatham in branch 'main':
    bpo-47176: Interrupt handling for wasm32-emscripten builds without pthreads (GH-32209)
    087d0fa

    @tiran
    Copy link
    Member

    tiran commented Apr 3, 2022

    Thanks for your patch!

    @tiran tiran closed this as completed Apr 3, 2022
    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    3.11 bug and security fixes interpreter-core (Objects, Python, Grammar, and Parser dirs) type-feature A feature request or enhancement
    Projects
    None yet
    Development

    No branches or pull requests

    1 participant