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