{"record":{"id":"97b9ecdfe6e1b2d3","repo":"apache/cassandra","slug":"attempted-to-seek-in-a-closed-rar","errorCode":null,"errorMessage":"Attempted to seek in a closed RAR","messagePattern":"Attempted to seek in a closed RAR","errorType":"exception","errorClass":"IllegalStateException","httpStatus":null,"severity":"error","filePath":"src/java/org/apache/cassandra/io/util/RandomAccessReader.java","lineNumber":224,"sourceCode":"     */\n    private static class BufferedRandomAccessFileMark implements DataPosition\n    {\n        final long pointer;\n\n        private BufferedRandomAccessFileMark(long pointer)\n        {\n            this.pointer = pointer;\n        }\n    }\n\n    @Override\n    public void seek(long newPosition)\n    {\n        if (newPosition < 0)\n            throw new IllegalArgumentException(\"new position should not be negative\");\n\n        if (buffer == null)\n            throw new IllegalStateException(\"Attempted to seek in a closed RAR\");\n\n        long bufferOffset = bufferHolderOffset;\n        if (newPosition >= bufferOffset && newPosition < bufferOffset + buffer.limit())\n        {\n            buffer.position((int) (newPosition - bufferOffset));\n            return;\n        }\n\n        if (newPosition > length())\n            throw new IllegalArgumentException(String.format(\"Unable to seek to position %d in %s (%d bytes) in read-only mode\",\n                                                         newPosition, getPath(), length()));\n        reBufferAt(newPosition);\n    }\n\n    @Override\n    public int skipBytes(int n) throws IOException\n    {\n        if (n <= 0)","sourceCodeStart":206,"sourceCodeEnd":242,"githubUrl":"https://github.com/apache/cassandra/blob/88fd0f6a0eaed8943f05ac9e8f947882b8ddc8f1/src/java/org/apache/cassandra/io/util/RandomAccessReader.java#L206-L242","documentation":"RandomAccessReader throws IllegalStateException from seek() when the reader has already been closed (buffer == null). Once close() releases the internal buffer, any further positioning or read operation is invalid because there is no channel/buffer to seek within.","triggerScenarios":"Calling seek() after close() — typically a use-after-close bug: a reader closed in a finally block or by an earlier exception path, then still referenced by an iterator (keyIterator/keyReader), a sync-marker scan (readSyncMarker), or reset()/skipBytes() invoked on a stale handle.","commonSituations":"Prematurely closing a shared reader while another component still iterates it; double-close followed by reuse; returning a reader from a cache after its owner closed it; try-with-resources closing a reader that an enclosing iterator still uses.","solutions":["Ensure the reader's lifetime covers all uses — do not close while iterators depend on it","Check isClosed() (or null buffer) before seeking and reopen if needed","Restructure try-with-resources so the iteration happens inside the scope","Fix double-close/reuse bugs: obtain a fresh RandomAccessReader via createReader instead of reusing closed handles"],"exampleFix":"// before\ntry (RandomAccessReader reader = RandomAccessReader.open(file)) {\n    scheduleAsyncIteration(reader); // reader closed before iteration runs\n}\n// after\nRandomAccessReader reader = RandomAccessReader.open(file);\ntry {\n    iterateKeys(reader);\n} finally {\n    CloseableIterator<?> it = iterator;\n    if (it != null) it.close();\n    reader.close();\n}","handlingStrategy":"try-catch","validationCode":"if (reader.isClosed()) { reader = RandomAccessReader.open(file); } reader.seek(pos);","typeGuard":"boolean open = reader != null && !reader.isClosed();","tryCatchPattern":"try { reader.seek(pos); } catch (IllegalStateException e) { /* reopen reader or abort iteration */ }","preventionTips":["Keep reader open for the entire iteration scope","Avoid sharing readers across async lifecycles","Use try-with-resources so close happens after all reads"],"tags":["io","randomaccessreader","closed-resource"],"backgroundTag":"invalid-state-transition","analyzedSha":"88fd0f6a0eaed8943f05ac9e8f947882b8ddc8f1","analyzedAt":"2026-09-10T07:29:22.284Z","contentChangedAt":"2026-09-10T07:29:22.284Z","schemaVersion":2},"datasetVersion":"2026-09-17T15:17:12.973Z"}