{"record":{"id":"e54f9f3559a16af3","repo":"apache/hadoop","slug":"tried-to-exit-non-existent-enclosing-element-in-fs","errorCode":null,"errorMessage":"Tried to exit non-existent enclosing element in FSImage file","messagePattern":"Tried to exit non-existent enclosing element in FSImage file","errorType":"exception","errorClass":"IOException","httpStatus":null,"severity":"error","filePath":"hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/tools/offlineImageViewer/XmlImageVisitor.java","lineNumber":56,"sourceCode":"       throws IOException {\n    super(filename, printToScreen);\n  }\n\n  @Override\n  void finish() throws IOException {\n    super.finish();\n  }\n\n  @Override\n  void finishAbnormally() throws IOException {\n    write(\"\\n<!-- Error processing image file.  Exiting -->\\n\");\n    super.finishAbnormally();\n  }\n\n  @Override\n  void leaveEnclosingElement() throws IOException {\n    if (tagQ.isEmpty()) {\n      throw new IOException(\"Tried to exit non-existent enclosing element \" +\n          \"in FSImage file\");\n    }\n\n    ImageElement element = tagQ.pop();\n    write(\"</\" + element.toString() + \">\\n\");\n  }\n\n  @Override\n  void start() throws IOException {\n    write(\"<?xml version=\\\"1.0\\\" ?>\\n\");\n  }\n\n  @Override\n  void visit(ImageElement element, String value) throws IOException {\n    writeTag(element.toString(), value);\n  }\n\n  @Override","sourceCodeStart":38,"sourceCodeEnd":74,"githubUrl":"https://github.com/apache/hadoop/blob/2add9630210752f88ceb1bb74eb65e37bf41da8e/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/tools/offlineImageViewer/XmlImageVisitor.java#L38-L74","documentation":"The legacy XML outputter (oiv_legacy -p XML, XmlImageVisitor) keeps a stack of currently open elements and pops it in leaveEnclosingElement(). The stack was empty when a close was requested, meaning the visitor emitted more leave-element calls than start-element calls — an internal bookkeeping imbalance in the legacy visitor logic (or a custom subclass), not a user configuration error.","triggerScenarios":"Walking an old-format fsimage whose constructs hit an unbalanced start/leave path in oiv_legacy's XML visitor; or subclassing XmlImageVisitor with a leaveEnclosingElement() call that has no matching startElement() on some code path.","commonSituations":"Running `hdfs oiv_legacy -p XML` over pre-2.x images with symlinks/snapshots; custom visitor subclasses that skip startElement on edge cases.","solutions":["Try other legacy processors (`-p Lsr`, `-p Indented`, `-p Delimited`); if they succeed, the XML visitor hit an imbalance — file a HADOOP JIRA with the image","In custom visitor subclasses, audit that every leaveEnclosingElement() has a matching startElement() on all paths including exception paths","For Hadoop 2.x+ images use `hdfs oiv -p XML` (PBImageXmlWriter), which does not share this code"],"exampleFix":"// before: custom visitor leaves an element it never opened\nvisitor.startElement(ImageElement.INODE);\nif (listChildren) {\n  emitChildren(visitor);\n}\nvisitor.leaveEnclosingElement(); // also runs when emitChildren already left\n\n// after: only leave what this code path opened\nif (!listChildren) {\n  visitor.leaveEnclosingElement();\n}","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"try {\n  new OfflineImageViewer(inputFile, new XmlImageVisitor(out, false), false).go();\n} catch (IOException e) {\n  if (e.getMessage().contains(\"non-existent enclosing element\")) {\n    // internal visitor imbalance: try Lsr/Indented processors to confirm the\n    // image parses, then report the image upstream\n    LOG.error(\"legacy XML visitor stack imbalance on {}\", inputFile, e);\n  }\n  throw e;\n}","preventionTips":["In custom visitors, pair every leaveEnclosingElement() with a startElement() on all paths","For 2.x+ images use `hdfs oiv -p XML` (PBImageXmlWriter) instead of oiv_legacy","Cross-check suspicious old images with other oiv_legacy processors before debugging"],"tags":["hdfs","oiv-legacy","xml","visitor-lifecycle"],"backgroundTag":"unbalanced-xml-tags","analyzedSha":"2add9630210752f88ceb1bb74eb65e37bf41da8e","analyzedAt":"2026-08-22T19:55:07.957Z","schemaVersion":2},"datasetVersion":"2026-08-22T20:17:22.307Z"}