{"record":{"id":"ea41251e4a89adf4","repo":"apache/hadoop","slug":"closed","errorCode":null,"errorMessage":"closed","messagePattern":"closed","errorType":"exception","errorClass":"IOException","httpStatus":null,"severity":"error","filePath":"hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/io/compress/bzip2/CBZip2OutputStream.java","lineNumber":650,"sourceCode":"      throw new IllegalArgumentException(\"blockSize(\" + blockSize\n          + \") < 1\");\n    }\n    if (blockSize > 9) {\n      throw new IllegalArgumentException(\"blockSize(\" + blockSize\n          + \") > 9\");\n    }\n\n    this.blockSize100k = blockSize;\n    this.out = out;\n    init();\n  }\n\n  @Override\n  public void write(final int b) throws IOException {\n    if (this.out != null) {\n      write0(b);\n    } else {\n      throw new IOException(\"closed\");\n    }\n  }\n\n  private void writeRun() throws IOException {\n    final int lastShadow = this.last;\n\n    if (lastShadow < this.allowableBlockSize) {\n      final int currentCharShadow = this.currentChar;\n      final Data dataShadow = this.data;\n      dataShadow.inUse[currentCharShadow] = true;\n      final byte ch = (byte) currentCharShadow;\n\n      int runLengthShadow = this.runLength;\n      this.crc.updateCRC(currentCharShadow, runLengthShadow);\n\n      switch (runLengthShadow) {\n      case 1:\n        dataShadow.block[lastShadow + 2] = ch;","sourceCodeStart":632,"sourceCodeEnd":668,"githubUrl":"https://github.com/apache/hadoop/blob/2add9630210752f88ceb1bb74eb65e37bf41da8e/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/io/compress/bzip2/CBZip2OutputStream.java#L632-L668","documentation":"CBZip2OutputStream.write(int) checks the internal output stream reference; after close() the codec nulls this.out, so any subsequent single-byte write throws IOException(\"closed\"). The object intentionally refuses work once it has been closed instead of silently dropping bytes. It is a lifecycle error in the caller's code, not data corruption.","triggerScenarios":"Calling write(int) on a CBZip2OutputStream after close() has run: double-processing the same writer, a finally block that closes early, nested wrappers where an inner close() nulls the stream and an outer wrapper keeps writing, or cleanup code racing with a producer thread.","commonSituations":"Utility wrappers (DataOutputStream around CBZip2OutputStream) whose close ordering is wrong; code that reuses a stream variable after a try-with-resources block; concurrent close/write without synchronization; frameworks calling close on error paths while another thread flushes the last byte.","solutions":["Fix the lifecycle: ensure close() happens exactly once, after all writes — use try-with-resources around the outermost wrapper owning the stream.","Track ownership explicitly (one component owns close; the rest only write) or guard with an AtomicBoolean closed flag.","Null out your own reference after close so later writes fail on a clear NullPointerException at your site rather than inside the codec.","In multithreaded writers, synchronize write/close on a single monitor so close cannot interleave with pending writes."],"exampleFix":"// before\ncbz.close();\ncbz.write(b); // IOException: closed\n\n// after\ntry (CBZip2OutputStream cbz = new CBZip2OutputStream(out)) {\n  cbz.write(b);\n} // close happens once, after all writes","handlingStrategy":"try-catch","validationCode":"// Track ownership outside the codec\nprivate boolean closed = false;\n\nvoid safeWrite(int b) throws IOException {\n  if (closed) throw new IllegalStateException(\"writer already closed\");\n  cbz.write(b);\n}","typeGuard":null,"tryCatchPattern":"try {\n  cbz.write(b);\n} catch (IOException e) {\n  if (\"closed\".equals(e.getMessage())) {\n    throw new IllegalStateException(\"bug: write after close\", e);\n  }\n  throw e;\n}","preventionTips":["Use try-with-resources around the outermost owning wrapper; close exactly once.","Do not share the stream across components without a single designated owner.","Synchronize write vs close in multithreaded producers."],"tags":["bzip2","compression","write-after-close","lifecycle","hadoop"],"backgroundTag":"write-after-close","analyzedSha":"2add9630210752f88ceb1bb74eb65e37bf41da8e","analyzedAt":"2026-08-22T19:55:07.957Z","schemaVersion":2},"datasetVersion":"2026-08-22T20:17:22.307Z"}