Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions lldb/include/lldb/Host/JSONTransport.h
Original file line number Diff line number Diff line change
Expand Up @@ -259,9 +259,11 @@ template <typename Proto> class IOTransport : public JSONTransport<Proto> {
if (!m_buffer.empty())
handler.OnError(llvm::make_error<TransportUnhandledContentsError>(
std::string(m_buffer.str())));
// Move the read handle to a local before notifying the handler. The
// handler may destroy this transport (e.g. by erasing it from a
// connection map), so accessing members after OnClosed() is unsafe.
auto read_handle = std::move(m_read_handle);
handler.OnClosed();
// On EOF, remove the read handle from the MainLoop.
m_read_handle.reset();
}
}

Expand Down
18 changes: 18 additions & 0 deletions lldb/unittests/Host/JSONTransportTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -479,6 +479,15 @@ TEST_F(HTTPDelimitedJSONTransportTest, ReadWithEOF) {
ASSERT_THAT_ERROR(Run(), Succeeded());
}

TEST_F(HTTPDelimitedJSONTransportTest, ReadWithEOFDestroyTransportOnClose) {
input.CloseWriteFileDescriptor();
EXPECT_CALL(message_handler, OnClosed()).WillOnce([this]() {
transport.reset();
loop.RequestTermination();
});
ASSERT_THAT_ERROR(loop.Run().takeError(), Succeeded());
}

TEST_F(HTTPDelimitedJSONTransportTest, ReaderWithUnhandledData) {
std::string json = R"json({"str": "foo"})json";
std::string message =
Expand Down Expand Up @@ -588,6 +597,15 @@ TEST_F(JSONRPCTransportTest, ReadWithEOF) {
ASSERT_THAT_ERROR(Run(), Succeeded());
}

TEST_F(JSONRPCTransportTest, ReadWithEOFDestroyTransportOnClose) {
input.CloseWriteFileDescriptor();
EXPECT_CALL(message_handler, OnClosed()).WillOnce([this]() {
transport.reset();
loop.RequestTermination();
});
ASSERT_THAT_ERROR(loop.Run().takeError(), Succeeded());
}

TEST_F(JSONRPCTransportTest, ReaderWithUnhandledData) {
std::string message = R"json({"req": "foo")json";
// Write an incomplete message and close the handle.
Expand Down