{"record":{"id":"4c7815d841734c6a","repo":"apache/hadoop","slug":"stream-is-closed-4c7815","errorCode":null,"errorMessage":"Stream is closed!","messagePattern":"Stream is closed!","errorType":"exception","errorClass":"IOException","httpStatus":null,"severity":"error","filePath":"hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/fs/store/ByteBufferInputStream.java","lineNumber":81,"sourceCode":"    LOG.debug(\"ByteBufferInputStream.close()\");\n    byteBuffer = null;\n  }\n\n  /**\n   * Is the stream open?\n   * @return true if the stream has not been closed.\n   */\n  public synchronized boolean isOpen() {\n    return byteBuffer != null;\n  }\n\n  /**\n   * Verify that the stream is open.\n   * @throws IOException if the stream is closed\n   */\n  private void verifyOpen() throws IOException {\n    if (byteBuffer == null) {\n      throw new IOException(FSExceptionMessages.STREAM_IS_CLOSED);\n    }\n  }\n\n  /**\n   * Check the open state.\n   * @throws IllegalStateException if the stream is closed.\n   */\n  private void checkOpenState() {\n    Preconditions.checkState(isOpen(),\n        FSExceptionMessages.STREAM_IS_CLOSED);\n  }\n\n  public synchronized int read() throws IOException {\n    if (available() > 0) {\n      return byteBuffer.get() & 0xFF;\n    } else {\n      return -1;\n    }","sourceCodeStart":63,"sourceCodeEnd":99,"githubUrl":"https://github.com/apache/hadoop/blob/2add9630210752f88ceb1bb74eb65e37bf41da8e/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/fs/store/ByteBufferInputStream.java#L63-L99","documentation":"ByteBufferInputStream serves reads directly from a java.nio.ByteBuffer; close() releases the reference. verifyOpen() then throws IOException('Stream is closed!') from read/skip paths invoked after close. The public synchronized isOpen() method is the supported test for this state.","triggerScenarios":"Any read()/skip()/position() call after close(); retry logic that closes the stream in a finally block and keeps reading; one thread closing while another reads.","commonSituations":"Use-after-close in cleanup paths; streams shared across components where each thinks it owns closing; defensive double-close followed by reads.","solutions":["Check isOpen() before each access on streams whose lifecycle you do not own","Restructure so a single owner closes the stream after all readers finish","Open a fresh stream over the buffer for the next pass instead of reusing the closed one"],"exampleFix":"// before\nin.close();\nint b = in.read(); // IOException: Stream is closed!\n\n// after\nin.close();\nif (in.isOpen()) {\n  int b = in.read();\n}","handlingStrategy":"validation","validationCode":"ByteBufferInputStream in = ...;\nif (in.isOpen()) {\n  int b = in.read();\n}","typeGuard":null,"tryCatchPattern":"Catch IOException and compare against FSExceptionMessages.STREAM_IS_CLOSED; treat a match as a lifecycle bug in the caller (read-after-close), not a transient failure — do not retry.","preventionTips":["Assign one owner for closing the stream","Check isOpen() before reads in shared or retried code","Never read inside finally blocks that also close"],"tags":["hadoop","io","input-stream","lifecycle","use-after-close"],"backgroundTag":"stream-already-closed","analyzedSha":"2add9630210752f88ceb1bb74eb65e37bf41da8e","analyzedAt":"2026-08-22T19:55:07.957Z","schemaVersion":2},"datasetVersion":"2026-08-23T01:17:44.959Z"}