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

oauth2.Config captures ctx and re-uses it later #388

Open
vangent opened this issue Jun 26, 2019 · 5 comments
Open

oauth2.Config captures ctx and re-uses it later #388

vangent opened this issue Jun 26, 2019 · 5 comments

Comments

@vangent
Copy link

vangent commented Jun 26, 2019

https://github.com/golang/oauth2/blob/master/oauth2.go#L239

For example, if I do something like this:

  ctx, cancel := context.WithCancel(context.Background())
  ts, err := google.DefaultTokenSource(ctx)
  cancel()
  tkn, err := ts.Token()

the last line will fail every time because the HTTP request isn't made until then, using the ctx I provided to DefaultTokenSource. Somewhere near here: https://github.com/golang/oauth2/blob/master/oauth2.go#L265

This is very surprising and should at least be documented clearly on DefaultTokenSource, FindDefaultCredentials, etc.

#262 is possibly related.

/cc @jba @zombiezen

@vangent
Copy link
Author

vangent commented Jun 26, 2019

Introduced in https://golang.org/cl/45370.

@vangent
Copy link
Author

vangent commented Jun 26, 2019

/cc @broady

@broady
Copy link
Contributor

broady commented Jun 26, 2019 via email

@jacobsevart
Copy link

We just got bit by the same problem, causing all requests to fail once the initial token expired, since the context used to attempt the refresh had expired after application startup.

Suggestion: reuseTokenSource should refuse to construct if the underlying token source has a context deadline.

jacobsevart added a commit to jacobsevart/oauth2 that referenced this issue Feb 25, 2021
The context given to Client() will be stored and reused for token
refreshes througout the life of the Config. If the context expires e.g.
during application startup, refresh attempts will fail.

Updates golang#388
@jacobsevart
Copy link

Hi folks, I've uploaded a pull request to document this issue and report a more useful error.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants