Skip to content
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

Fix the lifetime of InferPayload #241

Merged
merged 12 commits into from
May 17, 2023
24 changes: 12 additions & 12 deletions src/infer_payload.cc
Original file line number Diff line number Diff line change
Expand Up @@ -33,28 +33,28 @@ InferPayload::InferPayload(
std::function<void(std::unique_ptr<InferResponse>)> callback)
: is_decoupled_(is_decoupled), is_promise_set_(false), callback_(callback)
{
prev_promise_.reset(new std::promise<std::unique_ptr<InferResponse>>());
}

InferPayload::~InferPayload()
{
prev_promise_.reset();
promise_.reset(new std::promise<std::unique_ptr<InferResponse>>());
}

void
InferPayload::SetValueForPrevPromise(
std::unique_ptr<InferResponse> infer_response)
InferPayload::SetValue(std::unique_ptr<InferResponse> infer_response)
{
prev_promise_->set_value(std::move(infer_response));
prev_promise_.reset();
is_promise_set_ = true;
// Only set value to the promise with the first response. Call the callback
// function to send decoupled response to the stub.
std::lock_guard<std::mutex> lock(mutex_);
nnshah1 marked this conversation as resolved.
Show resolved Hide resolved
if (!is_promise_set_) {
is_promise_set_ = true;
promise_->set_value(std::move(infer_response));
} else {
Callback(std::move(infer_response));
}
}

void
InferPayload::SetFuture(
std::future<std::unique_ptr<InferResponse>>& response_future)
{
response_future = prev_promise_->get_future();
response_future = promise_->get_future();
}

bool
Expand Down
6 changes: 3 additions & 3 deletions src/infer_payload.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,8 @@ class InferPayload {
InferPayload(
const bool is_decouple,
std::function<void(std::unique_ptr<InferResponse>)> callback);
~InferPayload();

void SetValueForPrevPromise(std::unique_ptr<InferResponse> infer_response);
void SetValue(std::unique_ptr<InferResponse> infer_response);
void SetFuture(std::future<std::unique_ptr<InferResponse>>& response_future);
bool IsDecoupled();
bool IsPromiseSet();
Expand All @@ -60,8 +59,9 @@ class InferPayload {
std::shared_ptr<ResponseAllocatorUserp> ResponseAllocUserp();

private:
std::unique_ptr<std::promise<std::unique_ptr<InferResponse>>> prev_promise_;
std::unique_ptr<std::promise<std::unique_ptr<InferResponse>>> promise_;
bool is_decoupled_;
std::mutex mutex_;
bool is_promise_set_;
std::function<void(std::unique_ptr<InferResponse>)> callback_;
std::shared_ptr<ResponseAllocatorUserp> response_alloc_userp_;
Expand Down
8 changes: 1 addition & 7 deletions src/request_executor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -177,13 +177,7 @@ InferResponseComplete(
output_tensors, pb_error, true /* is_last_response */, userp /* id */);
}

// Only set value to the promise with the first response. Call the callback
// function to send decoupled response to the stub.
if (p->IsPromiseSet()) {
p->Callback(std::move(infer_response));
} else {
p->SetValueForPrevPromise(std::move(infer_response));
}
p->SetValue(std::move(infer_response));
}

TRITONSERVER_Error*
Expand Down