Skip to content
Merged
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
29 changes: 16 additions & 13 deletions presto-native-execution/presto_cpp/main/http/HttpClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -212,12 +212,12 @@ class ResponseHandler : public proxygen::HTTPTransactionHandler {
}

void setTransaction(proxygen::HTTPTransaction* txn) noexcept override {
if (txn) {
protocol_ = txn->getTransport().getCodec().getProtocol();
}
txn_ = CHECK_NOTNULL(txn);
protocol_ = txn_->getTransport().getCodec().getProtocol();
}

void detachTransaction() noexcept override {
txn_ = nullptr;
self_.reset();
}

Expand Down Expand Up @@ -269,12 +269,14 @@ class ResponseHandler : public proxygen::HTTPTransactionHandler {
self_.reset();
}

void sendRequest(proxygen::HTTPTransaction* txn) {
txn->sendHeaders(request_);
if (!body_.empty()) {
txn->sendBody(folly::IOBuf::wrapBuffer(body_.c_str(), body_.size()));
void sendRequest() {
if (txn_) {
txn_->sendHeaders(request_);
if (!body_.empty()) {
txn_->sendBody(folly::IOBuf::wrapBuffer(body_.c_str(), body_.size()));
}
txn_->sendEOM();
}
txn->sendEOM();
}

private:
Expand All @@ -288,6 +290,7 @@ class ResponseHandler : public proxygen::HTTPTransactionHandler {
std::shared_ptr<ResponseHandler> self_;
std::shared_ptr<HttpClient> client_;
std::optional<proxygen::CodecProtocol> protocol_;
proxygen::HTTPTransaction* txn_{nullptr};
};

// Responsible for making an HTTP request. The request will be made in 2
Expand Down Expand Up @@ -354,10 +357,8 @@ class ConnectionHandler : public proxygen::HTTPConnector::Callback {
http2InitialStreamWindow_, http2StreamWindow_, http2SessionWindow_);
session->setMaxConcurrentOutgoingStreams(maxConcurrentStreams_);
}
auto txn = session->newTransaction(responseHandler_.get());
if (txn) {
responseHandler_->sendRequest(txn);
}
session->newTransaction(responseHandler_.get());
responseHandler_->sendRequest();
Comment on lines +360 to +361
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

issue (bug_risk): Order of newTransaction and sendRequest may introduce subtle bugs if transaction is not set synchronously.

Ensure txn_ is set on responseHandler_ before calling sendRequest, or add synchronization to prevent race conditions.


sessionPool_->putSession(session);
delete this;
Expand Down Expand Up @@ -535,7 +536,9 @@ void HttpClient::sendRequest(std::shared_ptr<ResponseHandler> responseHandler) {
auto doSend = [this, responseHandler = std::move(responseHandler)](
proxygen::HTTPTransaction* txn) {
if (txn) {
responseHandler->sendRequest(txn);
// txn may no longer be valid, ::onError and ::detachTransaction would
// have been called. In such case, ::sendRequest will be no-op
responseHandler->sendRequest();
return;
}
VLOG(2) << "Create new connection to " << address_.describe();
Expand Down
Loading