{"record":{"id":"1a21e0b580290538","repo":"oracle/graal","slug":"trying-to-write-during-graph-print","errorCode":null,"errorMessage":"Trying to write during graph print.","messagePattern":"Trying to write during graph print\\.","errorType":"exception","errorClass":"IllegalStateException","httpStatus":null,"severity":"error","filePath":"compiler/src/jdk.graal.compiler/src/jdk/graal/compiler/graphio/GraphProtocol.java","lineNumber":180,"sourceCode":"            flushEmbedded();\n        } finally {\n            printing = false;\n        }\n    }\n\n    public final void endGroup() throws IOException {\n        printing = true;\n        try {\n            writeByte(CLOSE_GROUP);\n            flushEmbedded();\n        } finally {\n            printing = false;\n        }\n    }\n\n    final int write(ByteBuffer src) throws IOException {\n        if (printing) {\n            throw new IllegalStateException(\"Trying to write during graph print.\");\n        }\n        constantPool.reset();\n        return writeBytesRaw(src);\n    }\n\n    final boolean isOpen() {\n        return channel.isOpen();\n    }\n\n    @Override\n    public final void close() {\n        try {\n            flush();\n            channel.close();\n        } catch (IOException ex) {\n            throw new Error(ex);\n        }\n    }","sourceCodeStart":162,"sourceCodeEnd":198,"githubUrl":"https://github.com/oracle/graal/blob/a66e9ccd1d7bf2552883939aa0788dfd0e294aab/compiler/src/jdk.graal.compiler/src/jdk/graal/compiler/graphio/GraphProtocol.java#L162-L198","documentation":"GraphProtocol implements WritableByteChannel; while a graph print is executing (printing flag set during print/beginGroup/endGroup/startDocument), the channel is in a protocol-controlled state and raw writes would interleave garbage into the structured stream. write(ByteBuffer) therefore throws IllegalStateException if called mid-print. Only bytes written between print operations (e.g. via flushEmbedded boundaries) are legal.","triggerScenarios":"Another thread using the same channel while a dump is in progress; or application code writing custom bytes through the channel from inside a graph-printing callback. Embedding the GraphProtocol channel inside another protocol and writing to it during a nested print call.","commonSituations":"Sharing one GraphOutput/channel across threads without synchronization. Custom dump listeners that also emit data on the channel. Building composite dump formats where raw framing bytes are written around, but accidentally during, graph sections.","solutions":["Serialize all access to the channel: synchronize on the GraphOutput/GraphProtocol so raw writes never overlap print calls.","Move custom byte emission to after endGroup/print completes (between top-level dump operations).","Give each concurrent producer its own GraphOutput rather than multiplexing one channel."],"exampleFix":"// before\nnew Thread(() -> channel.write(myBuffer)).start(); // may run during print\noutput.print(graph, ...);\n\n// after\nsynchronized (output) {\n    output.print(graph, ...);\n    channel.write(myBuffer); // only between print operations\n}","handlingStrategy":"try-catch","validationCode":"// serialize all channel access around the GraphOutput\nsynchronized (output) {\n    output.print(graph, ...);\n}\nsynchronized (output) {\n    channel.write(rawBuffer); // only between print operations\n}","typeGuard":null,"tryCatchPattern":"try {\n    channel.write(buf);\n} catch (IllegalStateException e) {\n    if (e.getMessage().contains(\"during graph print\")) {\n        // a print is in flight on another thread: wait for the dump lock and retry once\n        synchronized (output) { channel.write(buf); }\n    } else throw e;\n}","preventionTips":["Give each writer thread its own GraphOutput; never share one channel across producers.","Wrap every channel operation in the same lock that guards print calls.","Emit custom framing bytes only between top-level dump operations."],"tags":["java","graal","graphio","concurrency","channel","dumping","state-machine"],"backgroundTag":null,"analyzedSha":"a66e9ccd1d7bf2552883939aa0788dfd0e294aab","analyzedAt":"2026-08-14T13:58:47.161Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}