Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

context: Clarify whether Values are retained after cancellation #57565

Open
paddycarver opened this issue Jan 4, 2023 · 3 comments
Open

context: Clarify whether Values are retained after cancellation #57565

paddycarver opened this issue Jan 4, 2023 · 3 comments
Labels
Documentation NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Milestone

Comments

@paddycarver
Copy link

paddycarver commented Jan 4, 2023

What version of Go are you using (go version)?

$ go version
go version go1.19 darwin/arm64

Also present on tip.

Does this issue reproduce with the latest release?

Yes

What operating system and processor architecture are you using (go env)?

Darwin arm64, but very confident it is not a system/processor architecture-specific issue.

What did you do?

Check https://pkg.go.dev/context#WithCancel

What did you expect to see?

Whether the Values of a Context are the resources released when a Context is canceled.

What did you see instead?

// Canceling this context releases resources associated with it, so code should
// call cancel as soon as the operations running in this Context complete.

Issue template taken care of, the crux of this issue is that I was browsing #19643 and realized that my problem with canceled Contexts--that once canceled, I can't count on accessing any of the Values I've stored in them using WithValue (logger, etc) anymore--wasn't one people were talking about. Reading that issue, it honestly seemed like people way more familiar with Go than I am were of the distinct impression you could access Values after canceling a Context.

So I dug into why I thought I couldn't, and tracked it back to that comment--that resources associated with the Context would released. But on rereading, I'm noting it doesn't say Values.... but it doesn't not say Values, either?

My two goals for this ticket, of which one would suffice but two would be ideal, are:

  1. Get clarification on whether the "resources" in question include Values or not.
  2. Update that comment to include that clarification, perhaps as
    // Canceling this context releases resources associated with it, so code should
    // call cancel as soon as the operations running in this Context complete. Data
    // associated with the context using WithValues will be retained after cancellation.
    
    or
    // Canceling this context releases resources associated with it, including data set
    // by WithValues, so code should call cancel as soon as the operations running in
    // this Context complete.
    
    depending on the clarification.

I'm willing to accept that I'm the only one with this confusion and therefore it doesn't need to be in the docs, I just don't want to accidentally rely on implementation details. :)

Thanks!

[edit] Play link as a demonstration, in case I'm being unclear: https://go.dev/play/p/Fw9O23Agl7c

@ianlancetaylor
Copy link
Contributor

CC @Sajmani

@seankhliao seankhliao added Documentation NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. labels Jan 4, 2023
@Sajmani
Copy link
Contributor

Sajmani commented Jan 4, 2023

There is no predetermined relationship between cancelation and context values, so neither of the proposed comments is quite right. For example, a context value might contain an auth token that becomes invalid after the corresponding request is canceled. This invalidation will be sometime after the context is canceled but not synchronized with it. On the other hand, a context value might contain a request-scoped logger that remains useful while the request is running, even after the context is canceled, allowing the request handler to log cleanup activity. I'm inclined to leave the docs unchanged unless you believe we can make this clearer by explicitly saying "There is no predetermined relationship between cancelation and context values".

@paddycarver
Copy link
Author

I'm inclined to leave the docs unchanged unless you believe we can make this clearer by explicitly saying "There is no predetermined relationship between cancelation and context values".

I think that sentence would have prevented my confusion that led to me opening this issue. I have no idea how likely other people are to be confused by this, though; whether I had a reasonable reading, or read too far into it in a way others are unlikely to.

I'll defer to y'all's expertise on which you think is more likely and will be satisfied with the resolution of this issue either way, namely knowing that canceling a context has no bearing on whether its values continue to be accessible or not.

@seankhliao seankhliao added this to the Unplanned milestone Jan 20, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Documentation NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Projects
None yet
Development

No branches or pull requests

4 participants