Skip to content

Commit 4aafc47

Browse files
committed
[lldb/Test] Always set the cleanupSubprocesses tear down hook
Always clean up subprocesses on tear down instead of relying on the caller to do so. This is not only less error prone but also means the tests can be more concise. Differential revision: https://reviews.llvm.org/D83787
1 parent 0257ba5 commit 4aafc47

File tree

18 files changed

+5
-35
lines changed

18 files changed

+5
-35
lines changed

lldb/packages/Python/lldbsuite/test/lldbtest.py

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -891,23 +891,18 @@ def cleanupSubprocesses(self):
891891
for p in self.subprocesses:
892892
p.terminate()
893893
del p
894-
self.subprocesses.clear()
894+
del self.subprocesses[:]
895895
# Ensure any forked processes are cleaned up
896896
for pid in self.forkedProcessPids:
897897
try:
898898
os.kill(pid, signal.SIGTERM)
899899
except OSError:
900900
pass
901-
self.forkedProcessPids.clear()
901+
del self.forkedProcessPids[:]
902902

903903
def spawnSubprocess(self, executable, args=[], install_remote=True):
904904
""" Creates a subprocess.Popen object with the specified executable and arguments,
905905
saves it in self.subprocesses, and returns the object.
906-
NOTE: if using this function, ensure you also call:
907-
908-
self.addTearDownHook(self.cleanupSubprocesses)
909-
910-
otherwise the test suite will leak processes.
911906
"""
912907
proc = _RemoteProcess(
913908
install_remote) if lldb.remote_platform else _LocalProcess(self.TraceOn())
@@ -917,11 +912,6 @@ def spawnSubprocess(self, executable, args=[], install_remote=True):
917912

918913
def forkSubprocess(self, executable, args=[]):
919914
""" Fork a subprocess with its own group ID.
920-
NOTE: if using this function, ensure you also call:
921-
922-
self.addTearDownHook(self.cleanupSubprocesses)
923-
924-
otherwise the test suite will leak processes.
925915
"""
926916
child_pid = os.fork()
927917
if child_pid == 0:
@@ -1025,9 +1015,6 @@ def deletePexpectChild(self):
10251015

10261016
def tearDown(self):
10271017
"""Fixture for unittest test case teardown."""
1028-
#import traceback
1029-
# traceback.print_stack()
1030-
10311018
self.deletePexpectChild()
10321019

10331020
# Check and run any hook functions.
@@ -1054,6 +1041,9 @@ def tearDown(self):
10541041
for dict in reversed(self.dicts):
10551042
self.cleanup(dictionary=dict)
10561043

1044+
# Remove subprocesses created by the test.
1045+
self.cleanupSubprocesses()
1046+
10571047
# This must be the last statement, otherwise teardown hooks or other
10581048
# lines might depend on this still being active.
10591049
lldb.SBDebugger.Destroy(self.dbg)

lldb/test/API/commands/platform/process/list/TestProcessList.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ def test_process_list_with_args(self):
2525

2626
# Spawn a new process
2727
popen = self.spawnSubprocess(exe, args=["arg1", "--arg2", "arg3"])
28-
self.addTearDownHook(self.cleanupSubprocesses)
2928

3029
substrs = [str(popen.pid), "TestProcess arg1 --arg2 arg3"]
3130

lldb/test/API/commands/process/attach-resume/TestAttachResume.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ def process_attach_continue_interrupt_detach(self):
3333
exe = self.getBuildArtifact(exe_name)
3434

3535
popen = self.spawnSubprocess(exe)
36-
self.addTearDownHook(self.cleanupSubprocesses)
3736

3837
self.runCmd("process attach -p " + str(popen.pid))
3938

lldb/test/API/commands/process/attach/TestProcessAttach.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ def test_attach_to_process_by_id(self):
2929

3030
# Spawn a new process
3131
popen = self.spawnSubprocess(exe)
32-
self.addTearDownHook(self.cleanupSubprocesses)
3332

3433
self.runCmd("process attach -p " + str(popen.pid))
3534

@@ -55,7 +54,6 @@ def test_attach_to_process_from_different_dir_by_id(self):
5554

5655
# Spawn a new process
5756
popen = self.spawnSubprocess(exe)
58-
self.addTearDownHook(self.cleanupSubprocesses)
5957

6058
os.chdir(newdir)
6159
self.addTearDownHook(lambda: os.chdir(testdir))
@@ -74,7 +72,6 @@ def test_attach_to_process_by_name(self):
7472

7573
# Spawn a new process
7674
popen = self.spawnSubprocess(exe)
77-
self.addTearDownHook(self.cleanupSubprocesses)
7875

7976
self.runCmd("process attach -n " + exe_name)
8077

lldb/test/API/commands/process/attach/attach_denied/TestAttachDenied.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ def test_attach_to_process_by_id_denied(self):
3737

3838
# Spawn a new process
3939
popen = self.spawnSubprocess(exe, [pid_file_path])
40-
self.addTearDownHook(self.cleanupSubprocesses)
4140

4241
pid = lldbutil.wait_for_file_on_target(self, pid_file_path)
4342

lldb/test/API/commands/register/register/register_command/TestRegisters.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -457,7 +457,6 @@ def convenience_registers_with_process_attach(self, test_16bit_regs):
457457

458458
# Spawn a new process
459459
pid = self.spawnSubprocess(exe, ['wait_for_attach']).pid
460-
self.addTearDownHook(self.cleanupSubprocesses)
461460

462461
if self.TraceOn():
463462
print("pid of spawned process: %d" % pid)

lldb/test/API/commands/target/auto-install-main-executable/TestAutoInstallMainExecutable.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@ def test_target_auto_install_main_executable(self):
4848
self.debug_monitor_exe,
4949
commandline_args,
5050
install_remote=False)
51-
self.addTearDownHook(self.cleanupSubprocesses)
5251

5352
# Wait for the new process gets ready.
5453
time.sleep(0.1)

lldb/test/API/functionalities/deleted-executable/TestDeletedExecutable.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ def test(self):
3535

3636
# Spawn a new process
3737
popen = self.spawnSubprocess(exe, [pid_file_path])
38-
self.addTearDownHook(self.cleanupSubprocesses)
3938

4039
# Wait until process has fully started up.
4140
pid = lldbutil.wait_for_file_on_target(self, pid_file_path)

lldb/test/API/functionalities/process_group/TestChangeProcessGroup.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ def test_setpgid(self):
3838
(pid_file_path)))
3939

4040
popen = self.spawnSubprocess(exe, [pid_file_path])
41-
self.addTearDownHook(self.cleanupSubprocesses)
4241

4342
pid = lldbutil.wait_for_file_on_target(self, pid_file_path)
4443

lldb/test/API/functionalities/reproducers/attach/TestReproducerAttach.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ def test_reproducer_attach(self):
3737
pass
3838

3939
self.build(dictionary={'EXE': exe})
40-
self.addTearDownHook(self.cleanupSubprocesses)
4140

4241
inferior = self.spawnSubprocess(self.getBuildArtifact(exe), [token])
4342
pid = inferior.pid

lldb/test/API/functionalities/thread/create_after_attach/TestCreateAfterAttach.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,6 @@ def create_after_attach(self, use_fork):
5656
else:
5757
popen = self.spawnSubprocess(exe)
5858
pid = popen.pid
59-
self.addTearDownHook(self.cleanupSubprocesses)
6059

6160
# Attach to the spawned process
6261
self.runCmd("process attach -p " + str(pid))

lldb/test/API/macosx/find-dsym/bundle-with-dot-in-filename/TestBundleWithDotInFilename.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ def test_attach_and_check_dsyms(self):
3838
self.build()
3939
os.chdir(self.getBuildDir());
4040
popen = self.spawnSubprocess(exe)
41-
self.addTearDownHook(self.cleanupSubprocesses)
4241

4342
# Give the inferior time to start up, dlopen a bundle, remove the bundle it linked in
4443
sleep(5)

lldb/test/API/macosx/find-dsym/deep-bundle/TestDeepBundle.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ def test_attach_and_check_dsyms(self):
3636
exe = self.getBuildArtifact(exe_name)
3737
self.build()
3838
popen = self.spawnSubprocess(exe, [self.getBuildDir()])
39-
self.addTearDownHook(self.cleanupSubprocesses)
4039

4140
# Give the inferior time to start up, dlopen a bundle, remove the bundle it linked in
4241
sleep(5)

lldb/test/API/macosx/function-starts/TestFunctionStarts.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,6 @@ def do_function_starts(self, in_memory):
5353
(pid_file_path)))
5454

5555
popen = self.spawnSubprocess(exe, [pid_file_path])
56-
self.addTearDownHook(self.cleanupSubprocesses)
5756

5857
# Wait until process has fully started up.
5958
pid = lldbutil.wait_for_file_on_target(self, pid_file_path)

lldb/test/API/macosx/universal/TestUniversal.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,6 @@ def test_process_attach_with_wrong_arch(self):
137137
"Our main breakpoint has locations.")
138138

139139
popen = self.spawnSubprocess(exe, ["keep_waiting"])
140-
self.addTearDownHook(self.cleanupSubprocesses)
141140

142141
error = lldb.SBError()
143142
empty_listener = lldb.SBListener()

lldb/test/API/python_api/hello_world/TestHelloWorld.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,6 @@ def test_with_attach_to_process_with_id_api(self):
9191
if os.path.exists(token):
9292
os.remove(token)
9393
popen = self.spawnSubprocess(self.getBuildArtifact(exe), [token])
94-
self.addTearDownHook(self.cleanupSubprocesses)
9594
lldbutil.wait_for_file_on_target(self, token)
9695

9796
listener = lldb.SBListener("my.attach.listener")
@@ -126,7 +125,6 @@ def test_with_attach_to_process_with_name_api(self):
126125
if os.path.exists(token):
127126
os.remove(token)
128127
popen = self.spawnSubprocess(self.getBuildArtifact(exe), [token])
129-
self.addTearDownHook(self.cleanupSubprocesses)
130128
lldbutil.wait_for_file_on_target(self, token)
131129

132130
listener = lldb.SBListener("my.attach.listener")

lldb/test/API/tools/lldb-server/platform-process-connect/TestPlatformProcessConnect.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@ def test_platform_process_connect(self):
5454
self.debug_monitor_exe,
5555
commandline_args,
5656
install_remote=False)
57-
self.addTearDownHook(self.cleanupSubprocesses)
5857

5958
socket_id = lldbutil.wait_for_file_on_target(self, port_file)
6059

lldb/test/API/tools/lldb-vscode/attach/TestVSCode_attach.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,6 @@ def cleanup():
9090
self.addTearDownHook(cleanup)
9191

9292
popen = self.spawnSubprocess(program, [pid_file_path])
93-
self.addTearDownHook(self.cleanupSubprocesses)
9493

9594
pid = lldbutil.wait_for_file_on_target(self, pid_file_path)
9695

0 commit comments

Comments
 (0)