Lifetime issue in the docs
Closed this issue · 2 comments
This page in the docs provide code that looks like this
void repeatedly_request_example(agrpc::GrpcContext& grpc_context, example::v1::Example::AsyncService& service)
{
agrpc::repeatedly_request(
&example::v1::Example::AsyncService::RequestUnary, service,
AssociatedHandler{grpc_context.get_executor(),
[](auto&& request_context, auto&& executor)
{
auto& writer = request_context.responder();
example::v1::Response response;
agrpc::finish(writer, response, grpc::Status::OK,
asio::bind_executor(executor, [c = std::move(request_context)](bool) {}));
},
grpc_context.get_allocator()});
}
The response object inside the lambda won't outlive the finish operation and therefore will give rise to a bug. Suggestion
- Instantiate response with a
make_shared
and carry it with thefinish
completion handler, or useasio::consign
. - Use the Asio convention that all async functions have the
async_
prefix.
At the moment I only get segfault when using agrpc::repeatedly_request
and couldn't find the source of problem. I will investigate more and eventually open another issue.
Thanks for your report.
The example code is correct, agrpc::finish
serializes the response object before it returns. This is also mentioned in the documentation
gRPC does not take ownership or a reference to message and status, so it is safe to deallocate once finish returns.
Meaning this cannot be the source of the segfault that you are getting.
I chose not to prefix functions with async_
because I do not provide synchronous versions of them. There are some non-asynchronous functions in this library (not necessarily related to gRPC) which can be identified by not taking a CompletionToken as their last argument.
Ok thanks. I guess I have to read the docs with more attention. I will close the issue now and come back with a new one if necessary.