grpc.io icon indicating copy to clipboard operation
grpc.io copied to clipboard

Cancellation: discuss server-side cancellation of the request context when handler completes

Open matttproud opened this issue 1 year ago • 0 comments

Hi folks,

I was reviewing https://grpc.io/docs/guides/cancellation/ to better learn the semantics of context.Context lifetime semantics in the server-side handlers. The document currently mentions only what happens client-side, but we know (through experience) that in grpc-go that the gRPC library cancels the context.Context server-side once the handler completes.

If you modify the example from the basics guide, you can demonstrate this:

func (s *routeGuideServer) GetFeature(ctx context.Context, req *pb.Point) (*pb.Feature, error) {
	context.AfterFunc(ctx, func() { log.Println("canceled") })
}

I would like to propose modifying minimally the cancellation guide and possibly the basics guide to convey this fact. The standard net/http library does this with (*http.Request).Context, which is better than nothing, but not very visibly. I'd like to take a similar approach here. This is a pretty key thing for users to know in terms of designing their servers correctly.

I'd be happy to do the documentation cleanup myself; I am just filing the bug to signal intent and give the maintainers a chance to chime in if they want this cleanup done in a different way.

matttproud avatar Jul 22 '24 11:07 matttproud