Skip to content

Commit fd22a07

Browse files
ashgtiSquallATF
authored andcommitted
[lldb-dap] Addressing the order of events during disconnect to flush output. (llvm#128583)
The TestDAP_ouput test is flaky due to the order of events during shutdown. We were stopping the output and error handle redirection after we finished the disconnect request, which can cause us to miss output events due to timing. Moving when we stop the redirection to ensure we have consistent output prior to disconnect responding. Fixes llvm#128567
1 parent 22ff938 commit fd22a07

File tree

3 files changed

+12
-7
lines changed

3 files changed

+12
-7
lines changed

lldb/test/API/tools/lldb-dap/output/TestDAP_output.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ def test_output(self):
1616
program = self.getBuildArtifact("a.out")
1717
self.build_and_launch(
1818
program,
19+
disconnectAutomatically=False,
1920
exitCommands=[
2021
# Ensure that output produced by lldb itself is not consumed by the OutputRedirector.
2122
"?script print('out\\0\\0', end='\\r\\n', file=sys.stdout)",
@@ -33,6 +34,9 @@ def test_output(self):
3334

3435
self.continue_to_exit()
3536

37+
# Disconnecting from the server to ensure any pending IO is flushed.
38+
self.dap_server.request_disconnect()
39+
3640
output += self.get_stdout(timeout=lldbdap_testcase.DAPTestCaseBase.timeoutval)
3741
self.assertTrue(output and len(output) > 0, "expect program stdout")
3842
self.assertIn(

lldb/tools/lldb-dap/DAP.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -223,10 +223,7 @@ llvm::Error DAP::ConfigureIO(std::FILE *overrideOut, std::FILE *overrideErr) {
223223
return llvm::Error::success();
224224
}
225225

226-
void DAP::StopIO() {
227-
out.Stop();
228-
err.Stop();
229-
226+
void DAP::StopEventHandlers() {
230227
if (event_thread.joinable()) {
231228
broadcaster.BroadcastEventByType(eBroadcastBitStopEventThread);
232229
event_thread.join();
@@ -853,13 +850,17 @@ lldb::SBError DAP::Disconnect(bool terminateDebuggee) {
853850

854851
SendTerminatedEvent();
855852

853+
// Stop forwarding the debugger output and error handles.
854+
out.Stop();
855+
err.Stop();
856+
856857
disconnecting = true;
857858

858859
return error;
859860
}
860861

861862
llvm::Error DAP::Loop() {
862-
auto stop_io = llvm::make_scope_exit([this]() { StopIO(); });
863+
auto cleanup = llvm::make_scope_exit([this]() { StopEventHandlers(); });
863864
while (!disconnecting) {
864865
llvm::json::Object object;
865866
lldb_dap::PacketStatus status = GetNextObject(object);

lldb/tools/lldb-dap/DAP.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -228,8 +228,8 @@ struct DAP {
228228
llvm::Error ConfigureIO(std::FILE *overrideOut = nullptr,
229229
std::FILE *overrideErr = nullptr);
230230

231-
/// Stop the redirected IO threads and associated pipes.
232-
void StopIO();
231+
/// Stop event handler threads.
232+
void StopEventHandlers();
233233

234234
// Serialize the JSON value into a string and send the JSON packet to
235235
// the "out" stream.

0 commit comments

Comments
 (0)