Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/PyEventLoop.cc
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ PyEventLoop::AsyncHandle PyEventLoop::enqueue(PyObject *jobFn) {
// Enqueue job to the Python event-loop
// https://docs.python.org/3/library/asyncio-eventloop.html#asyncio.loop.call_soon
PyObject *asyncHandle = PyObject_CallMethod(_loop, "call_soon_threadsafe", "O", wrapper);
Py_DECREF(wrapper); // the Handle owns the wrapper now; release our ref so jobFn is freed after the job runs
return PyEventLoop::AsyncHandle(asyncHandle);
}

Expand All @@ -82,6 +83,7 @@ static PyObject *_enqueueWithDelay(PyObject *_loop, PyEventLoop::AsyncHandle::id
// Schedule job to the Python event-loop
// https://docs.python.org/3/library/asyncio-eventloop.html#asyncio.loop.call_later
PyObject *asyncHandle = PyObject_CallMethod(_loop, "call_later", "dOOIdb", delaySeconds, wrapper, _loop, handleId, delaySeconds, repeat); // https://docs.python.org/3/c-api/arg.html#c.Py_BuildValue
Py_DECREF(wrapper); // the TimerHandle now owns the wrapper; release our creation ref
if (!asyncHandle) {
return nullptr; // RuntimeError
}
Expand Down
18 changes: 18 additions & 0 deletions tests/python/test_event_loop.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import pytest
import pythonmonkey as pm
import asyncio
import gc


def test_setTimeout_unref():
Expand Down Expand Up @@ -433,3 +434,20 @@ async def async_fn():
# making sure the async_fn is run
return True
assert asyncio.run(async_fn())


def test_promise_jobs_release_job_wrappers():
schedule = pm.eval("() => Promise.resolve().then(() => 0)")
awaits = 50

async def drive():
for _ in range(awaits):
await schedule()
return sum(1 for o in gc.get_objects()
if getattr(o, "__name__", "") == "eventLoopJobWrapper")

leaked = asyncio.run(drive())
assert leaked == 0, (
f"{leaked} eventLoopJobWrapper objects retained after {awaits} awaits; "
f"each settled promise job's wrapper must be released once the job has run"
)