Skip to content

Parent context unexpectedly expires with child #30287

@inliquid

Description

@inliquid

I'm using context with net/http.Request. Recently I made a mistake:

ctx, cancel := context.WithTimeout(r.Context(), timeout)
defer cancel()
<...>
next.ServeHTTP(w, r) // <-- child context has not been passed

So here is something missing, either
next.ServeHTTP(w, r.WithContext(ctx)) or
r = r.WithContext(ctx)

However, what I found is that it doesn't change anything. So in called handler I can run this code:

select {
    <...>
    case <-r.Context().Done():
    fmt.Println("Parent context expired?")
    <...>
}

and see this message as output.

Parent context expired?

Metadata

Metadata

Assignees

No one assigned

    Labels

    FrozenDueToAgeWaitingForInfoIssue is not actionable because of missing required information, which needs to be provided.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions