{"record":{"id":"302ea45c2896b363","repo":"apache/beam","slug":"unexpected-ioexception","errorCode":null,"errorMessage":"Unexpected IOException: ","messagePattern":"Unexpected IOException: ","errorType":"exception","errorClass":"RuntimeException","httpStatus":null,"severity":"error","filePath":"sdks/java/core/src/main/java/org/apache/beam/sdk/values/PCollectionViews.java","lineNumber":497,"sourceCode":"    private transient @Nullable T defaultValue;\n    private @Nullable Coder<T> valueCoder;\n    private boolean hasDefault;\n    private TypeDescriptorSupplier<T> typeDescriptorSupplier;\n\n    private SingletonViewFn2(\n        boolean hasDefault,\n        T defaultValue,\n        Coder<T> valueCoder,\n        TypeDescriptorSupplier<T> typeDescriptorSupplier) {\n      this.hasDefault = hasDefault;\n      this.defaultValue = defaultValue;\n      this.valueCoder = valueCoder;\n      this.typeDescriptorSupplier = typeDescriptorSupplier;\n      if (hasDefault) {\n        try {\n          this.encodedDefaultValue = CoderUtils.encodeToByteArray(valueCoder, defaultValue);\n        } catch (IOException e) {\n          throw new RuntimeException(\"Unexpected IOException: \", e);\n        }\n      }\n    }\n\n    /** Returns if a default value was specified. */\n    @Internal\n    public boolean hasDefault() {\n      return hasDefault;\n    }\n\n    /**\n     * Returns the default value that was specified.\n     *\n     * <p>For internal use only.\n     *\n     * @throws NoSuchElementException if no default was specified.\n     */\n    @Override","sourceCodeStart":479,"sourceCodeEnd":515,"githubUrl":"https://github.com/apache/beam/blob/12126d8942aaf848030c478b4c6a28c6af861c66/sdks/java/core/src/main/java/org/apache/beam/sdk/values/PCollectionViews.java#L479-L515","documentation":"In SingletonViewFn2's constructor, if a default value was supplied for a singleton view, Beam eagerly encodes it with the value coder via CoderUtils.encodeToByteArray. Encoding should never fail for a well-formed coder, so an IOException there indicates a broken/misconfigured coder and is rethrown as a RuntimeException with message 'Unexpected IOException: '. This is an internal-invariant failure, not a user-data problem.","triggerScenarios":"Constructing a SingletonViewFn2 (e.g. via View.asSingleton().withDefaultValue(...) materialization) with a value coder whose encode throws IOException — typically a coder backed by a stream/registry failure, a closed stream, or a custom coder with a faulty encodeClosed implementation.","commonSituations":"Custom Coder implementations that declare checked IO but are buggy; coders for types with externalized state; using a coder incompatible with the actual default value type after a refactor or Beam version upgrade.","solutions":["Inspect the cause attached to the RuntimeException — fix the underlying custom Coder's encodeToByteArray/encode method that throws IOException.","Verify the coder passed to withDefaultValue / the view matches the default value's actual type.","Test the coder in isolation (CoderUtils.encodeToByteArray(coder, defaultValue)) in a unit test before running the pipeline.","Upgrade/align Beam SDK version if the failure came from a known coder bug."],"exampleFix":"// before (custom coder throwing)\nclass BuggyCoder implements Coder<MyType> { public void encode(MyType v, OutputStream out) throws IOException { throw new IOException(\"nyi\"); } }\n// after — implement encoding properly or use a registry coder\nCoder<MyType> coder = SerializableCoder.of(MyType.class);","handlingStrategy":"try-catch","validationCode":"try {\n  CoderUtils.encodeToByteArray(valueCoder, defaultValue);\n} catch (IOException e) {\n  throw new IllegalStateException(\"Coder cannot encode default value\", e);\n}","typeGuard":null,"tryCatchPattern":"try {\n  PCollectionView<T> view = pc.apply(View.asSingleton().withDefaultValue(d));\n} catch (RuntimeException e) {\n  if (e.getCause() instanceof IOException) { /* fix/replace coder */ }\n  throw e;\n}","preventionTips":["Prefer built-in coders (SerializableCoder, AvroCoder) over hand-rolled ones for view defaults.","Round-trip test every custom coder before use in pipelines.","Keep coder and value type declarations in sync during refactors."],"tags":["java","apache-beam","coder","runtime-exception"],"backgroundTag":"coder-encode-failed","analyzedSha":"12126d8942aaf848030c478b4c6a28c6af861c66","analyzedAt":"2026-09-13T01:50:10.254Z","contentChangedAt":"2026-09-13T01:50:10.254Z","schemaVersion":2},"datasetVersion":"2026-09-14T21:17:11.552Z"}