{"record":{"id":"7a270c6531b73425","repo":"apache/beam","slug":"unexpected-error-while-serializing-pubsubmessage-to-a-byte","errorCode":null,"errorMessage":"Unexpected error while serializing PubsubMessage to a byte array.","messagePattern":"Unexpected error while serializing PubsubMessage to a byte array\\.","errorType":"exception","errorClass":"RuntimeException","httpStatus":null,"severity":"error","filePath":"sdks/java/io/google-cloud-platform/src/main/java/org/apache/beam/sdk/io/gcp/pubsub/PubsubMessages.java","lineNumber":77,"sourceCode":"    if ((attributes == null || attributes.isEmpty())\n        && (messageId == null || messageId.isEmpty())\n        && (orderingKey == null || orderingKey.isEmpty())) {\n      // Optimize the case where we are just sending a payload.\n      byte[] payload = input.getPayload();\n      if (payload == null || payload.length == 0) {\n        return new byte[0];\n      }\n      int size =\n          CodedOutputStream.computeByteArraySize(\n              com.google.pubsub.v1.PubsubMessage.DATA_FIELD_NUMBER, payload);\n      byte[] serialized = new byte[size];\n      try {\n        CodedOutputStream output = CodedOutputStream.newInstance(serialized);\n        output.writeByteArray(com.google.pubsub.v1.PubsubMessage.DATA_FIELD_NUMBER, payload);\n        output.checkNoSpaceLeft();\n      } catch (IOException e) {\n        // Should not happen since we are writing to a byte array of the exact size.\n        throw new RuntimeException(\n            \"Unexpected error while serializing PubsubMessage to a byte array.\", e);\n      }\n      return serialized;\n    }\n    // Fallback to general case by building up a protobuf and serializing it.\n    return toProto(input).toByteArray();\n  }\n\n  public static PubsubMessage fromProto(com.google.pubsub.v1.PubsubMessage input) {\n    return new PubsubMessage(\n        input.getData().toByteArray(),\n        input.getAttributesMap(),\n        input.getMessageId(),\n        input.getOrderingKey());\n  }\n\n  // Convert the beam PubsubMessage to a serialized com.google.pubsub.v1.PubsubMessage proto\n  // representation.","sourceCodeStart":59,"sourceCodeEnd":95,"githubUrl":"https://github.com/apache/beam/blob/12126d8942aaf848030c478b4c6a28c6af861c66/sdks/java/io/google-cloud-platform/src/main/java/org/apache/beam/sdk/io/gcp/pubsub/PubsubMessages.java#L59-L95","documentation":"PubsubMessages.toSerializedPubsubMessageProto serializes a payload directly into a pre-sized PubsubMessage protobuf byte array. Writing to an in-memory array should never throw IOException; if it does, an internal invariant is broken, so it rethrows as RuntimeException with this message and the IOException as cause.","triggerScenarios":"Internal-only: CodedOutputStream.writeByteArray on the exact-size buffer reports no space left or another IOException. Triggered in practice by corrupted size computation or misuse of the internal API, not by user input.","commonSituations":"Rarely seen by end users; may appear during Beam upgrades or when calling internal PubsubMessage-to-proto helpers with unusual payload sizes.","solutions":["Report the issue to the Beam project with the payload size if reproducible; this indicates an internal size-computation bug.","Use the public API (PubsubMessages.toProto(...).toByteArray() or PubsubIO APIs) instead of the internal fast-path helper.","Upgrade Beam to a version where the fast-path sizing bug is fixed."],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"try {\n  byte[] bytes = PubsubMessages.toSerializedPubsubMessageProto(msg);\n} catch (RuntimeException e) {\n  // internal invariant broken: fall back to toProto(msg).toByteArray() and report\n}","preventionTips":["Avoid internal helpers; use public PubsubIO/PubsubMessages APIs","Keep Beam up to date","Report reproducible cases to the Beam project"],"tags":["java","pubsub","protobuf","serialization","internal"],"backgroundTag":"json-serialization-failed","analyzedSha":"12126d8942aaf848030c478b4c6a28c6af861c66","analyzedAt":"2026-09-13T01:50:10.254Z","contentChangedAt":"2026-09-13T01:50:10.254Z","schemaVersion":2},"datasetVersion":"2026-09-14T16:17:12.679Z"}