Skip to content

fix: return "Session is closed" instead of NoNodeAvailableException during shutdown - #1023

Open
quantumsoul wants to merge 1 commit into
scylladb:scylla-4.xfrom
quantumsoul:fix/session-closed-error-during-shutdown
Open

fix: return "Session is closed" instead of NoNodeAvailableException during shutdown#1023
quantumsoul wants to merge 1 commit into
scylladb:scylla-4.xfrom
quantumsoul:fix/session-closed-error-during-shutdown

Conversation

@quantumsoul

Copy link
Copy Markdown

Motivation

CqlSession request handlers currently surface NoNodeAvailableException when a request happens to be in flight while the session is shutting down. This is misleading — no node was even attempted; the load balancing policy simply returned an empty query plan because it had already transitioned to CLOSING. See #846 for the report and the exact stack trace.

The existing DefaultSession.execute() and the request handler's timer path already surface IllegalStateException("Session is closed") for this situation. The shutdown-race path should match.

Modifications

  • Added DefaultSession.queryPlanExhaustedError(session, errors) — when the errors list is empty and the session is either closing or already closed, returns IllegalStateException("Session is closed"); otherwise delegates to AllNodesFailedException.fromErrors(errors) as before.
  • Added DefaultSession.isClosing(), backed by a volatile boolean closing flipped synchronously at the top of closeAsync() / forceCloseAsync(). isClosed() alone would miss the window between close initiation and closeFuture completion — exactly when the LB policy starts returning empty plans.
  • Routed CqlRequestHandler, CqlPrepareHandler, GraphRequestHandler, and ContinuousRequestHandlerBase through the helper at their "query plan exhausted" branches.
  • Two new unit tests in CqlRequestHandlerTest pin down each branch of the helper: isClosed() (post-shutdown-complete) and isClosing() (shutdown-in-progress).

Result

  • Requests that reach a handler with an empty query plan during shutdown now fail with IllegalStateException("Session is closed"), matching the error already produced elsewhere for the same condition.
  • Genuine "no node available" outages on a healthy session remain unchanged (non-empty per-node errors still produce AllNodesFailedException).
  • No public API changes on CqlSession / AsyncAutoCloseable; isClosing() is a new method on the internal DefaultSession only.

Fixes #846

…uring shutdown

Route the four request handlers (CqlRequestHandler, CqlPrepareHandler, GraphRequestHandler, ContinuousRequestHandlerBase) through a new helper DefaultSession.queryPlanExhaustedError. When the errors list is empty and the session is closing or already closed, it returns IllegalStateException("Session is closed") -- matching what DefaultSession.execute() and the timer path already do for shutdown -- instead of the misleading NoNodeAvailableException.

Shutdown-in-progress is detected via a new isClosing() predicate on DefaultSession, set synchronously when closeAsync/forceCloseAsync is invoked. Relying solely on isClosed() would have missed the window between close initiation and closeFuture completion -- exactly when the load balancing policy starts returning empty query plans.

Genuine "no node available" outages on a healthy session are unchanged.

Fixes scylladb#846
@coderabbitai

coderabbitai Bot commented Aug 29, 2026

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: QUIET

Plan: Pro Plus

Run ID: 8a27cd91-899b-45bd-8d6b-2882c87319ed

📥 Commits

Reviewing files that changed from the base of the PR and between d8f6dd3 and d04f37d.

📒 Files selected for processing (6)
  • core/src/main/java/com/datastax/dse/driver/internal/core/cql/continuous/ContinuousRequestHandlerBase.java
  • core/src/main/java/com/datastax/dse/driver/internal/core/graph/GraphRequestHandler.java
  • core/src/main/java/com/datastax/oss/driver/internal/core/cql/CqlPrepareHandler.java
  • core/src/main/java/com/datastax/oss/driver/internal/core/cql/CqlRequestHandler.java
  • core/src/main/java/com/datastax/oss/driver/internal/core/session/DefaultSession.java
  • core/src/test/java/com/datastax/oss/driver/internal/core/cql/CqlRequestHandlerTest.java

Included review availability: Your plan provides up to 1 included review per hour; 0 remain after this review.


📝 Walkthrough

Walkthrough

DefaultSession now tracks shutdown before closure completes and provides queryPlanExhaustedError. Request handlers use this helper when query plans are exhausted. Empty query plans during session closure or after closure now produce IllegalStateException("Session is closed"). Existing per-node errors continue through AllNodesFailedException.fromErrors. Tests cover both closed and closing sessions.

Suggested reviewers: dkropachev, nikagra

Merge Risk: ⚪ Minimal · up to d04f3

Requests that race with session shutdown now receive the intended "Session is closed" error, while genuine node-availability failures remain unchanged; no actionable merge-blocking risk remains beyond normal checks and review.

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 46.67% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 15 functions across 6 files. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and concisely describes the shutdown error-handling change.
Description check ✅ Passed The description directly explains the shutdown race, implementation, affected handlers, tests, and expected error behavior.
Linked Issues check ✅ Passed The changes satisfy issue #846 by returning IllegalStateException("Session is closed") for empty query plans during shutdown while preserving AllNodesFailedException for genuine node-availability fail…
Out of Scope Changes check ✅ Passed The modified handlers, DefaultSession state, helper method, and tests are directly related to the shutdown error-handling objective. No unrelated code changes are identified.
Full details: Linked Issues check

Explanation

The changes satisfy issue #846 by returning IllegalStateException("Session is closed") for empty query plans during shutdown while preserving AllNodesFailedException for genuine node-availability failures.

  • Fix all pre-merge checks with AI

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

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.

Check if session is closed to handle errors properly

1 participant