[llvm] [lit] Fix lit hang on pool join when exception is thrown (PR #131881)

David Garcia Orozco via llvm-commits llvm-commits at lists.llvm.org
Tue Apr 8 06:38:40 PDT 2025


https://github.com/ayylol updated https://github.com/llvm/llvm-project/pull/131881

>From 7851d95c356be62edd3b1286c78f3ad2efc24ecc Mon Sep 17 00:00:00 2001
From: "Garcia Orozco, David" <david.garcia.orozco at intel.com>
Date: Tue, 18 Mar 2025 07:12:19 -0700
Subject: [PATCH 1/4] Fix lit hang

---
 llvm/utils/lit/lit/TestRunner.py | 18 +++++++++---------
 1 file changed, 9 insertions(+), 9 deletions(-)

diff --git a/llvm/utils/lit/lit/TestRunner.py b/llvm/utils/lit/lit/TestRunner.py
index 00432b8d31778..471c76b732e62 100644
--- a/llvm/utils/lit/lit/TestRunner.py
+++ b/llvm/utils/lit/lit/TestRunner.py
@@ -201,7 +201,12 @@ def executeShCmd(cmd, shenv, results, timeout=0):
     timeoutHelper = TimeoutHelper(timeout)
     if timeout > 0:
         timeoutHelper.startTimer()
-    finalExitCode = _executeShCmd(cmd, shenv, results, timeoutHelper)
+    try:
+        finalExitCode = _executeShCmd(cmd, shenv, results, timeoutHelper)
+    except InternalShellError:
+        e = sys.exc_info()[1]
+        finalExitCode = 127
+        results.append(ShellCommandResult(e.command, "", e.message, finalExitCode, False))
     timeoutHelper.cancel()
     timeoutInfo = None
     if timeoutHelper.timeoutReached():
@@ -1105,15 +1110,10 @@ def executeScriptInternal(
 
     results = []
     timeoutInfo = None
-    try:
-        shenv = ShellEnvironment(cwd, test.config.environment)
-        exitCode, timeoutInfo = executeShCmd(
+    shenv = ShellEnvironment(cwd, test.config.environment)
+    exitCode, timeoutInfo = executeShCmd(
             cmd, shenv, results, timeout=litConfig.maxIndividualTestTime
-        )
-    except InternalShellError:
-        e = sys.exc_info()[1]
-        exitCode = 127
-        results.append(ShellCommandResult(e.command, "", e.message, exitCode, False))
+            )
 
     out = err = ""
     for i, result in enumerate(results):

>From bf4873cdf09cdf6b2a3e1fed94439fa55231ef82 Mon Sep 17 00:00:00 2001
From: David Garcia Orozco <david.garcia.orozco at intel.com>
Date: Tue, 1 Apr 2025 18:43:13 +0000
Subject: [PATCH 2/4] Initial commit for timeout hang test

---
 llvm/utils/lit/tests/Inputs/timeout-hang/lit.cfg           | 7 +++++++
 .../utils/lit/tests/Inputs/timeout-hang/run-nonexistent.py | 1 +
 llvm/utils/lit/tests/timeout-hang.py                       | 6 ++++++
 3 files changed, 14 insertions(+)
 create mode 100644 llvm/utils/lit/tests/Inputs/timeout-hang/lit.cfg
 create mode 100644 llvm/utils/lit/tests/Inputs/timeout-hang/run-nonexistent.py
 create mode 100644 llvm/utils/lit/tests/timeout-hang.py

diff --git a/llvm/utils/lit/tests/Inputs/timeout-hang/lit.cfg b/llvm/utils/lit/tests/Inputs/timeout-hang/lit.cfg
new file mode 100644
index 0000000000000..d1fb2a223ccfd
--- /dev/null
+++ b/llvm/utils/lit/tests/Inputs/timeout-hang/lit.cfg
@@ -0,0 +1,7 @@
+import lit.formats
+
+config.name = "timeout-hang"
+config.suffixes = [".py"]
+config.test_format = lit.formats.ShTest()
+config.test_source_root = None
+config.test_exec_root = None
diff --git a/llvm/utils/lit/tests/Inputs/timeout-hang/run-nonexistent.py b/llvm/utils/lit/tests/Inputs/timeout-hang/run-nonexistent.py
new file mode 100644
index 0000000000000..79776d262f109
--- /dev/null
+++ b/llvm/utils/lit/tests/Inputs/timeout-hang/run-nonexistent.py
@@ -0,0 +1 @@
+# RUN: nonexistent
diff --git a/llvm/utils/lit/tests/timeout-hang.py b/llvm/utils/lit/tests/timeout-hang.py
new file mode 100644
index 0000000000000..5950cbd010575
--- /dev/null
+++ b/llvm/utils/lit/tests/timeout-hang.py
@@ -0,0 +1,6 @@
+# REQUIRES: lit-max-individual-test-time
+
+# https://github.com/llvm/llvm-project/issues/133914
+
+# RUN: not %{lit} %{inputs}/timeout-hang/run-nonexistent.py \
+# RUN: --timeout=1 --param external=0 -a

>From 98119a12527e32c9ce1b9a762bbcecafe905d9aa Mon Sep 17 00:00:00 2001
From: David Garcia Orozco <david.garcia.orozco at intel.com>
Date: Tue, 1 Apr 2025 20:25:20 +0000
Subject: [PATCH 3/4] Implement timeout hang test

---
 .../lit/tests/Inputs/timeout-hang/lit.cfg     |  2 +-
 ...run-nonexistent.py => run-nonexistent.txt} |  0
 llvm/utils/lit/tests/timeout-hang.py          | 27 ++++++++++++++++---
 3 files changed, 25 insertions(+), 4 deletions(-)
 rename llvm/utils/lit/tests/Inputs/timeout-hang/{run-nonexistent.py => run-nonexistent.txt} (100%)

diff --git a/llvm/utils/lit/tests/Inputs/timeout-hang/lit.cfg b/llvm/utils/lit/tests/Inputs/timeout-hang/lit.cfg
index d1fb2a223ccfd..1019d94898b6d 100644
--- a/llvm/utils/lit/tests/Inputs/timeout-hang/lit.cfg
+++ b/llvm/utils/lit/tests/Inputs/timeout-hang/lit.cfg
@@ -1,7 +1,7 @@
 import lit.formats
 
 config.name = "timeout-hang"
-config.suffixes = [".py"]
+config.suffixes = [".txt"]
 config.test_format = lit.formats.ShTest()
 config.test_source_root = None
 config.test_exec_root = None
diff --git a/llvm/utils/lit/tests/Inputs/timeout-hang/run-nonexistent.py b/llvm/utils/lit/tests/Inputs/timeout-hang/run-nonexistent.txt
similarity index 100%
rename from llvm/utils/lit/tests/Inputs/timeout-hang/run-nonexistent.py
rename to llvm/utils/lit/tests/Inputs/timeout-hang/run-nonexistent.txt
diff --git a/llvm/utils/lit/tests/timeout-hang.py b/llvm/utils/lit/tests/timeout-hang.py
index 5950cbd010575..1a43ee548f330 100644
--- a/llvm/utils/lit/tests/timeout-hang.py
+++ b/llvm/utils/lit/tests/timeout-hang.py
@@ -1,6 +1,27 @@
 # REQUIRES: lit-max-individual-test-time
 
-# https://github.com/llvm/llvm-project/issues/133914
+# Python has some issues dealing with exceptions when multiprocessing,
+# which can cause hangs. Previously this could occur when we encountered
+# an internal shell exception, and had a timeout set.
 
-# RUN: not %{lit} %{inputs}/timeout-hang/run-nonexistent.py \
-# RUN: --timeout=1 --param external=0 -a
+# This test runs a lit test that tries to launch a non-existent file,
+# throwing an exception. We expect this to fail immediately, rather than
+# timeout.
+
+# DEFINE: %{timeout}=1
+
+# RUN: not %{lit} %{inputs}/timeout-hang/run-nonexistent.txt \
+# RUN: --timeout=%{timeout} --param external=0 | %{python} %s %{timeout}
+
+import sys
+import re
+
+timeout_time = float(sys.argv[1])
+testing_time = float(re.search(r'Testing Time: (.*)s', sys.stdin.read()).group(1))
+
+if testing_time < timeout_time:
+    print("Testing took less than timeout")
+    sys.exit(0)
+else:
+    print("Testing took as long or longer than timeout")
+    sys.exit(1)

>From a1a6945871dbad83ed78c215f4bce60d61a035b4 Mon Sep 17 00:00:00 2001
From: "Garcia Orozco, David" <david.garcia.orozco at intel.com>
Date: Tue, 8 Apr 2025 06:31:21 -0700
Subject: [PATCH 4/4] Format lit files

---
 llvm/utils/lit/lit/TestRunner.py                          | 8 +++++---
 .../lit/tests/Inputs/timeout-hang/run-nonexistent.txt     | 2 +-
 llvm/utils/lit/tests/timeout-hang.py                      | 2 +-
 3 files changed, 7 insertions(+), 5 deletions(-)

diff --git a/llvm/utils/lit/lit/TestRunner.py b/llvm/utils/lit/lit/TestRunner.py
index 4a60634be406b..16e9c7fbf45c5 100644
--- a/llvm/utils/lit/lit/TestRunner.py
+++ b/llvm/utils/lit/lit/TestRunner.py
@@ -206,7 +206,9 @@ def executeShCmd(cmd, shenv, results, timeout=0):
     except InternalShellError:
         e = sys.exc_info()[1]
         finalExitCode = 127
-        results.append(ShellCommandResult(e.command, "", e.message, finalExitCode, False))
+        results.append(
+            ShellCommandResult(e.command, "", e.message, finalExitCode, False)
+        )
     timeoutHelper.cancel()
     timeoutInfo = None
     if timeoutHelper.timeoutReached():
@@ -1112,8 +1114,8 @@ def executeScriptInternal(
     timeoutInfo = None
     shenv = ShellEnvironment(cwd, test.config.environment)
     exitCode, timeoutInfo = executeShCmd(
-            cmd, shenv, results, timeout=litConfig.maxIndividualTestTime
-            )
+        cmd, shenv, results, timeout=litConfig.maxIndividualTestTime
+    )
 
     out = err = ""
     for i, result in enumerate(results):
diff --git a/llvm/utils/lit/tests/Inputs/timeout-hang/run-nonexistent.txt b/llvm/utils/lit/tests/Inputs/timeout-hang/run-nonexistent.txt
index 79776d262f109..fd7ed210baee0 100644
--- a/llvm/utils/lit/tests/Inputs/timeout-hang/run-nonexistent.txt
+++ b/llvm/utils/lit/tests/Inputs/timeout-hang/run-nonexistent.txt
@@ -1 +1 @@
-# RUN: nonexistent
+RUN: nonexistent
diff --git a/llvm/utils/lit/tests/timeout-hang.py b/llvm/utils/lit/tests/timeout-hang.py
index 1a43ee548f330..486f07983708f 100644
--- a/llvm/utils/lit/tests/timeout-hang.py
+++ b/llvm/utils/lit/tests/timeout-hang.py
@@ -17,7 +17,7 @@
 import re
 
 timeout_time = float(sys.argv[1])
-testing_time = float(re.search(r'Testing Time: (.*)s', sys.stdin.read()).group(1))
+testing_time = float(re.search(r"Testing Time: (.*)s", sys.stdin.read()).group(1))
 
 if testing_time < timeout_time:
     print("Testing took less than timeout")



More information about the llvm-commits mailing list