{"record":{"id":"2e47c8a0878b53c6","repo":"SonarSource/sonarqube","slug":"can-not-write-message-msg","errorCode":null,"errorMessage":"Can not write message ${msg}","messagePattern":"Can not write message (.+?)","errorType":"exception","errorClass":"IllegalStateException","httpStatus":null,"severity":"error","filePath":"server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/util/Protobuf2.java","lineNumber":41,"sourceCode":"import com.google.protobuf.Message;\nimport com.google.protobuf.Parser;\nimport java.io.IOException;\nimport java.io.InputStream;\nimport java.io.OutputStream;\nimport javax.annotation.CheckForNull;\n\npublic class Protobuf2 {\n\n  public static final Protobuf2 PROTOBUF2 = new Protobuf2();\n\n  private Protobuf2() {\n  }\n\n  public Protobuf2 writeTo(Message msg, OutputStream output) {\n    try {\n      msg.writeTo(output);\n    } catch (IOException e) {\n      throw new IllegalStateException(\"Can not write message \" + msg, e);\n    }\n    return this;\n  }\n\n  public Protobuf2 writeDelimitedTo(Message msg, OutputStream output) {\n    try {\n      msg.writeDelimitedTo(output);\n    } catch (IOException e) {\n      throw new IllegalStateException(\"Can not write message \" + msg, e);\n    }\n    return this;\n  }\n\n  public <M extends Message> M parseFrom(Parser<M> parser, InputStream input) {\n    try {\n      return parser.parseFrom(input);\n    } catch (InvalidProtocolBufferException e) {\n      throw new IllegalStateException(\"Can not parse message\", e);","sourceCodeStart":23,"sourceCodeEnd":59,"githubUrl":"https://github.com/SonarSource/sonarqube/blob/184c821202192afc1c599fc912d0889b69fffa53/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/util/Protobuf2.java#L23-L59","documentation":"Protobuf2.writeTo wraps IOException from Message.writeTo into an IllegalStateException, since serialization to an in-memory or well-behaved stream should not fail. It means the protobuf message could not be written to the output stream.","triggerScenarios":"Calling writeTo(msg, output) when the underlying OutputStream throws IOException: stream closed, broken pipe, disk full for file streams, or missing required fields in proto2 syntax.","commonSituations":"Writing protobuf-serialized reports/components to files or network streams in CE tasks; streams closed upstream; serialization of messages with unset required fields.","solutions":["Check the wrapped IOException cause for the stream failure reason","Verify the OutputStream is open and writable at call time","Ensure required proto2 fields are set before writing (missing fields cause IOException)","Check disk space / connection health for file or socket streams"],"exampleFix":"// before\nprotobuf2.writeTo(msg, outputStream);\n// after\ntry (OutputStream out = Files.newOutputStream(path)) {\n  protobuf2.writeTo(msg, out);\n}","handlingStrategy":"try-catch","validationCode":"if (output == null) {\n  throw new IllegalArgumentException(\"OutputStream is null\");\n}\n// for proto2: ensure required fields are set\nif (!msg.isInitialized()) {\n  throw new IllegalArgumentException(\"Message missing required fields: \" + msg);\n}","typeGuard":"boolean writable(OutputStream out) {\n  return out != null; // deeper checks require probing the stream\n}","tryCatchPattern":"try {\n  protobuf2.writeTo(msg, output);\n} catch (IllegalStateException e) {\n  LOGGER.error(\"Failed to serialize {}\", msg.getDescriptorForType().getFullName(), e.getCause());\n}","preventionTips":["Call msg.isInitialized() before writing proto2 messages","Keep streams open for the duration of writes","Check disk space / connection health for file and socket streams","Avoid writing to streams owned by already-closed resources"],"tags":["protobuf","serialization","io"],"backgroundTag":"json-marshal-failed","analyzedSha":"184c821202192afc1c599fc912d0889b69fffa53","analyzedAt":"2026-09-09T12:23:51.573Z","contentChangedAt":"2026-09-09T12:23:51.573Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}