412670
diff --git a/src/lxml/apihelpers.pxi b/src/lxml/apihelpers.pxi
412670
index 5eb3416..88a031d 100644
412670
--- a/src/lxml/apihelpers.pxi
412670
+++ b/src/lxml/apihelpers.pxi
412670
@@ -246,9 +246,10 @@ cdef dict _build_nsmap(xmlNode* c_node):
412670
     while c_node is not NULL and c_node.type == tree.XML_ELEMENT_NODE:
412670
         c_ns = c_node.nsDef
412670
         while c_ns is not NULL:
412670
-            prefix = funicodeOrNone(c_ns.prefix)
412670
-            if prefix not in nsmap:
412670
-                nsmap[prefix] = funicodeOrNone(c_ns.href)
412670
+            if c_ns.prefix or c_ns.href:
412670
+                prefix = funicodeOrNone(c_ns.prefix)
412670
+                if prefix not in nsmap:
412670
+                    nsmap[prefix] = funicodeOrNone(c_ns.href)
412670
             c_ns = c_ns.next
412670
         c_node = c_node.parent
412670
     return nsmap
412670
diff --git a/src/lxml/includes/xmlparser.pxd b/src/lxml/includes/xmlparser.pxd
412670
index a196e34..45acfc8 100644
412670
--- a/src/lxml/includes/xmlparser.pxd
412670
+++ b/src/lxml/includes/xmlparser.pxd
412670
@@ -144,6 +144,7 @@ cdef extern from "libxml/parser.h":
412670
         void* userData
412670
         int* spaceTab
412670
         int spaceMax
412670
+        int nsNr
412670
         bint html
412670
         bint progressive
412670
         int inSubset
412670
diff --git a/src/lxml/iterparse.pxi b/src/lxml/iterparse.pxi
412670
index 4c20506..3da7485 100644
412670
--- a/src/lxml/iterparse.pxi
412670
+++ b/src/lxml/iterparse.pxi
412670
@@ -419,7 +419,7 @@ cdef int _countNsDefs(xmlNode* c_node):
412670
     count = 0
412670
     c_ns = c_node.nsDef
412670
     while c_ns is not NULL:
412670
-        count += 1
412670
+        count += (c_ns.href is not NULL)
412670
         c_ns = c_ns.next
412670
     return count
412670
 
412670
@@ -430,9 +430,10 @@ cdef int _appendStartNsEvents(xmlNode* c_node, list event_list) except -1:
412670
     count = 0
412670
     c_ns = c_node.nsDef
412670
     while c_ns is not NULL:
412670
-        ns_tuple = (funicode(c_ns.prefix) if c_ns.prefix is not NULL else '',
412670
-                    funicode(c_ns.href))
412670
-        event_list.append( (u"start-ns", ns_tuple) )
412670
-        count += 1
412670
+        if c_ns.href:
412670
+            ns_tuple = (funicodeOrEmpty(c_ns.prefix),
412670
+                        funicode(c_ns.href))
412670
+            event_list.append( (u"start-ns", ns_tuple) )
412670
+            count += 1
412670
         c_ns = c_ns.next
412670
     return count
412670
diff --git a/src/lxml/parser.pxi b/src/lxml/parser.pxi
412670
index 3ed223b..f5ff6b2 100644
412670
--- a/src/lxml/parser.pxi
412670
+++ b/src/lxml/parser.pxi
412670
@@ -569,6 +569,9 @@ cdef class _ParserContext(_ResolverContext):
412670
                 self._c_ctxt.disableSAX = 0 # work around bug in libxml2
412670
             else:
412670
                 xmlparser.xmlClearParserCtxt(self._c_ctxt)
412670
+                # work around bug in libxml2 [2.9.10 .. 2.9.14]:
412670
+                # https://gitlab.gnome.org/GNOME/libxml2/-/issues/378
412670
+                self._c_ctxt.nsNr = 0
412670
 
412670
     cdef int prepare(self, bint set_document_loader=True) except -1:
412670
         cdef int result
412670
diff --git a/src/lxml/tests/test_etree.py b/src/lxml/tests/test_etree.py
412670
index 42613dc..db1f560 100644
412670
--- a/src/lxml/tests/test_etree.py
412670
+++ b/src/lxml/tests/test_etree.py
412670
@@ -1459,6 +1459,27 @@ class ETreeOnlyTestCase(HelperTestCase):
412670
             [1,2,1,4],
412670
             counts)
412670
 
412670
+    def test_walk_after_parse_failure(self):
412670
+        # This used to be an issue because libxml2 can leak empty namespaces
412670
+        # between failed parser runs.  iterwalk() failed to handle such a tree.
412670
+        parser = etree.XMLParser()
412670
+
412670
+        try:
412670
+            etree.XML('''<anot xmlns="1">''', parser=parser)
412670
+        except etree.XMLSyntaxError:
412670
+            pass
412670
+        else:
412670
+            assert False, "invalid input did not fail to parse"
412670
+
412670
+        et = etree.XML('''<root>  </root>''', parser=parser)
412670
+        try:
412670
+            ns = next(etree.iterwalk(et, events=('start-ns',)))
412670
+        except StopIteration:
412670
+            # This would be the expected result, because there was no namespace
412670
+            pass
412670
+        else:
412670
+            assert False, "Found unexpected namespace '%s'" % ns
412670
+
412670
     def test_itertext_comment_pi(self):
412670
         # https://bugs.launchpad.net/lxml/+bug/1844674
412670
         XML = self.etree.XML