Skip to content

pycvc.ariadne's tests keep the tasks they start, so a garbage collection mid-request loses none - #1243

Merged
transfix merged 1 commit into
masterfrom
fix/ariadne-task-refs
Nov 15, 2026
Merged

transfix merged 1 commit into
masterfrom
fix/ariadne-task-refs

Conversation

@transfix

Copy link
Copy Markdown
Owner

On Windows, pycvc_ariadne_server (bindings/pycvc/test_pycvc_ariadne_server.py) hung until its timeout on every run from #1168 on. Each run printed Task was destroyed but it is pending! for its client() task. Windows, Winsock, asyncio's proactor and the GIL had nothing to do with it. The garbage collector freed a task that the test never kept.

Cause

The test started its HTTP client with a bare host.loop.create_task(client()) and kept no reference to the task.

  • asyncio's loop holds tasks only weakly.
  • A client of asyncio.open_connection waits on its StreamReader, and its protocol holds that reader only weakly too.

So while client() waits on await reader.read(), the only references to the task are a cycle of its own:

task -> coroutine frame -> reader -> reader._waiter (future) -> callback task.__wakeup -> task

The first full (generation-2) collection that runs during that wait frees the task. host.stop(7) never runs, and host.run() waits forever.

Whether a full collection lands in that wait depends on how many objects were allocated before it. That is why the hang moved with unrelated changes.

Run Live objects after import pycvc Full collections during the run Result
a286873 build 30,664 1, 31 ms into the client's wait (collected 11, the task among them) hangs
8857fe8 build (#1168's parent) 30,264 none before the test ends (gens run: [95, 8, 0]) passes, 2/2
a286873, unchanged test, N extra live objects allocated before import pycvc N ≤ 1000 hangs; N ≥ 1500 passes
a286873, gc.disable() at the top passes
a286873, the task kept in a variable passes

The other clues from the report fit the same way. runpy.run_path, and a sitecustomize without the importlib imports, each allocate a different number of objects before the test runs.

Bisect

#1168 is the commit that turns it on, but it is not the bug. Its input-trace wrappers (pycvc_trace.i) leave about 400 more objects at import pycvc, and that moves the run's first full collection into the client's wait.

The bug was there before #1168:

  • A gc.collect() while client() waits hangs the 8857fe8 build the same way.
  • A minimal asyncio program with no pycvc and no uvicorn (start_server, a client task nothing holds, one gc.collect() in the handler) loses its client the same way on Linux's _UnixSelectorEventLoop (Python 3.10, WSL) and on Windows' ProactorEventLoop (3.12). Keeping a reference fixes it on both.

Linux CI passes only because its allocation counts happen not to put a full collection in that wait.

Fix

  • test_pycvc_ariadne_server.py: keeps client_task. Its /ping handler now runs gc.collect(), so a full collection always falls inside the request. The test now checks that every task on the loop survives one, the host's tasks and its own. On every platform, the old test with that line added hangs.
  • The arisrv ctest programs, which used the same unheld idiom:
    • http_listen.py's selftest waits on a stream exactly as client() did. It now keeps its task, and its /ping runs gc.collect() too, so arisrvx_http_listen checks arisrvx's own path under a full collection.
    • service.py (arisrvx_serve): its selftest and its in-flight /slow request are kept in tasks.
    • program.py (arisrvx_program, and the arisrvx package's smoke test in publish-cvcpkg.yml) waits on asyncio.sleep, whose timer keeps its task alive, so it could not hang this way. It keeps its task anyway, because it is the example people copy.
  • Docs: the pycvc.ariadne module docstring and ARIADNE.md's script-thread bullet now say to keep the task host.loop.create_task() returns until it is done.

arisrvx's own serving path was already safe: _Program.spawn, _Served.task and uvicorn's per-connection tasks are all held. A packaged arisrvx serving HTTP does not hang this way. A service's own code would, on any platform, if it starts a task without keeping it and that task waits on a stream of its own, which is what the docs now warn about.

Verification

Windows 11, MSVC 14.44, static Release (pycvc core on, pycvc_gl off), Python 3.12.10. Built at this branch (master 4239741 + this commit):

Run Result
ctest pycvc_ariadne_server, this branch's test passed 3/3 (2.4–3.1 s)
master's test unchanged, same build hangs 2/2 (timeout, Task was destroyed but it is pending!)
master's test plus only the /ping gc.collect() (task not kept) hangs 2/2, deterministically
arisrvx --python http_listen.py --http-listen …, this branch's program passed (1 s)
the same program with its task not kept hangs 2/2 (Task was destroyed, no selftest line)
ctest -R '^(arisrvx_|pycvc_ariadne)' 25/25 passed

Linux has not been run here beyond the minimal asyncio repro. pycvc-linux and the arisrvx lanes on this PR are the check there.

…ion mid-request loses none

On Windows, pycvc_ariadne_server hung until its timeout on every run from #1168 on, with "Task was
destroyed but it is pending!" for its client() task. It was not Windows, Winsock or asyncio's
proactor: it was the garbage collector, and the test.

The test started client() with a bare host.loop.create_task(client()) and kept no reference. The
loop holds tasks only weakly, and a client of asyncio.open_connection waits on its StreamReader,
which its protocol holds only weakly too. So while client() waits for the response, the only
references to the task are a cycle of its own: task -> coroutine frame -> reader -> waiter future
-> the task's wakeup callback. The first full (generation 2) collection that runs in that wait
frees the task. stop(7) never runs, and host.run() waits forever.

Whether a full collection falls in that wait is set by how many objects were allocated before it.
On a286873 the first one of the run falls 30 ms into the wait. #1168 is the commit that moves it
there: its input-trace wrappers (pycvc_trace.i) leave about 400 more objects at import pycvc (30,264
-> 30,664). On its parent, 8857fe8, no full collection runs before the test ends, so it passed. The
bug was there before: a gc.collect() while client() waits hangs 8857fe8 the same way. A minimal
asyncio program with no pycvc (start_server, a client task nothing holds, one gc.collect() in the
handler) loses its client the same way on Linux's selector loop and on Windows' proactor. And
padding the unchanged test with 1,500 extra objects before import pycvc makes it pass on a286873
again. The other clues fit as well: runpy.run_path, and a sitecustomize without
the importlib imports, each allocate a different number of objects.

The fix: the test keeps client_task. Its /ping handler now runs gc.collect(), so a full collection
always falls in the request and the test checks that every task on the loop survives one, the
host's and its own. Before this fix that hangs on every platform, not only where the counts happen
to land in the wait.

arisrvx's own path was already safe: _Program.spawn, _Served.task and uvicorn's per-connection
tasks are all held. The arisrv ctest programs used the same unheld idiom. http_listen.py's selftest
waits on a stream exactly as client() did, and so do service.py's selftest and its /slow request.
Each now keeps its tasks, and http_listen.py's /ping runs gc.collect() too, so arisrvx_http_listen
checks the arisrvx side under a full collection. program.py (arisrvx_program, the arisrvx package's
smoke test) waits on asyncio.sleep, whose timer keeps the task alive; it keeps its task as well, as
an example to copy.

The module docstring and ARIADNE.md's script-thread bullet now say to keep the task
host.loop.create_task() returns: a service that starts one without keeping it, and waits on a stream
of its own, loses it the same way on any platform.
@transfix
transfix enabled auto-merge (squash) November 14, 2026 22:08
@transfix
transfix merged commit 336e728 into master Nov 15, 2026
28 checks passed
@transfix
transfix deleted the fix/ariadne-task-refs branch November 15, 2026 06:05
transfix added a commit that referenced this pull request Nov 15, 2026
#1243 added a sentence to ARIADNE.md's script-thread bullet, on the line
just before the Writes bullet this branch edits. The resolution keeps
#1243's sentence, and the Writes bullet names pycvc.ari.Host.
transfix added a commit that referenced this pull request Nov 15, 2026
- app_runtime.cpp: the constructor hands the scene its repeat: count function (#1230), then
  takes the verb table's lock as before.
- e2ee_intrinsics.cpp (#1191): the e2ee-* provider registers its async functions on the calling
  program's host (*ictx.host, else the app's), as every provider on this branch does (B30), so an
  isolated document's e2ee-backup and e2ee-restore park on its own group.
- STATE_EXEC_THREADED.md: the e2ee provider in the provider table and the file list.
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.

1 participant