From d596330a2a357ec2d57eb37030fbee0d84c2f531 Mon Sep 17 00:00:00 2001 From: Cheuk <90270663+cheukt@users.noreply.github.com> Date: Tue, 3 Sep 2024 16:36:53 -0400 Subject: [PATCH] RSDK-6985 - Filter out SetHeader errors (#4338) --- operation/web.go | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/operation/web.go b/operation/web.go index d290312055b..aa63d1c0f7f 100644 --- a/operation/web.go +++ b/operation/web.go @@ -56,7 +56,16 @@ func (m *Manager) UnaryServerInterceptor( ctx, done := m.CreateFromIncomingContext(ctx, info.FullMethod) defer done() if op := Get(ctx); op != nil && op.ID.String() != "" { - utils.UncheckedError(grpc.SetHeader(ctx, metadata.MD{opidMetadataKey: []string{op.ID.String()}})) + // SetHeader will occasionally error because of a data race if the request has been cancelled from client side. + // The cancel signal (RST_STREAM) is processed on a separate goroutine and will close the existing gRPC stream, + // which will end up writing headers and returning a message to the server. If headers were sent before SetHeader + // is called here, SetHeader will error. + // Since the behavior is expected and part of the gRPC stream closing, only log the error if it is unexpected + // (the context error is nil, meaning request wasn't cancelled). + if err := grpc.SetHeader(ctx, metadata.MD{opidMetadataKey: []string{op.ID.String()}}); err != nil && + ctx.Err() == nil { + m.logger.CDebugw(ctx, "error while setting header", "err", err) + } } return handler(ctx, req) }