{"record":{"id":"f272209e423dd6b5","repo":"apache/cassandra","slug":"querycancelledexception","errorCode":null,"errorMessage":"QueryCancelledException","messagePattern":"QueryCancelledException","errorType":"exception","errorClass":"QueryCancelledException","httpStatus":null,"severity":"warning","filePath":"src/java/org/apache/cassandra/db/ReadCommand.java","lineNumber":892,"sourceCode":"            maybeCancel();\n            return row;\n        }\n\n        private void maybeCancel()\n        {\n            /*\n             * The value returned by approxTime.now() is updated only every\n             * {@link org.apache.cassandra.utils.MonotonicClock.SampledClock.CHECK_INTERVAL_MS}, by default 2 millis.\n             * Since MonitorableImpl relies on approxTime, we don't need to check unless the approximate time has elapsed.\n             */\n            if (lastCheckedAt == approxTime.now())\n                return;\n            lastCheckedAt = approxTime.now();\n\n            if (isAborted())\n            {\n                stop();\n                throw new QueryCancelledException(ReadCommand.this);\n            }\n        }\n    }\n\n    private UnfilteredPartitionIterator withQueryCancellation(UnfilteredPartitionIterator iter)\n    {\n        return Transformation.apply(iter, new QueryCancellationChecker());\n    }\n\n    /**\n     *  A transformation used for simulating slow queries by tests.\n     */\n    @VisibleForTesting\n    private static class DelayInjector extends Transformation<UnfilteredRowIterator>\n    {\n        @Override\n        protected UnfilteredRowIterator applyToPartition(UnfilteredRowIterator partition)\n        {","sourceCodeStart":874,"sourceCodeEnd":910,"githubUrl":"https://github.com/apache/cassandra/blob/88fd0f6a0eaed8943f05ac9e8f947882b8ddc8f1/src/java/org/apache/cassandra/db/ReadCommand.java#L874-L910","documentation":"ReadCommand's ClientRequest / execution controller checks an approximate-time deadline; if the command has been aborted (e.g. request timeout elapsed or a client disconnected and abort() was called), the iterator is stopped and QueryCancelledException is thrown. It converts an externally requested cancellation into a distinct, catchable failure instead of returning partial results.","triggerScenarios":"A read that exceeds the client's request timeout (driver/socket read timeout) or whose client disconnects, so the coordinator aborts the command; the next iterator advance notices isAborted() and throws.","commonSituations":"Slow reads (huge scans, tombstone storms) exceeding the driver's read timeout; application shutting down/cancelling futures while a query is in flight; load-shedding under overload.","solutions":["Increase the client-side read/request timeout or reduce query size (LIMIT, paging).","Retry with exponential backoff if the query was cancelled due to overload; investigate underlying read latency.","Address root causes: run compaction, check tombstone counts, avoid unbounded queries.","Ensure driver consistency: don't close sessions/cluster while queries are pending."],"exampleFix":"// before\nclusterBuilder.withSocketOptions(opts.setReadTimeoutMillis(2000)); // too low for scans\n// after\nclusterBuilder.withSocketOptions(opts.setReadTimeoutMillis(20000)); // plus paged queries with LIMIT","handlingStrategy":"retry","validationCode":"// Set driver read timeout comfortably above p99 query latency measured in monitoring.","typeGuard":null,"tryCatchPattern":"try { rs = session.execute(query); } catch (DriverException e) {\n    if (isCancellation(e)) { if (attempt < MAX_RETRY) retryWithBackoff(); else fail(); } else throw e;\n}","preventionTips":["Keep queries under the client read timeout: page, limit, index.","Never close sessions/cluster while queries are in flight.","Investigate read-latency regressions (compaction, GC, tombstones) promptly.","Use idempotent queries so retries are safe."],"tags":["query-cancelled","timeout","read-path","cancellation"],"backgroundTag":"request-timeout","analyzedSha":"88fd0f6a0eaed8943f05ac9e8f947882b8ddc8f1","analyzedAt":"2026-09-10T07:29:22.284Z","contentChangedAt":"2026-09-10T07:29:22.284Z","schemaVersion":2},"datasetVersion":"2026-09-14T11:17:12.474Z"}