Skip to content

Commit

Permalink
fix: preventing dangling pointer for a ResponseHandler when an error …
Browse files Browse the repository at this point in the history
…occurred (#469)

Summary:
On big chunk data, if an error occurs, a dangling pointer issue can rise for the `ResponseHandler` due to the usage of `runInEventBaseThread`.

Pull Request resolved: #469

Reviewed By: mjoras

Differential Revision: D50937161

Pulled By: afrind

fbshipit-source-id: c12abf8ead4cdaf9c5c243c33fa37344fb0ccd97
  • Loading branch information
PooyaEimandar authored and facebook-github-bot committed Nov 27, 2023
1 parent 3179ce7 commit 293687a
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
13 changes: 10 additions & 3 deletions proxygen/httpserver/samples/static/StaticHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ namespace StaticService {
*/

void StaticHandler::onRequest(std::unique_ptr<HTTPMessage> headers) noexcept {
error_ = false;
if (headers->getMethod() != HTTPMethod::GET) {
ResponseBuilder(downstream_)
.status(400, "Bad method")
Expand Down Expand Up @@ -76,13 +77,18 @@ void StaticHandler::readFile(folly::EventBase* evb) {
// done
file_.reset();
VLOG(4) << "Read EOF";
evb->runInEventBaseThread(
[this] { ResponseBuilder(downstream_).sendWithEOM(); });
evb->runInEventBaseThread([this] {
if (!error_) {
ResponseBuilder(downstream_).sendWithEOM();
}
});
break;
} else {
buf.postallocate(rc);
evb->runInEventBaseThread([this, body = buf.move()]() mutable {
ResponseBuilder(downstream_).body(std::move(body)).send();
if (!error_) {
ResponseBuilder(downstream_).body(std::move(body)).send();
}
});
}
}
Expand Down Expand Up @@ -136,6 +142,7 @@ void StaticHandler::requestComplete() noexcept {
}

void StaticHandler::onError(ProxygenError /*err*/) noexcept {
error_ = true;
finished_ = true;
paused_ = true;
checkForCompletion();
Expand Down
2 changes: 2 additions & 0 deletions proxygen/httpserver/samples/static/StaticHandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

#pragma once

#include <atomic>
#include <folly/File.h>
#include <folly/Memory.h>
#include <proxygen/httpserver/RequestHandler.h>
Expand Down Expand Up @@ -45,6 +46,7 @@ class StaticHandler : public proxygen::RequestHandler {
bool readFileScheduled_{false};
std::atomic<bool> paused_{false};
bool finished_{false};
std::atomic<bool> error_{false};
};

} // namespace StaticService

0 comments on commit 293687a

Please sign in to comment.