fix(graph): eliminate Kùzu memory exhaustion and teardown race condition - #95
Merged
Merged
Conversation
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.
Overview
Resolves production Profile B VM runtime failures where Kùzu graph retrieval causes memory exhaustion (OOM / high virtual memory) and teardown segmentation faults (use-after-free race condition) under timeout/cancellation.
Root Cause Analysis
q2_undirected,q3_undirected) constructed single-pass monolithic Cypher queries passing tens of thousands ofallowed_assertion_idsandallowed_entity_idsdirectly into CypherIN $allowed_...predicates.asyncio.wait_fortimed out, the Python coroutine was cancelled immediately while the native Kùzu C++ query was still executing in a background worker thread.close()teardown released the database and connection (_sync_close), causing the still-running native worker thread to access deallocated memory (use-after-free), triggering SIGSEGV.Key Changes
mesa_storage/kuzu_provider.py):_step_expand)._MAX_FRONTIER_NODES = 64and_MAX_FANOUT_PER_NODE = 32, preventing combinatorial explosion while preserving graph path discovery and ranking contracts.mesa_storage/kuzu_provider.py):_in_flight_native_query()context manager,_drain_event, and_shutting_downflag.close()rejects new queries and waits with a bounded timeout (_drain_event.wait(timeout)) for in-flight native C++ executions to complete before executing_sync_close()under_conn_lock.tests/test_v4_kuzu_runtime_closure.py):test_real_scale_3_hop_legal_graph_retrieval: Validates 3-hop traversal on realistic legal graph density under strict memory/time bounds.test_teardown_segfault_race_prevention: Recreates async timeout cancellation followed immediately byclose()to guarantee no segfault.test_shutdown_barrier_rejects_new_queries: Verifies query rejection during shutdown.test_kuzu_resource_exhaustion_typed_and_non_fatal: Verifies non-fatal typed error handling.test_server_survival_after_graph_outage: Verifies server resilience and fallback readiness.Verification
tests/test_v4_kuzu_runtime_closure.py: 5 passed in 15.6stests/test_v4_graph_retrieval_hardening.py: 16 passed in 9.5stests/test_v4_certification_contracts.py: 5 passed in 2.8stests/test_v4_hardening_final_verification.py: 9 passed in 1.9spy_compile) and linting (ruff): Clean