Skip to content

Conversation

@gkatz2
Copy link

@gkatz2 gkatz2 commented Jan 30, 2026

Summary

Fix OAuth token refresh failure that occurs when the access token expires (~1 hour), requiring users to manually reauthenticate in the browser.

Problem

The TokenSource created in processToken() was using the OAuth flow's startup context, which gets cancelled when the callback server shuts down after authentication completes. When the token expires and refresh is attempted, the HTTP request fails with "context canceled".

This triggers a full re-authentication flow with new DCR client registration, requiring user interaction.

Solution

Use context.Background() for the TokenSource instead of the OAuth flow's context, since the TokenSource is long-lived and must remain valid for token refresh operations long after the OAuth flow completes.

Testing

  • Added TestTokenRefreshAfterContextCancellation unit test that:
    • Creates a TokenSource with a cancellable context
    • Cancels the context (simulating OAuth flow completion)
    • Verifies token refresh still succeeds
  • Verified manually with Datadog MCP server over 1+ hour session

Related

Closes #3538

Complements #3418 (DCR credential persistence) - that fix handles restarts, this fix handles runtime token refresh.

The TokenSource created in processToken() was using the OAuth flow's
startup context, which gets cancelled when the callback server shuts down.
When the access token expires (~1 hour later) and refresh is attempted,
the HTTP request fails with "context canceled".

This triggers a full re-authentication flow with new DCR client
registration, requiring user interaction in the browser.

The fix uses context.Background() for the TokenSource, since it is
long-lived and must remain valid for token refresh operations long
after the OAuth flow completes.

Closes stacklokgh-3538

Signed-off-by: Greg Katz <gkatz@indeed.com>
amirejaz
amirejaz previously approved these changes Jan 30, 2026
@github-actions github-actions bot added the size/XS Extra small PR: < 100 lines changed label Jan 30, 2026
@amirejaz
Copy link
Contributor

@gkatz2 There is a linter error which needs to be fixed.

Screenshot 2026-01-30 at 22 17 54

@codecov
Copy link

codecov bot commented Jan 30, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 65.34%. Comparing base (7d02b34) to head (1d0de9b).
⚠️ Report is 1 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #3539   +/-   ##
=======================================
  Coverage   65.34%   65.34%           
=======================================
  Files         403      403           
  Lines       39278    39278           
=======================================
  Hits        25665    25665           
  Misses      11628    11628           
  Partials     1985     1985           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@gkatz2
Copy link
Author

gkatz2 commented Jan 31, 2026

@amirejaz Fixed the linter error in 23f8b61 - renamed the unused ctx parameter to _.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size/XS Extra small PR: < 100 lines changed

Projects

None yet

Development

Successfully merging this pull request may close these issues.

OAuth token refresh fails, requiring periodic manual reauthentication

2 participants