{"record":{"id":"3db575b25204a90c","repo":"apache/flink","slug":"a-stream-against-this-file-was-already-created","errorCode":null,"errorMessage":"A stream against this file was already created.","messagePattern":"A stream against this file was already created\\.","errorType":"exception","errorClass":"IllegalStateException","httpStatus":null,"severity":"error","filePath":"flink-formats/flink-parquet/src/main/java/org/apache/flink/formats/parquet/StreamOutputFile.java","lineNumber":63,"sourceCode":"    private final AtomicBoolean used;\n\n    /**\n     * Creates a new StreamOutputFile. The first call to {@link #create(long)} or {@link\n     * #createOrOverwrite(long)} returns a stream that writes to the given stream.\n     *\n     * @param stream The stream to write to.\n     */\n    StreamOutputFile(FSDataOutputStream stream) {\n        this.stream = checkNotNull(stream);\n        this.used = new AtomicBoolean(false);\n    }\n\n    @Override\n    public PositionOutputStream create(long blockSizeHint) {\n        if (used.compareAndSet(false, true)) {\n            return new PositionOutputStreamAdapter(stream);\n        } else {\n            throw new IllegalStateException(\"A stream against this file was already created.\");\n        }\n    }\n\n    @Override\n    public PositionOutputStream createOrOverwrite(long blockSizeHint) {\n        return create(blockSizeHint);\n    }\n\n    @Override\n    public boolean supportsBlockSize() {\n        return false;\n    }\n\n    @Override\n    public long defaultBlockSize() {\n        return DEFAULT_BLOCK_SIZE;\n    }\n}","sourceCodeStart":45,"sourceCodeEnd":81,"githubUrl":"https://github.com/apache/flink/blob/2f3c205e9266cb30240eb7f4fdab15cad629a70f/flink-formats/flink-parquet/src/main/java/org/apache/flink/formats/parquet/StreamOutputFile.java#L45-L81","documentation":"StreamOutputFile wraps an FSDataOutputStream and enforces single-use semantics: create() may be called exactly once, guarded by an AtomicBoolean compareAndSet. A second call throws IllegalStateException because the underlying stream cannot be reopened or repositioned.","triggerScenarios":"Calling create() or createOrOverwrite() (which delegates to create()) twice on the same StreamOutputFile instance.","commonSituations":"Retries in sink code that reuse the same OutputFile, or a committing/rolling policy that attempts to overwrite the same wrapped stream instead of opening a new file.","solutions":["Create a fresh StreamOutputFile (or a new output stream via the FileSystem) for each write attempt","For retry logic, re-resolve the OutputFile from the RecoverableWriter/FileSystem rather than caching it","Note createOrOverwrite() has the same restriction here - do not rely on overwrite semantics on a stream-backed file"],"exampleFix":"// before\nStreamOutputFile f = new StreamOutputFile(stream);\nf.create(0);\nf.create(0); // throws\n\n// after\ntry (PositionOutputStream out = new StreamOutputFile(stream).create(0)) {\n    // write\n}\n// need another write? open a NEW stream + StreamOutputFile","handlingStrategy":"validation","validationCode":"// treat OutputFile as one-shot; never call create twice\nif (outputFileCreated) { throw new IllegalStateException(\"reuse\"); }","typeGuard":null,"tryCatchPattern":"try { out = outputFile.create(blockSize); } catch (IllegalStateException e) { // re-open the file via FileSystem and retry once with a NEW OutputFile }","preventionTips":["Open a new stream/OutputFile for every (re)write attempt","Never cache StreamOutputFile across retry iterations","Remember createOrOverwrite() is not overwriting on stream-backed files"],"tags":["parquet","output-stream","single-use","flink"],"backgroundTag":null,"analyzedSha":"2f3c205e9266cb30240eb7f4fdab15cad629a70f","analyzedAt":"2026-08-14T08:48:24.518Z","schemaVersion":2},"datasetVersion":"2026-08-14T10:17:34.591Z"}