-
Notifications
You must be signed in to change notification settings - Fork 14.4k
[lldb] Support non-blocking reads in JSONRPCTransport #144610
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -85,7 +85,8 @@ class JSONTransport { | |
|
||
/// Reads the next message from the input stream. | ||
template <typename T> | ||
llvm::Expected<T> Read(const std::chrono::microseconds &timeout) { | ||
llvm::Expected<T> | ||
Read(std::optional<std::chrono::microseconds> timeout = std::nullopt) { | ||
llvm::Expected<std::string> message = ReadImpl(timeout); | ||
if (!message) | ||
return message.takeError(); | ||
|
@@ -97,10 +98,20 @@ class JSONTransport { | |
|
||
virtual llvm::Error WriteImpl(const std::string &message) = 0; | ||
virtual llvm::Expected<std::string> | ||
ReadImpl(const std::chrono::microseconds &timeout) = 0; | ||
ReadImpl(std::optional<std::chrono::microseconds> timeout) = 0; | ||
|
||
llvm::Expected<std::string> | ||
ReadFull(IOObject &descriptor, size_t length, | ||
std::optional<std::chrono::microseconds> timeout) const; | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. And if these are members, then they probably don't need to take the IOObject arg. |
||
llvm::Expected<std::string> | ||
ReadUntil(IOObject &descriptor, llvm::StringRef delimiter, | ||
std::optional<std::chrono::microseconds> timeout); | ||
|
||
lldb::IOObjectSP m_input; | ||
lldb::IOObjectSP m_output; | ||
|
||
std::string m_buffer; | ||
}; | ||
|
||
/// A transport class for JSON with a HTTP header. | ||
|
@@ -113,7 +124,7 @@ class HTTPDelimitedJSONTransport : public JSONTransport { | |
protected: | ||
virtual llvm::Error WriteImpl(const std::string &message) override; | ||
virtual llvm::Expected<std::string> | ||
ReadImpl(const std::chrono::microseconds &timeout) override; | ||
ReadImpl(std::optional<std::chrono::microseconds> timeout) override; | ||
|
||
// FIXME: Support any header. | ||
static constexpr llvm::StringLiteral kHeaderContentLength = | ||
|
@@ -131,7 +142,7 @@ class JSONRPCTransport : public JSONTransport { | |
protected: | ||
virtual llvm::Error WriteImpl(const std::string &message) override; | ||
virtual llvm::Expected<std::string> | ||
ReadImpl(const std::chrono::microseconds &timeout) override; | ||
ReadImpl(std::optional<std::chrono::microseconds> timeout) override; | ||
|
||
static constexpr llvm::StringLiteral kMessageSeparator = "\n"; | ||
}; | ||
|
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -27,9 +27,9 @@ using namespace lldb_private; | |||||
|
||||||
/// ReadFull attempts to read the specified number of bytes. If EOF is | ||||||
/// encountered, an empty string is returned. | ||||||
static Expected<std::string> | ||||||
ReadFull(IOObject &descriptor, size_t length, | ||||||
std::optional<std::chrono::microseconds> timeout = std::nullopt) { | ||||||
Expected<std::string> JSONTransport::ReadFull( | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could we keep this a |
||||||
IOObject &descriptor, size_t length, | ||||||
std::optional<std::chrono::microseconds> timeout) const { | ||||||
if (!descriptor.IsValid()) | ||||||
return llvm::make_error<TransportInvalidError>(); | ||||||
|
||||||
|
@@ -67,19 +67,22 @@ ReadFull(IOObject &descriptor, size_t length, | |||||
return data.substr(0, length); | ||||||
} | ||||||
|
||||||
static Expected<std::string> | ||||||
ReadUntil(IOObject &descriptor, StringRef delimiter, | ||||||
std::optional<std::chrono::microseconds> timeout = std::nullopt) { | ||||||
std::string buffer; | ||||||
buffer.reserve(delimiter.size() + 1); | ||||||
while (!llvm::StringRef(buffer).ends_with(delimiter)) { | ||||||
Expected<std::string> | ||||||
JSONTransport::ReadUntil(IOObject &descriptor, StringRef delimiter, | ||||||
std::optional<std::chrono::microseconds> timeout) { | ||||||
if (!timeout || *timeout != std::chrono::microseconds::zero()) { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why should this depend on the timeout value? If you previously timed out reading something (with a non-zero timeout) and you try to read again, who says the result is going to be better if you discard the data you've read so far? |
||||||
m_buffer.clear(); | ||||||
m_buffer.reserve(delimiter.size() + 1); | ||||||
} | ||||||
|
||||||
while (!llvm::StringRef(m_buffer).ends_with(delimiter)) { | ||||||
Expected<std::string> next = | ||||||
ReadFull(descriptor, buffer.empty() ? delimiter.size() : 1, timeout); | ||||||
ReadFull(descriptor, m_buffer.empty() ? delimiter.size() : 1, timeout); | ||||||
Comment on lines
+73
to
+80
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If we have a buffer, we could adjust our approach to read in larger than 1 byte chunks when we're reading until a delimiter. We could read chunks of say 1024 and then split the buffer on the delimited until we run out of data and then do a new read with the next chunk size. I don't know if this approach would have issues on windows or anything though, so maybe someone with more platform specific knowledge may know how it handles blocking reads if There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/read The win32 _read API for reference. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think I don't understand the question. A blocking read with no data... blocks, sort of by definition. |
||||||
if (auto Err = next.takeError()) | ||||||
return std::move(Err); | ||||||
buffer += *next; | ||||||
m_buffer += *next; | ||||||
} | ||||||
return buffer.substr(0, buffer.size() - delimiter.size()); | ||||||
return m_buffer.substr(0, m_buffer.size() - delimiter.size()); | ||||||
} | ||||||
|
||||||
JSONTransport::JSONTransport(IOObjectSP input, IOObjectSP output) | ||||||
|
@@ -89,11 +92,15 @@ void JSONTransport::Log(llvm::StringRef message) { | |||||
LLDB_LOG(GetLog(LLDBLog::Host), "{0}", message); | ||||||
} | ||||||
|
||||||
Expected<std::string> | ||||||
HTTPDelimitedJSONTransport::ReadImpl(const std::chrono::microseconds &timeout) { | ||||||
Expected<std::string> HTTPDelimitedJSONTransport::ReadImpl( | ||||||
std::optional<std::chrono::microseconds> timeout) { | ||||||
if (!m_input || !m_input->IsValid()) | ||||||
return llvm::make_error<TransportInvalidError>(); | ||||||
|
||||||
if (timeout && *timeout == std::chrono::microseconds::zero()) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
this should also work |
||||||
return llvm::createStringError( | ||||||
"HTTPDelimitedJSONTransport does not support non-blocking reads"); | ||||||
|
||||||
IOObject *input = m_input.get(); | ||||||
Expected<std::string> message_header = | ||||||
ReadFull(*input, kHeaderContentLength.size(), timeout); | ||||||
|
@@ -104,7 +111,8 @@ HTTPDelimitedJSONTransport::ReadImpl(const std::chrono::microseconds &timeout) { | |||||
kHeaderContentLength, *message_header) | ||||||
.str()); | ||||||
|
||||||
Expected<std::string> raw_length = ReadUntil(*input, kHeaderSeparator); | ||||||
Expected<std::string> raw_length = | ||||||
ReadUntil(*input, kHeaderSeparator, timeout); | ||||||
if (!raw_length) | ||||||
return handleErrors(raw_length.takeError(), | ||||||
[&](const TransportEOFError &E) -> llvm::Error { | ||||||
|
@@ -117,7 +125,7 @@ HTTPDelimitedJSONTransport::ReadImpl(const std::chrono::microseconds &timeout) { | |||||
return createStringError( | ||||||
formatv("invalid content length {0}", *raw_length).str()); | ||||||
|
||||||
Expected<std::string> raw_json = ReadFull(*input, length); | ||||||
Expected<std::string> raw_json = ReadFull(*input, length, timeout); | ||||||
if (!raw_json) | ||||||
return handleErrors( | ||||||
raw_json.takeError(), [&](const TransportEOFError &E) -> llvm::Error { | ||||||
|
@@ -143,7 +151,7 @@ Error HTTPDelimitedJSONTransport::WriteImpl(const std::string &message) { | |||||
} | ||||||
|
||||||
Expected<std::string> | ||||||
JSONRPCTransport::ReadImpl(const std::chrono::microseconds &timeout) { | ||||||
JSONRPCTransport::ReadImpl(std::optional<std::chrono::microseconds> timeout) { | ||||||
if (!m_input || !m_input->IsValid()) | ||||||
return make_error<TransportInvalidError>(); | ||||||
|
||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,7 +16,7 @@ using namespace lldb_private; | |
namespace { | ||
template <typename T> class JSONTransportTest : public PipeTest { | ||
protected: | ||
std::unique_ptr<JSONTransport> transport; | ||
std::unique_ptr<T> transport; | ||
|
||
void SetUp() override { | ||
PipeTest::SetUp(); | ||
|
@@ -36,7 +36,13 @@ class HTTPDelimitedJSONTransportTest | |
using JSONTransportTest::JSONTransportTest; | ||
}; | ||
|
||
class JSONRPCTransportTest : public JSONTransportTest<JSONRPCTransport> { | ||
class TestJSONRPCTransport : public JSONRPCTransport { | ||
public: | ||
using JSONRPCTransport::JSONRPCTransport; | ||
using JSONRPCTransport::WriteImpl; // For partial writes. | ||
}; | ||
|
||
class JSONRPCTransportTest : public JSONTransportTest<TestJSONRPCTransport> { | ||
public: | ||
using JSONTransportTest::JSONTransportTest; | ||
}; | ||
|
@@ -84,7 +90,6 @@ TEST_F(HTTPDelimitedJSONTransportTest, ReadWithEOF) { | |
Failed<TransportEOFError>()); | ||
} | ||
|
||
|
||
TEST_F(HTTPDelimitedJSONTransportTest, InvalidTransport) { | ||
transport = std::make_unique<HTTPDelimitedJSONTransport>(nullptr, nullptr); | ||
ASSERT_THAT_EXPECTED( | ||
|
@@ -142,13 +147,43 @@ TEST_F(JSONRPCTransportTest, Write) { | |
} | ||
|
||
TEST_F(JSONRPCTransportTest, InvalidTransport) { | ||
transport = std::make_unique<JSONRPCTransport>(nullptr, nullptr); | ||
transport = std::make_unique<TestJSONRPCTransport>(nullptr, nullptr); | ||
ASSERT_THAT_EXPECTED( | ||
transport->Read<JSONTestType>(std::chrono::milliseconds(1)), | ||
Failed<TransportInvalidError>()); | ||
} | ||
|
||
#ifndef _WIN32 | ||
TEST_F(HTTPDelimitedJSONTransportTest, NonBlockingRead) { | ||
ASSERT_THAT_EXPECTED( | ||
transport->Read<JSONTestType>(std::chrono::microseconds::zero()), | ||
llvm::FailedWithMessage( | ||
"HTTPDelimitedJSONTransport does not support non-blocking reads")); | ||
} | ||
|
||
TEST_F(JSONRPCTransportTest, NonBlockingRead) { | ||
llvm::StringRef head = R"({"str")"; | ||
llvm::StringRef tail = R"(: "foo"})" | ||
"\n"; | ||
|
||
ASSERT_THAT_EXPECTED(input.Write(head.data(), head.size()), Succeeded()); | ||
ASSERT_THAT_EXPECTED( | ||
transport->Read<JSONTestType>(std::chrono::microseconds::zero()), | ||
Failed<TransportTimeoutError>()); | ||
|
||
ASSERT_THAT_EXPECTED(input.Write(tail.data(), tail.size()), Succeeded()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we also test a chunk with the delimiter in the middle of the data? |
||
while (true) { | ||
llvm::Expected<JSONTestType> result = | ||
transport->Read<JSONTestType>(std::chrono::microseconds::zero()); | ||
if (result.errorIsA<TransportTimeoutError>()) { | ||
llvm::consumeError(result.takeError()); | ||
continue; | ||
} | ||
ASSERT_THAT_EXPECTED(result, HasValue(testing::FieldsAre(/*str=*/"foo"))); | ||
break; | ||
} | ||
} | ||
|
||
TEST_F(HTTPDelimitedJSONTransportTest, ReadWithTimeout) { | ||
ASSERT_THAT_EXPECTED( | ||
transport->Read<JSONTestType>(std::chrono::milliseconds(1)), | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since this type has moved into
lldb_private
now, should we use theTimeout
helper? https://github.com/llvm/llvm-project/blob/main/lldb/include/lldb/Utility/Timeout.h#L28