procutil: fix passing of stdin_bytes to background command stable
authorPierre-Yves David <pierre-yves.david@octobus.net>
Thu, 06 Mar 2025 12:23:27 +0100
branchstable
changeset 53034 7a4772e92f23
parent 53032 c3e450bfdf34
child 53035 771706c5acfb
procutil: fix passing of stdin_bytes to background command When the command where run in the background, the std was closed. So we have to so do some file description juggling to make sure it is passed down. This was affecting automatic clone bundle generation and associated test was added.
hgext/clonebundles.py
mercurial/configitems.toml
mercurial/utils/procutil.py
tests/test-clonebundles-autogen.t
--- a/hgext/clonebundles.py	Thu Mar 06 10:15:00 2025 +0100
+++ b/hgext/clonebundles.py	Thu Mar 06 12:23:27 2025 +0100
@@ -1029,8 +1029,10 @@
     details about how to configure this feature.
     """
     debug = repo.ui.configbool(b'devel', b'debug.clonebundles')
+    op_id = repo.ui.config(b'devel', b'clonebundles.override-operation-id')
     bundles = read_auto_gen(repo)
-    op_id = b"%d_acbr" % os.getpid()
+    if op_id is None:
+        op_id = b"%d_acbr" % os.getpid()
     create, delete = auto_bundle_needed_actions(repo, bundles, op_id)
 
     # if some bundles are scheduled for creation in the background, they will
--- a/mercurial/configitems.toml	Thu Mar 06 10:15:00 2025 +0100
+++ b/mercurial/configitems.toml	Thu Mar 06 12:23:27 2025 +0100
@@ -465,6 +465,15 @@
 
 [[items]]
 section = "devel"
+name = "clonebundles.override-operation-id"
+documentation = """
+override the operation id used to produce unique filename at generation time.
+
+Useful to stabilize some filename in the tests.
+"""
+
+[[items]]
+section = "devel"
 name = "check-locks"
 default = false
 
--- a/mercurial/utils/procutil.py	Thu Mar 06 10:15:00 2025 +0100
+++ b/mercurial/utils/procutil.py	Thu Mar 06 12:23:27 2025 +0100
@@ -743,7 +743,7 @@
             script = b' '.join(shellquote(x) for x in cmd)
         if record_wait is None:
             # double-fork to completely detach from the parent process
-            script = b'( %s ) &' % script
+            script = b'( ( %s ) <&3 3<&- &) 3<&0' % script
             start_new_session = True
         else:
             start_new_session = False
--- a/tests/test-clonebundles-autogen.t	Thu Mar 06 10:15:00 2025 +0100
+++ b/tests/test-clonebundles-autogen.t	Thu Mar 06 12:23:27 2025 +0100
@@ -411,12 +411,61 @@
   full-bzip2-v2-11_revs-4226b1cd5fda_tip-*_acbr.hg (glob)
   $ ls -1 ../server/.hg/tmp-bundles
 
+background generation without debug
+-----------------------------------
+
+The debug option make the command wait for background process and change the
+way stdin is accessible to the script. So we also test this variant.
+
+Gather the name of the expected bundle
+
+  $ v1_file=$TESTTMP/final-upload/full-bzip2-v1-11_revs-4226b1cd5fda_tip-background_testing.hg
+  $ v2_file=$TESTTMP/final-upload/full-bzip2-v2-11_revs-4226b1cd5fda_tip-background_testing.hg
+
+cleanup things
+
+
+  $ hg -R ../server/ admin::clone-bundles-clear
+  clone-bundles: deleting bundle full-bzip2-v1-11_revs-4226b1cd5fda_tip-*_acbr.hg (glob)
+  clone-bundles: deleting bundle full-bzip2-v2-11_revs-4226b1cd5fda_tip-*_acbr.hg (glob)
+(also delete the manifest to be sure)
+  $ rm ../server/.hg/clonebundles.manifest
+
+Nothing should remain
+
+  $ cat ../server/.hg/clonebundles.auto-gen
+  $ ls -1 ../final-upload
+  $ ls -1 ../server/.hg/tmp-bundles
+
+Start a process in the background without the debug option
+
+  $ hg -R ../server/ admin::clone-bundles-refresh --background \
+  >     --config devel.debug.clonebundles=no \
+  >     --config devel.clonebundles.override-operation-id=background_testing
+
+  $ $RUNTESTDIR/testlib/wait-on-file 30 $v1_file
+  $ $RUNTESTDIR/testlib/wait-on-file 30 $v2_file
+
+We should have bundle now
+
+  $ cat ../server/.hg/clonebundles.manifest
+  file:/*/$TESTTMP/final-upload/full-bzip2-v1-11_revs-4226b1cd5fda_tip-background_testing.hg BUNDLESPEC=bzip2-v1 (glob)
+  file:/*/$TESTTMP/final-upload/full-bzip2-v2-11_revs-4226b1cd5fda_tip-background_testing.hg BUNDLESPEC=bzip2-v2 (glob)
+  $ ls -1 ../final-upload
+  full-bzip2-v1-11_revs-4226b1cd5fda_tip-background_testing.hg
+  full-bzip2-v2-11_revs-4226b1cd5fda_tip-background_testing.hg
+  $ ls -1 ../server/.hg/tmp-bundles
+
+
+
+
+
 Test HTTP URL
 =========================
 
   $ hg -R ../server/ admin::clone-bundles-clear
-  clone-bundles: deleting bundle full-bzip2-v1-11_revs-4226b1cd5fda_tip-*_acbr.hg (glob)
-  clone-bundles: deleting bundle full-bzip2-v2-11_revs-4226b1cd5fda_tip-*_acbr.hg (glob)
+  clone-bundles: deleting bundle full-bzip2-v1-11_revs-4226b1cd5fda_tip-*.hg (glob)
+  clone-bundles: deleting bundle full-bzip2-v2-11_revs-4226b1cd5fda_tip-*.hg (glob)
 
   $ cat >> ../server/.hg/hgrc << EOF
   > [clone-bundles]