Skip to content

test(llm): stop leaking un-awaited mock coroutines in builder tests - #425

Open
lplalbert wants to merge 1 commit into
apache:mainfrom
lplalbert:test/builder-mock-coroutine-leak
Open

lplalbert wants to merge 1 commit into
apache:mainfrom
lplalbert:test/builder-mock-coroutine-leak

Conversation

@lplalbert

Copy link
Copy Markdown
Contributor

Problem

index_op test runs emit "coroutine ... was never awaited" RuntimeWarnings (3 per run, surfaced at GC).

Root cause

patch() creates an AsyncMock when its target is an async function. The builder tests mock both get_embeddings_parallel (async) and asyncio.run — calling the auto-AsyncMock produces a coroutine, and the mocked asyncio.run discards that call result instead of awaiting it, leaving the coroutine un-awaited behind.

Fix

Force new_callable=MagicMock for these mocks so the mocked call returns a plain value — exactly the pattern test_build_gremlin_example_index already uses for the same double-mock structure (the working comparison). Assertions and coverage are unchanged.

Sibling search (same failure class elsewhere): only test_build_semantic_index's two run() tests (patch.object on the async _get_embeddings_parallel + mocked asyncio.run) — fixed here too. Note PR #424 rewrites those same tests and drops the asyncio.run mock entirely (which also removes the leak there); whichever lands second rebases mechanically — same root cause grouped here.

Validation (executed locally)

  • PYTHONTRACEMALLOC=2 ... pytest hugegraph-llm/src/tests/operators/index_op/ hugegraph-llm/src/tests/nodes/index_node/ -> 3 "was never awaited" warnings before, 0 after (traces attribute every instance to the patched async helper called under the mocked asyncio.run)
  • SKIP_EXTERNAL_SERVICES=true uv run pytest hugegraph-llm/src/tests -> 1289 passed, 12 skipped, 2 failed — the two test_external_config.py failures also fail on clean origin/main (verified via stash), pre-existing and unrelated
  • uv run --extra dev ruff format --check + ruff check clean; ty check on the changed files: all checks passed

Refs #107

🤖 Generated with Claude Code

patch() creates an AsyncMock when its target is an async function, so mocking
get_embeddings_parallel / _get_embeddings_parallel produced a coroutine per
call; these tests also mock asyncio.run, which discards that call's result and
leaves the coroutine un-awaited. pytest surfaced 'coroutine ... was never
awaited' RuntimeWarnings at GC (3 per index_op run).

Force MagicMock for these mocks - the pattern test_build_gremlin_example_index
already uses - so the mocked call returns a plain value. Assertions and coverage
are unchanged. Sibling search found no other occurrences: the gremlin builder
tests (new_callable=MagicMock) are the working comparison.

Validation: index_op + index_node runs go from 3 'was never awaited' warnings to
0 (PYTHONTRACEMALLOC attributes every instance to the patched async helper
called under the mocked asyncio.run); full suite 1289 passed, 12 skipped,
2 failed = the pre-existing test_external_config pair that also fails on
origin/main; ruff/ty clean on the changed files.

Co-Authored-By: Claude Code <noreply@anthropic.com>
@github-actions github-actions Bot added the llm label Nov 10, 2026

@bitflicker64 bitflicker64 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Blocking: no. Score: 9/10. Summary: Forcing new_callable=MagicMock on the patched async embedding helpers removes the 3 un-awaited AsyncMock coroutines in the index_op tests without changing assertions, matching test_build_gremlin_example_index.py.

@imbajin imbajin left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Score: 8.8/10 · Confidence: high

Blocking: no

Summary

The async helper mocks are used under mocked asyncio.run; returning plain values removes discarded coroutine objects without changing the existing assertions.

Evidence

Reviewed both changed test files and builder call paths; separate semantic helper tests exercise its async behavior. LLM Python 3.12 and quality CI succeeded.

Validation notes

Static review; the reported warning counts were not independently reproduced.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants