Skip to content

Bug: Async JS Promise cannot find running Python event loop on Python 3.13 (Windows) #507

Open

Description

@LetuGitHub

Issue type

Bug

How did you install PythonMonkey?

None

OS platform and distribution

Windows 11 x64

Python version (python --version)

3.13.0

PythonMonkey version (pip show pythonmonkey)

1.3.1

Bug Description

When awaiting a JavaScript async function from Python using asyncio.run(), PythonMonkey raises:

RuntimeError: PythonMonkey cannot find a running Python event-loop to make asynchronous calls.

However, a Python event loop is clearly running and can be retrieved with:

print(asyncio.get_running_loop())

which outputs:

<ProactorEventLoop running=True closed=False debug=False>

This suggests that PythonMonkey is unable to access the currently running event loop internally, even though one exists.

Standalone code to reproduce the issue

import asyncio
import pythonmonkey as pm

async def async_fn():
    print(asyncio.get_running_loop())

    await pm.eval("""
        new Promise((resolve) => setTimeout((...args) => {
            console.log(args);
            resolve();
        }, 1000, 42, "abc")
        )
    """)
    await pm.eval("async (x) => await x")(asyncio.sleep(0.5))

asyncio.run(async_fn())

Relevant log output or backtrace

await pm.eval("""
          ~~~~~~~^^^^
        new Promise((resolve) => setTimeout((...args) => {
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    ...<3 lines>...
        )
        ^
    """)
    ^^^^
RuntimeError: PythonMonkey cannot find a running Python event-loop to make asynchronous calls

Additional info if applicable

What branch of PythonMonkey were you developing on? (If applicable)

No response

Activity

  1. wiwichips commented on May 28, 2026

    @wiwichips
    Collaborator

    I assume changing it to the following would make it work?

    await pm.eval("""
          () => {
            new Promise((resolve) => setTimeout((...args) => {
                console.log(args);
                resolve();
            }, 1000, 42, "abc")
            )
          }
        """)()

    lmk if I'm wrong, I didn't test it...

  2. LetuGitHub commented on May 29, 2026

    @LetuGitHub
    Author

    我猜改成以下内容会让它正常工作?

    await pm.eval("""
    () => {
    new Promise((resolve) => setTimeout((...args) => {
    console.log(args);
    resolve();
    }, 1000, 42, "abc")
    )
    }
    """)()
    如果我错了请告诉我,我没测试过......

    Thanks. I tested the snippet you provided, but I still get the same error.

        await pm.eval("""
              ~~~~~~~~~~~
        ...<6 lines>...
          }
          ~
        """)()
        ~~~~^^
    RuntimeError: PythonMonkey cannot find a running Python event-loop to make asynchronous calls.
  3. wiwichips commented on May 29, 2026

    @wiwichips
    Collaborator

    FWIW - I can't reproduce this issue with python 3.12.8 and pm 1.3.1 on macos

    wkp@m2~$ cat ex.py 
    import pythonmonkey as pm
    import asyncio
    
    async def async_fn():
        print(asyncio.get_running_loop())
    
        # initial bug report repro
        await pm.eval("""
            new Promise((resolve) => setTimeout((...args) => {
                console.log(args);
                resolve();
            }, 1000, 42, "abc")
            )
        """)
        await pm.eval("async (x) => await x")(asyncio.sleep(0.5))
    
    
        # other way
        await pm.eval("""
          () => {
            return new Promise((resolve) => setTimeout((...args) => {
                console.log(args);
                resolve();
            }, 1000, 42, "abc")
            )
          }
        """)()
    
    asyncio.run(async_fn())
    
    
    wkp@m2~$ python3 ex.py 
    <_UnixSelectorEventLoop running=True closed=False debug=False>
    [ 42, 'abc' ]
    [ 42, 'abc' ]
    
    
    wkp@m2~$ python3 --version 
    Python 3.12.8
    
    
    wkp@m2~$ pip show pythonmonkey
    Name: pythonmonkey
    Version: 1.3.1
    Summary: Seamless interop between Python and JavaScript.
    Home-page: https://pythonmonkey.io/
    Author: Distributive Corp.
    Author-email: pm@distributive.network
    License: MIT
    Location: /Users/wkp/.local/share/mise/installs/python/3.12.8/lib/python3.12/site-packages
    Requires: aiohttp, pminit
    Required-by:

    Will try 3.13 on windows later...

  4. LetuGitHub commented on May 30, 2026

    @LetuGitHub
    Author

    FWIW - I can't reproduce this issue with python 3.12.8 and pm 1.3.1 on macos

    wkp@m2~$ cat ex.py
    import pythonmonkey as pm
    import asyncio

    async def async_fn():
    print(asyncio.get_running_loop())

    # initial bug report repro
    await pm.eval("""
        new Promise((resolve) => setTimeout((...args) => {
            console.log(args);
            resolve();
        }, 1000, 42, "abc")
        )
    """)
    await pm.eval("async (x) => await x")(asyncio.sleep(0.5))
    
    
    # other way
    await pm.eval("""
      () => {
        return new Promise((resolve) => setTimeout((...args) => {
            console.log(args);
            resolve();
        }, 1000, 42, "abc")
        )
      }
    """)()
    

    asyncio.run(async_fn())

    wkp@m2~$ python3 ex.py
    <_UnixSelectorEventLoop running=True closed=False debug=False>
    [ 42, 'abc' ]
    [ 42, 'abc' ]

    wkp@m2~$ python3 --version
    Python 3.12.8

    wkp@m2~$ pip show pythonmonkey
    Name: pythonmonkey
    Version: 1.3.1
    Summary: Seamless interop between Python and JavaScript.
    Home-page: https://pythonmonkey.io/
    Author: Distributive Corp.
    Author-email: pm@distributive.network
    License: MIT
    Location: /Users/wkp/.local/share/mise/installs/python/3.12.8/lib/python3.12/site-packages
    Requires: aiohttp, pminit
    Required-by:
    Will try 3.13 on windows later...

    Thanks for looking into this. I'm currently waiting for the Windows test results. Please let me know what you find.

  5. LetuGitHub commented on May 30, 2026

    @LetuGitHub
    Author

    After switching to Python 3.13.13, the issue disappeared and it now returns the results correctly.

    import asyncio
    import pythonmonkey as pm
    
    async def async_fn():
        await pm.eval("""
            new Promise((resolve) => setTimeout((...args) => {
                console.log(args);
                resolve();
            }, 1000, 42, "abc"))
        """)
    
    asyncio.run(async_fn())
    $ python --version
    Python 3.13.13
    
    $ python .\demo.py
    [ 42, 'abc' ]
    
    $ pip show pythonmonkey
    
    Name: pythonmonkey
    Version: 1.3.1
    Summary: Seamless interop between Python and JavaScript.
    Home-page: https://pythonmonkey.io/
    Author: Distributive Corp.
    Author-email: pm@distributive.network
    License: MIT
    Location: c:\Users\13944\Desktop\program\python\Lecourt\.venv\Lib\site-packages
    Requires: aiohttp, pminit, pyreadline3
    Required-by:
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions