https://github.com/i3/i3/pull/6644

From d87997f93b94aac34b7b8a3397bf833c16773a53 Mon Sep 17 00:00:00 2001
From: Jan Vornberger <jan@271.de>
Date: Fri, 31 Jul 2026 21:25:03 +0200
Subject: [PATCH] tests: Fix race-condition in TestWorker.pm

Wait for the forked test process to exit and only then emit $EOF to
signal the end of the test. Otherwise i3 might still be in the process
of cleaning up when another i3 instance is trying to re-use the display.
---
 testcases/lib/TestWorker.pm | 21 +++++++++++++++------
 1 file changed, 15 insertions(+), 6 deletions(-)

diff --git a/testcases/lib/TestWorker.pm b/testcases/lib/TestWorker.pm
index 9716f7f196..1aa540fa8a 100644
--- a/testcases/lib/TestWorker.pm
+++ b/testcases/lib/TestWorker.pm
@@ -42,8 +42,8 @@ sub worker {
     if ($pid == 0) {
         close $ipc;
         undef @complete_run::CLEANUP;
-        # reap dead test children
-        $SIG{CHLD} = sub { waitpid -1, POSIX::WNOHANG };
+        # Test children are waited for synchronously in worker_wait().
+        $SIG{CHLD} = 'DEFAULT';
 
         $worker->{ipc} = $ipc_child;
 
@@ -130,12 +130,21 @@ sub worker_wait {
             do $file;
             $test->ok(undef, "$@") if $@;
 
-            # XXX hack, we need to trigger the read watcher once more
-            # to signal eof to TAP::Parser
-            print $EOF;
-
             exit 0;
         }
+
+        # Wait until the test process has exited, including all of its END
+        # blocks. In particular, i3test's END block terminates and reaps the i3
+        # process. Only signal completion afterwards so that complete-run.pl
+        # cannot start the next test on this display while the previous i3 is
+        # still running.
+        my $waited = waitpid $pid, 0;
+        die "waitpid($pid): $!" unless $waited == $pid;
+
+        # XXX hack, we need to trigger the read watcher once more
+        # to signal eof to TAP::Parser
+        syswrite($ipc, $EOF) == length($EOF)
+            or die "could not signal test completion: $!";
     }
 }
 
