Skip to content

ServerHooks Context bug: Context from Error is not passed to ResponseSent #267

@mickeyreiss

Description

@mickeyreiss

There seems to be a bug in the go code generation that leads to context updates in Error server hooks failing to reach the ResponseSent hook.

if n, err := resp.Write(respBytes); err != nil {
msg := fmt.Sprintf("failed to write response, %d of %d bytes written: %s", n, len(respBytes), err.Error())
twerr := twirp.NewError(twirp.Unknown, msg)
callError(ctx, s.hooks, twerr)
}
callResponseSent(ctx, s.hooks)

callError on line 276 returns a new context, which is ignored, rather than overriding ctx.

This behavior is critical for finishing spans correctly in the twirp/datadog integration. In general, this behavior is critical any time the error hook wants to pass state down to the response sent callback.

Edit: This bug only manifests in a rare edge case, when writing the response fails. Normal application errors go through writeError in the generated code, which does update ctx correctly based on callError before calling callResponseSent.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions