{"record":{"id":"9b30640de4542839","repo":"apache/beam","slug":"exceptions-are-thrown-from-dofn-teardown-method-when-trying","errorCode":null,"errorMessage":"Exceptions are thrown from DoFn.teardown method when trying to discard ProcessBundleHandler","messagePattern":"Exceptions are thrown from DoFn\\.teardown method when trying to discard ProcessBundleHandler","errorType":"console","errorClass":null,"httpStatus":null,"severity":"warning","filePath":"sdks/java/harness/src/main/java/org/apache/beam/fn/harness/control/ProcessBundleHandler.java","lineNumber":1205,"sourceCode":"      getProgressRequestLock().unlock();\n    }\n\n    void discard() {\n      synchronized (this) {\n        this.instructionId = null;\n        this.cacheTokens = null;\n        if (this.bundleCache != null) {\n          this.bundleCache.clear();\n        }\n        // setupFunctions are invoked in createBundleProcessor. Invoke teardownFunction here as the\n        // BundleProcessor is already removed from cache and won't be re-used.\n        for (ThrowingRunnable teardownFunction : Lists.reverse(this.getTearDownFunctions())) {\n          try {\n            teardownFunction.run();\n          } catch (OutOfMemoryError oom) {\n            throw oom;\n          } catch (Throwable e) {\n            LOG.warn(\n                \"Exceptions are thrown from DoFn.teardown method when trying to discard \"\n                    + \"ProcessBundleHandler\",\n                e);\n          }\n        }\n        getMetricsEnvironmentStateForBundle().discard();\n        for (BeamFnDataOutboundAggregator aggregator : getOutboundAggregators().values()) {\n          aggregator.discard();\n        }\n      }\n    }\n\n    // this is called in cachedBundleProcessors removal listener\n    void shutdown() {\n      for (ThrowingRunnable tearDownFunction : getTearDownFunctions()) {\n        LOG.debug(\"Tearing down function {}\", tearDownFunction);\n        try {\n          tearDownFunction.run();","sourceCodeStart":1187,"sourceCodeEnd":1223,"githubUrl":"https://github.com/apache/beam/blob/12126d8942aaf848030c478b4c6a28c6af861c66/sdks/java/harness/src/main/java/org/apache/beam/fn/harness/control/ProcessBundleHandler.java#L1187-L1223","documentation":"When a ProcessBundleHandler is discarded, all registered DoFn teardown functions are run in reverse order; OutOfMemoryError is rethrown, but any other Throwable from teardown is caught and this warning is logged. The bundle handler continues discarding (metrics are discarded), but user cleanup logic partially failed.","triggerScenarios":"One or more DoFn.teardown() methods (via @Teardown annotated methods) throw an exception when the handler is discarded at the end of processing; only OutOfMemoryError propagates.","commonSituations":"Teardown closing connections that are already closed; NPEs from uninitialized fields in teardown; external resource cleanup failures during harness shutdown or invalid bundle retries.","solutions":["Fix the exception in the logged stack trace inside your @Teardown method.","Make teardown idempotent and null-safe (check fields before use).","Do not throw from teardown; log and swallow recoverable cleanup errors in user code.","Release external resources in FinishBundle rather than relying solely on teardown."],"exampleFix":"// before\n@Teardown\npublic void teardown() {\n  connection.close(); // throws if never opened\n}\n// after\n@Teardown\npublic void teardown() {\n  if (connection != null) {\n    try { connection.close(); } catch (Exception e) { LOG.warn(\"close failed\", e); }\n  }\n}","handlingStrategy":"try-catch","validationCode":"// Guard teardown state\nif (connection != null && !connection.isClosed()) { /* safe to close */ }","typeGuard":null,"tryCatchPattern":"try {\n  teardown();\n} catch (Exception e) {\n  LOG.warn(\"teardown failed\", e); // never propagate from @Teardown\n}","preventionTips":["Write null-safe, idempotent @Teardown methods","Never throw from teardown; log recoverable errors","Free heavy resources in FinishBundle so teardown is trivial","Check logs for the underlying exception to fix user code"],"tags":["java","apache-beam","dofn","teardown"],"backgroundTag":"resource-cleanup-failed","analyzedSha":"12126d8942aaf848030c478b4c6a28c6af861c66","analyzedAt":"2026-09-13T01:50:10.254Z","contentChangedAt":"2026-09-13T01:50:10.254Z","schemaVersion":2},"datasetVersion":"2026-09-20T03:17:13.778Z"}