diff --git a/mercurial/debugcommands.py b/mercurial/debugcommands.py
index ed90f4a1bb2caa76ccac2f052504269cffaeb468_bWVyY3VyaWFsL2RlYnVnY29tbWFuZHMucHk=..a8e1ff9ac257567808ccce4c9bc9b4eb78568ac3_bWVyY3VyaWFsL2RlYnVnY29tbWFuZHMucHk= 100644
--- a/mercurial/debugcommands.py
+++ b/mercurial/debugcommands.py
@@ -2238,6 +2238,13 @@
 
     locks = []
     try:
+        # Help the tests out on Windows by writing the correct PID when
+        # invoked by the test harness, before creating the lock.
+        pids = encoding.environ.get(b'DAEMON_PIDS')
+        if pids:
+            with open(pids, "ab") as fp:
+                fp.write(b'%d\n' % os.getpid())
+
         if opts.get('set_wlock'):
             try:
                 locks.append(repo.wlock(False))
diff --git a/tests/test-upgrade-repo.t b/tests/test-upgrade-repo.t
index ed90f4a1bb2caa76ccac2f052504269cffaeb468_dGVzdHMvdGVzdC11cGdyYWRlLXJlcG8udA==..a8e1ff9ac257567808ccce4c9bc9b4eb78568ac3_dGVzdHMvdGVzdC11cGdyYWRlLXJlcG8udA== 100644
--- a/tests/test-upgrade-repo.t
+++ b/tests/test-upgrade-repo.t
@@ -2103,7 +2103,6 @@
 ----------------------------------------------
 
   $ hg -R auto-upgrade debuglock --set-lock --quiet &
-  $ echo $! >> $DAEMON_PIDS
   $ $RUNTESTDIR/testlib/wait-on-file 10 auto-upgrade/.hg/store/lock
   $ hg status -R auto-upgrade \
   >     --config format.use-dirstate-v2.automatic-upgrade-of-mismatching-repositories=yes \