{"record":{"id":"48d378d50d7d9739","repo":"apache/flink","slug":"writer-has-already-been-opened-as-writetype-type","errorCode":null,"errorMessage":"Writer has already been opened as {writeType} type, but trying to reopen it as {type} type.","messagePattern":"Writer has already been opened as (.+?) type, but trying to reopen it as (.+?) type\\.","errorType":"exception","errorClass":"IllegalStateException","httpStatus":null,"severity":"error","filePath":"flink-connectors/flink-file-sink-common/src/main/java/org/apache/flink/streaming/api/functions/sink/filesystem/OutputStreamBasedPartFileWriter.java","lineNumber":101,"sourceCode":"    }\n\n    @Override\n    public long getSize() throws IOException {\n        return currentPartStream.getPos();\n    }\n\n    @Override\n    public OutputStream asOutputStream() throws IOException {\n        ensureWriteType(Type.OUTPUT_STREAM);\n        return currentPartStream;\n    }\n\n    protected void ensureWriteType(Type type) {\n        if (type != this.writeType) {\n            if (this.writeType == null) {\n                this.writeType = type;\n            } else {\n                throw new IllegalStateException(\n                        \"Writer has already been opened as \"\n                                + writeType\n                                + \" type, but trying to reopen it as \"\n                                + type\n                                + \" type.\");\n            }\n        }\n    }\n\n    abstract static class OutputStreamBasedBucketWriter<IN, BucketID>\n            implements BucketWriter<IN, BucketID> {\n\n        private final RecoverableWriter recoverableWriter;\n\n        OutputStreamBasedBucketWriter(final RecoverableWriter recoverableWriter) {\n            this.recoverableWriter = recoverableWriter;\n        }\n","sourceCodeStart":83,"sourceCodeEnd":119,"githubUrl":"https://github.com/apache/flink/blob/2f3c205e9266cb30240eb7f4fdab15cad629a70f/flink-connectors/flink-file-sink-common/src/main/java/org/apache/flink/streaming/api/functions/sink/filesystem/OutputStreamBasedPartFileWriter.java#L83-L119","documentation":"Thrown by OutputStreamBasedPartFileWriter.ensureWriteType when the writer has already committed to one write mode (RECORD_WISE via write() or OUTPUT_STREAM via asOutputStream()) and a call attempts to switch to the other. A single part-file writer supports only one mode for its lifetime because the underlying BulkWriter or OutputStream contract assumes exclusive access.","triggerScenarios":"Calling write(element, time) (which sets RECORD_WISE) followed by asOutputStream() (which sets OUTPUT_STREAM) on the same InProgressFileWriter instance, or vice versa.","commonSituations":"A custom BucketWriter or compaction logic that tries to write elements via the record API and also obtain the raw OutputStream on the same file; mixing BulkWriter.addElement with direct stream writes in the same bucket lifecycle.","solutions":["Pick one write mode per writer instance and stick with it — do not interleave write() and asOutputStream().","If both modes are needed, open a new in-progress file for the second mode rather than reusing the same writer.","Audit custom compaction or BucketWriter implementations to ensure they don't mix the two APIs."],"exampleFix":"// before — mixing write types on the same writer\nwriter.write(record, now);\nOutputStream os = writer.asOutputStream(); // throws IllegalStateException\n// after — use one mode per writer\nwriter.write(record, now);\n// or open a dedicated stream-based writer\nOutputStreamBasedPartFileWriter<?, ?> streamWriter =\n    (OutputStreamBasedPartFileWriter<?, ?>) bucketWriter.openNewInProgressFile(bucketId, path, ts);\nOutputStream os = streamWriter.asOutputStream();","handlingStrategy":"validation","validationCode":"// Track which write mode the writer is in before switching\n// Do not interleave write() and asOutputStream() on the same writer\nif (writerHasBeenUsedForRecords) {\n    throw new IllegalStateException(\"Cannot obtain OutputStream after record-wise writes\");\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Commit to one write mode (RECORD_WISE or OUTPUT_STREAM) per writer instance for its entire lifetime.","If both modes are needed, open separate in-progress files.","Audit custom BucketWriter and compaction implementations for mixed write-mode usage."],"tags":["file-sink","output-stream-writer","internal-api"],"backgroundTag":null,"analyzedSha":"2f3c205e9266cb30240eb7f4fdab15cad629a70f","analyzedAt":"2026-08-14T08:48:24.518Z","schemaVersion":2},"datasetVersion":"2026-08-14T10:17:34.591Z"}