changeset 46330:02f3badf9011

upgrade: re-read current requirements after taking lock Since we are writing to repository, it's better to re-read after taking the lock. Differential Revision: https://phab.mercurial-scm.org/D9822
author Pulkit Goyal <7895pulkit@gmail.com>
date Mon, 18 Jan 2021 19:50:21 +0530
parents 17176f64a03d
children 8788981c95f8
files mercurial/upgrade.py
diffstat 1 files changed, 26 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/mercurial/upgrade.py	Thu Jan 14 21:29:46 2021 +0530
+++ b/mercurial/upgrade.py	Mon Jan 18 19:50:21 2021 +0530
@@ -245,6 +245,7 @@
     """Upgrades a share to use share-safe mechanism"""
     wlock = None
     store_requirements = localrepo._readrequires(storevfs, False)
+    original_crequirements = current_requirements.copy()
     # after upgrade, store requires will be shared, so lets find
     # the requirements which are not present in store and
     # write them to share's .hg/requires
@@ -254,6 +255,19 @@
     current_requirements.add(requirementsmod.SHARESAFE_REQUIREMENT)
     try:
         wlock = lockmod.trylock(ui, hgvfs, b'wlock', 0, 0)
+        # some process might change the requirement in between, re-read
+        # and update current_requirements
+        locked_requirements = localrepo._readrequires(hgvfs, True)
+        if locked_requirements != original_crequirements:
+            removed = current_requirements - locked_requirements
+            # update current_requirements in place because it's passed
+            # as reference
+            current_requirements -= removed
+            current_requirements |= locked_requirements
+            diffrequires = current_requirements - store_requirements
+            # add share-safe requirement as it will mark the share as share-safe
+            diffrequires.add(requirementsmod.SHARESAFE_REQUIREMENT)
+            current_requirements.add(requirementsmod.SHARESAFE_REQUIREMENT)
         scmutil.writerequires(hgvfs, diffrequires)
         ui.warn(_(b'repository upgraded to use share-safe mode\n'))
     except error.LockError as e:
@@ -281,6 +295,7 @@
     """Downgrades a share which use share-safe to not use it"""
     wlock = None
     source_requirements = localrepo._readrequires(sharedvfs, True)
+    original_crequirements = current_requirements.copy()
     # we cannot be 100% sure on which requirements were present in store when
     # the source supported share-safe. However, we do know that working
     # directory requirements were not there. Hence we remove them
@@ -290,6 +305,17 @@
 
     try:
         wlock = lockmod.trylock(ui, hgvfs, b'wlock', 0, 0)
+        # some process might change the requirement in between, re-read
+        # and update current_requirements
+        locked_requirements = localrepo._readrequires(hgvfs, True)
+        if locked_requirements != original_crequirements:
+            removed = current_requirements - locked_requirements
+            # update current_requirements in place because it's passed
+            # as reference
+            current_requirements -= removed
+            current_requirements |= locked_requirements
+            current_requirements |= source_requirements
+            current_requirements -= set(requirementsmod.SHARESAFE_REQUIREMENT)
         scmutil.writerequires(hgvfs, current_requirements)
         ui.warn(_(b'repository downgraded to not use share-safe mode\n'))
     except error.LockError as e: