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