Skip to content

Fix UNPREPARED handling in ResponseFuture to allow recovery on query ID mismatch#726

Draft
Copilot wants to merge 2 commits intoscylladb-27657-v2from
copilot/sub-pr-725
Draft

Fix UNPREPARED handling in ResponseFuture to allow recovery on query ID mismatch#726
Copilot wants to merge 2 commits intoscylladb-27657-v2from
copilot/sub-pr-725

Conversation

Copy link

Copilot AI commented Mar 4, 2026

The original PreparedQueryNotFound handler crashed with AssertionError when the server returned a query ID that differed from the local prepared_statement.query_id, permanently blocking reprepare/retry even when enough context existed to recover.

Root cause

# Original code — asserts IDs always match, but coordinator races/restarts can break this
if self.prepared_statement:
    query_id = self.prepared_statement.query_id
    assert query_id == response.info, "Got different query ID..."  # AssertionError → no recovery

Two additional defects in the same block:

  • query_id.encode('hex') — Python 2 syntax, raises AttributeError in Python 3.
  • Direct _prepared_statements[query_id] = ... — not thread-safe; bypasses _prepared_statement_lock.

Changes

  • cassandra/cluster.py — Remove the assertion; use server-returned ID for cache lookup first, fall back to in-flight prepared_statement when the lookup misses; use add_prepared() for thread-safe cache writes; fix log calls to hexlify(...).decode('ascii') for clean string output.
  • tests/unit/test_response_future.py — Add test_prepared_query_not_found_uses_local_prepared_context (regression: raises AssertionError on old code when server returns a different query ID) and test_prepared_query_not_found_prefers_returned_id_from_cache (validates cache-resolved statement takes precedence over local context). Docstring on the regression test explicitly documents the pre-fix failure mode.

Pre-review checklist

  • I have split my patch into logically separate commits.
  • All commit messages clearly explain what they change and why.
  • I added relevant tests for new features and bug fixes.
  • All commits compile, pass static checks and pass test.
  • PR description sums up the changes and reasons why they should be introduced.
  • I have provided docstrings for the public items that I want to introduce.
  • I have adjusted the documentation in ./docs/source/.
  • I added appropriate Fixes: annotations to PR description.

🔒 GitHub Advanced Security automatically protects Copilot coding agent pull requests. You can protect all pull requests by enabling Advanced Security for your repositories. Learn more about Advanced Security.

…QueryNotFound

Co-authored-by: mykaul <4655593+mykaul@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix UNPREPARED handling in the Python driver execution path Fix UNPREPARED handling in ResponseFuture to allow recovery on query ID mismatch Mar 4, 2026
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.

2 participants