diff --git a/mercurial/dirstate.py b/mercurial/dirstate.py
index 7a8bfc05b6918f03e2d3387fc945b81c9333f435_bWVyY3VyaWFsL2RpcnN0YXRlLnB5..5b0beaf45491318488ceb6137310b3178017f0a2_bWVyY3VyaWFsL2RpcnN0YXRlLnB5 100644
--- a/mercurial/dirstate.py
+++ b/mercurial/dirstate.py
@@ -178,7 +178,18 @@
                 # manager
                 if self._parentwriters <= 0:
                     assert self._parentwriters == 0
-                    self._invalidated_context = False
+                    if self._invalidated_context:
+                        self._invalidated_context = False
+                    else:
+                        # When an exception occured, `_invalidated_context`
+                        # would have been set to True by the `invalidate`
+                        # call earlier.
+                        #
+                        # We don't have more straightforward code, because the
+                        # Exception catching (and the associated `invalidate`
+                        # calling) might have been called by a nested context
+                        # instead of the top level one.
+                        self.write(repo.currenttransaction())
 
     # here to help migration to the new code
     def parentchange(self):