{"record":{"id":"3ed06286b1346d89","repo":"apache/beam","slug":"unclosed-error-handler-initialized-at-s","errorCode":null,"errorMessage":"Unclosed error handler initialized at %s","messagePattern":"Unclosed error handler initialized at (.+?)","errorType":"exception","errorClass":"RuntimeError","httpStatus":null,"severity":"error","filePath":"sdks/python/apache_beam/transforms/error_handling.py","lineNumber":97,"sourceCode":"    \"\"\"Returns result of applying the error consumer to the error pcollections.\n    \"\"\"\n    if not self._closed:\n      raise RuntimeError(\n          \"Cannot access the output of an error handler \"\n          \"until it has been closed.\")\n    return self._output\n\n  def add_error_pcollection(self, pcoll):\n    \"\"\"Called by a class implementing error handling on the error records.\n    \"\"\"\n    pcoll.pipeline._register_error_handler(self)\n    self._error_pcolls.append(pcoll)\n\n  def verify_closed(self):\n    \"\"\"Called at end of pipeline construction to ensure errors are not ignored.\n    \"\"\"\n    if not self._closed:\n      raise RuntimeError(\n          \"Unclosed error handler initialized at %s\" % self._creation_traceback)\n\n\nclass _IdentityPTransform(transforms.PTransform):\n  def expand(self, pcoll):\n    return pcoll\n\n\nclass CollectingErrorHandler(ErrorHandler):\n  \"\"\"An ErrorHandler that simply collects all errors for further processing.\n\n  This ErrorHandler requires the set of errors be retrieved via `output()`\n  and consumed (or explicitly discarded).\n  \"\"\"\n  def __init__(self):\n    super().__init__(_IdentityPTransform())\n    self._creation_traceback = traceback.format_stack()[-2]\n    self._output_accessed = False","sourceCodeStart":79,"sourceCodeEnd":115,"githubUrl":"https://github.com/apache/beam/blob/12126d8942aaf848030c478b4c6a28c6af861c66/sdks/python/apache_beam/transforms/error_handling.py#L79-L115","documentation":"At the end of pipeline construction Beam calls verify_closed() on every ErrorHandler to make sure none was created and then abandoned with errors silently dropped. If the handler was never closed (never used as a context manager / never had its output consumed into the pipeline), this RuntimeError fires, pointing at the traceback captured when the handler was created.","triggerScenarios":"Creating an ErrorHandler (e.g. CollectingErrorHandler or via with_exception_handling) but never exiting its context manager, or constructing one manually and attaching it to no transform before the pipeline is built.","commonSituations":"Abandoned with-blocks exited early via return/raise, handlers instantiated in interactive sessions for experimentation, or code refactors that removed the with_exception_handling usage but left the handler construction.","solutions":["Use the handler as a context manager (with error_handling(...) as handler:) so it closes automatically.","Check the 'initialized at' traceback in the message to locate the unclosed handler and either use or delete it.","If errors are intentionally ignored, wrap the transform differently or explicitly consume/close the handler."],"exampleFix":"# before\nhandler = RecordCountingErrorHandler()\npcoll.with_exception_handling(handler, ...)  # never closed\n# after\nwith error_handling(RecordCountingErrorHandler()) as handler:\n  pcoll.with_exception_handling(handler, ...)","handlingStrategy":"validation","validationCode":"assert getattr(handler, '_closed', False), 'error handler must be used as a context manager'","typeGuard":"def is_closed(handler) -> bool:\n    return bool(getattr(handler, '_closed', False))","tryCatchPattern":"try:\n    pipeline.run()\nexcept RuntimeError as e:\n    if 'Unclosed error handler' in str(e):\n        locate_and_fix_handler(e)  # message includes creation traceback\n    raise","preventionTips":["Prefer `with error_handling(...) as handler:` over manual construction.","Search code for ErrorHandler( constructions not wrapped in a with-block.","Read the 'initialized at' traceback in the message to find the leak."],"tags":["python","apache-beam","resource-leak","pipeline-construction"],"backgroundTag":"invalid-state-transition","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"}