xref: /llvm-project/lldb/test/API/python_api/process/cancel_attach/TestCancelAttach.py (revision 1277bea4311692d3bd3d1a6566ec1011d3e72f65)
1"""
2Test using SendAsyncInterrupt to interrupt an "attach wait"
3"""
4
5import lldb
6import sys
7import time
8import threading
9from lldbsuite.test.decorators import *
10from lldbsuite.test.lldbtest import *
11import lldbsuite.test.lldbutil
12
13
14class AttachCancelTestCase(TestBase):
15    NO_DEBUG_INFO_TESTCASE = True
16
17    @skipIf(
18        remote=True,
19        hostoslist=["windows"],
20        bugnumber="https://github.com/llvm/llvm-project/issues/115618",
21    )
22    def test_scripted_implementation(self):
23        """Test that cancelling a stuck "attach waitfor" works."""
24        # First make an empty target for the attach:
25        target = self.dbg.CreateTarget(None)
26
27        # We need to cancel this, so we need to do the attach
28        # on a separate thread:
29        class AttachThread(threading.Thread):
30            def __init__(self, target, error):
31                # Make this a daemon thread so if we don't manage to interrupt,
32                # Python will keep this thread from hanging the test.
33                threading.Thread.__init__(self, daemon=True)
34                self.target = target
35                self.error = error
36
37            def run(self):
38                self.target.AttachToProcessWithName(
39                    lldb.SBListener(), "LLDB-No-Such-Process", True, self.error
40                )
41
42        error = lldb.SBError()
43        thread = AttachThread(target, error)
44        thread.start()
45
46        # Now wait till the attach on the child thread has made a process
47        # for the attach attempt:
48        while not target.process.IsValid():
49            time.sleep(1)
50        # I don't have a positive signal for "we started the attach attempt"
51        # so the best I can do is sleep a bit more here to give that a chance
52        # to start:
53        time.sleep(1)
54
55        # Now send the attach interrupt:
56        target.process.SendAsyncInterrupt()
57        # We don't want to stall if we can't interrupt, so join with a timeout:
58        thread.join(60)
59        if thread.is_alive():
60            self.fail("The attach thread is alive after timeout interval")
61
62        # Now check the error, should say the attach was interrupted:
63        self.assertTrue(error.Fail(), "We succeeded in not attaching")
64