Skip to content

Commit c40952c

Browse files
author
Piyush Jaiswal
committed
Fix non-deterministic failure in
1 parent a4e8ec9 commit c40952c

File tree

2 files changed

+84
-10
lines changed

2 files changed

+84
-10
lines changed

lldb/packages/Python/lldbsuite/test/tools/lldb-dap/lldbdap_testcase.py

Lines changed: 37 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
import os
22
import time
3-
from typing import Optional
43
import uuid
4+
from typing import Optional
55

66
import dap_server
77
from dap_server import Source
88
from lldbsuite.test.lldbtest import *
9-
from lldbsuite.test import lldbplatformutil
10-
import lldbgdbserverutils
119
import base64
1210

11+
import lldbgdbserverutils
12+
from lldbsuite.test import lldbplatformutil
13+
1314

1415
class DAPTestCaseBase(TestBase):
1516
# set timeout based on whether ASAN was enabled or not. Increase
@@ -451,8 +452,25 @@ def attach(
451452
# if we throw an exception during the test case.
452453
def cleanup():
453454
if disconnectAutomatically:
454-
self.dap_server.request_disconnect(terminateDebuggee=True)
455-
self.dap_server.terminate()
455+
try:
456+
self.dap_server.request_disconnect(terminateDebuggee=True)
457+
except (
458+
ValueError,
459+
TimeoutError,
460+
BrokenPipeError,
461+
ConnectionError,
462+
Exception,
463+
) as e:
464+
# DAP server might not be responsive, skip disconnect and terminate directly
465+
print(
466+
f"Warning: disconnect failed ({e}), skipping and terminating directly"
467+
)
468+
try:
469+
self.dap_server.terminate()
470+
except Exception as e:
471+
print(
472+
f"Warning: terminate failed ({e}), DAP server may have already died"
473+
)
456474

457475
# Execute the cleanup function during test case tear down.
458476
self.addTearDownHook(cleanup)
@@ -462,9 +480,20 @@ def cleanup():
462480
if expectFailure:
463481
return response
464482
if not (response and response["success"]):
465-
self.assertTrue(
466-
response["success"], "attach failed (%s)" % (response["message"])
467-
)
483+
error_msg = "attach failed"
484+
if response:
485+
if "message" in response:
486+
error_msg += " (%s)" % response["message"]
487+
elif "body" in response and "error" in response["body"]:
488+
if "format" in response["body"]["error"]:
489+
error_msg += " (%s)" % response["body"]["error"]["format"]
490+
else:
491+
error_msg += " (error in body)"
492+
else:
493+
error_msg += " (no error details available)"
494+
else:
495+
error_msg += " (no response)"
496+
self.assertTrue(response and response["success"], error_msg)
468497

469498
def launch(
470499
self,

lldb/test/API/tools/lldb-dap/attach/TestDAP_attach.py

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,13 @@
44

55
from lldbsuite.test.decorators import *
66
from lldbsuite.test.lldbtest import *
7-
from lldbsuite.test import lldbutil
8-
import lldbdap_testcase
97
import subprocess
108
import threading
119
import time
1210

11+
import lldbdap_testcase
12+
from lldbsuite.test import lldbutil
13+
1314

1415
def spawn_and_wait(program, delay):
1516
if delay:
@@ -227,3 +228,47 @@ def test_terminate_commands(self):
227228
pattern=terminateCommands[0],
228229
)
229230
self.verify_commands("terminateCommands", output, terminateCommands)
231+
232+
def test_session_id_update(self):
233+
program = self.build_and_create_debug_adapter_for_attach()
234+
self.process = subprocess.Popen(
235+
[program],
236+
stdin=subprocess.PIPE,
237+
stdout=subprocess.PIPE,
238+
stderr=subprocess.PIPE,
239+
)
240+
241+
postRunCommands = [
242+
"script print('Actual_Session_ID: ' + str(os.getenv('VSCODE_DEBUG_SESSION_ID')))"
243+
]
244+
self.attach(
245+
pid=self.process.pid,
246+
vscode_session_id="test_session_id",
247+
postRunCommands=postRunCommands,
248+
)
249+
output = self.get_console()
250+
lines = filter(lambda x: "Actual_Session_ID" in x, output.splitlines())
251+
self.assertTrue(
252+
any("test_session_id" in l for l in lines),
253+
"expect session id in console output",
254+
)
255+
256+
def test_session_id_update_empty(self):
257+
program = self.build_and_create_debug_adapter_for_attach()
258+
259+
self.spawn_thread = threading.Thread(
260+
target=spawn_and_wait,
261+
args=(program, 0.1),
262+
)
263+
self.spawn_thread.start()
264+
265+
postRunCommands = [
266+
"script print('Actual_Session_ID: ' + str(os.getenv('VSCODE_DEBUG_SESSION_ID', 'None')))"
267+
]
268+
self.attach(program=program, postRunCommands=postRunCommands)
269+
output = self.get_console()
270+
lines = filter(lambda x: "Actual_Session_ID" in x, output.splitlines())
271+
self.assertTrue(
272+
any("Actual_Session_ID: None" in l for l in lines),
273+
"expect session id in console output",
274+
)

0 commit comments

Comments
 (0)