Skip to content

Fix segfault awaiting JS promises on Python 3.14.0–3.14.3 - #516

Draft
Xmader with Copilot wants to merge 2 commits into
mainfrom
copilot/segfault-access-violation-fix
Draft

Xmader with Copilot wants to merge 2 commits into
mainfrom
copilot/segfault-access-violation-fix

Conversation

Copilot AI commented Nov 9, 2026 •

Copy link
Copy Markdown
Contributor

PyEventLoop::_getLoopOnThread read asyncio_running_loop from a local copy of CPython's private _PyThreadStateImpl at a compiled-in offset. Python 3.14.4 added a pointer to the public PyThreadState, so a wheel built against 3.14.4+ headers reads the neighbouring asyncio_running_task on 3.14.0–3.14.3. The Task is then treated as the loop, and awaiting any JS promise crashes. The reverse mismatch would break the same way.

  • Current thread: _getLoopOnThread now calls asyncio._get_running_loop() instead of reading the struct. This fixes the crash path and no longer depends on the patch version. It applies to all Python versions, not only 3.14.
  • Other threads: getMainLoop() from a helper thread still needs the struct read, because the Python API only works for the current thread. For 3.14 builds, the runtime Py_Version is now compared with the build-time PY_VERSION_HEX across the 3.14.4 boundary. If they straddle it, the read is skipped and the call returns the usual "cannot find a running Python event-loop" error instead of reading the wrong field.
import asyncio, pythonmonkey as pm

async def main():
    return await pm.eval('Promise.resolve(1)')

print(asyncio.run(main()))  # segfault / access violation on 3.14.0–3.14.3 before; prints 1.0

For review:

  • I did not cache the main loop, because a cached loop goes stale across repeated asyncio.run calls.
  • On a layout mismatch, helper threads calling getMainLoop() now get an error rather than a crash.
  • This has not been built or tested yet, and no regression test is added, since reproducing needs a 3.14.0–3.14.3 interpreter.

Copilot AI changed the title [WIP] Fix segfault and access violation on Python 3.14.0–3.14.3 Fix segfault awaiting JS promises on Python 3.14.0–3.14.3 Nov 9, 2026
Copilot AI requested a review from Xmader November 9, 2026 16:09

This branch has not been deployed

No deployments
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.

Segfault / access violation awaiting any JS promise on Python 3.14.0–3.14.3 (thread-state offset from 3.14.4+ headers)

2 participants