fix: capture init task ref + cancel on close for Neo4jDriver and FalkorDriver#1636
Open
nandanadileep wants to merge 2 commits into
Open
fix: capture init task ref + cancel on close for Neo4jDriver and FalkorDriver#1636nandanadileep wants to merge 2 commits into
nandanadileep wants to merge 2 commits into
Conversation
…orDriver Both Neo4jDriver and FalkorDriver __init__ scheduled build_indices_and_constraints() via loop.create_task() without capturing the task reference. The orphan task could: 1. Race with explicit build_indices_and_constraints() calls 2. Produce 'Task exception was never retrieved' warnings on failure 3. Leave the connection in a mid-transaction state when the driver is closed concurrently Now captures the task in self._init_task and cancels it in close(), with suppress(CancelledError) to handle the cancellation cleanly. Closes getzep#1513
Contributor
|
All contributors have signed the CLA ✍️ ✅ |
Author
|
I have read the CLA Document and I hereby sign the CLA behalf on myself, e-mail: nandanadileep29@gmail.com |
Contributor
|
@nandanadileep Thanks for catching this! One gap: close() skips done() tasks, so a failed init task still leaks its exception. Calling task.exception() on done tasks too would complete it. |
This was referenced Jul 20, 2026
Previously, close() only cancelled and awaited tasks that were still running, but skipped already-done tasks. If a done task failed with an exception, the exception was never retrieved, causing 'Task exception was never retrieved' warnings. Now calls task.exception() on done tasks to consume any pending exception and complete the task's lifecycle.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Both
Neo4jDriver.__init__andFalkorDriver.__init__schedulebuild_indices_and_constraints()vialoop.create_task()without capturing the task reference. This creates a fire-and-forget background task whose lifecycle is not managed.Problem
await driver.build_indices_and_constraints()race with the constructor-fired task, producingEquivalentSchemaRuleAlreadyExistserrors.Fix
self._init_task: asyncio.Task | Noneclose()\, cancel the task if still running and await it withsuppress(asyncio.CancelledError)import asyncioto the top ofneo4j_driver.pyTesting
make lintpassesmake typecheckpassesDISABLE_FALKORDB=1 DISABLE_KUZU=1 DISABLE_NEPTUNE=1 uv run pytest -m 'not integration'— 165 passed, 2 skipped (FalkorDB integration), 1 pre-existing Neo4j auth error (no local Neo4j)Closes #1513