Skip to content

feat: add timeout to SMD token refresh and benchmark - #130

Open
synackd wants to merge 3 commits into
mainfrom
pr/b-token-refresh-timeout
Open

feat: add timeout to SMD token refresh and benchmark#130
synackd wants to merge 3 commits into
mainfrom
pr/b-token-refresh-timeout

Conversation

@synackd

@synackd synackd commented Aug 31, 2026

Copy link
Copy Markdown
Contributor

Description

Introduces a 10‑second timeout for SMD token refresh operations to prevent potential deadlocks when the OIDC token endpoint is slow or unresponsive. A new constant defaultRefreshTimeout is defined and used with context.WithTimeout in both RefreshToken and refreshTokenIfCurrent. A refreshLock mutex is added to serialize concurrent refresh attempts, ensuring only one refresh request proceeds at a time. Additionally, a benchmark BenchmarkRefreshToken (in smdclient_benchmark_test.go) measures the latency of the refresh path. Comprehensive tests have been added to verify correct behavior under concurrent requests. These changes improve robustness of the SMD client during token expiration handling.

Fixes #128 (comment)

Checklist

  • My code follows the style guidelines of this project
  • I have added/updated comments where needed
  • I have added tests that prove my fix is effective or my feature works
  • I have run make test (or equivalent) locally and all tests pass
  • I have updated the relevant documentation (CLI examples, man pages, README, other docs, etc.)
  • DCO Sign-off: All commits are signed off (git commit -s) with my real name and email
  • REUSE Compliance:
    • Each new/modified source file has SPDX copyright and license headers
    • Any non-commentable files include a <filename>.license sidecar
    • All referenced licenses are present in the LICENSES/ directory

Type of Change

  • Bug fix
  • New feature
  • Breaking change
  • Documentation update
  • Dependency update
  • Build system/CI

@synackd
synackd force-pushed the pr/b-token-refresh-timeout branch 2 times, most recently from 3360725 to ba43b84 Compare August 31, 2026 16:28
Introduce a 10‑second timeout for SMD token refresh operations to
prevent potential deadlocks when the OIDC token endpoint is slow or
unresponsive. A new constant defaultRefreshTimeout is defined and used
with context.WithTimeout in both RefreshToken and refreshTokenIfCurrent.
A refreshLock mutex is added to serialize concurrent refresh attempts,
ensuring only one refresh request proceeds at a time.  Additionally, a
benchmark BenchmarkRefreshToken (in smdclient_benchmark_test.go)
measures the latency of the refresh path. Comprehensive tests have been
added to verify correct behavior under concurrent requests. These
changes improve robustness of the SMD client during token expiration
handling.

Signed-off-by: Devon Bautista <17506592+synackd@users.noreply.github.com>
@synackd
synackd force-pushed the pr/b-token-refresh-timeout branch from ba43b84 to 206a36f Compare August 31, 2026 16:31
@synackd
synackd marked this pull request as ready for review August 31, 2026 16:44
Comment thread internal/smdclient/oidc.go Outdated
if err != nil {
return err
}
r, err := http.DefaultClient.Do(req)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shouldn't this be s.smdClient rather than http.DefaultClient to benefit from our custom configs?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure how I missed that... Fixed in 0a95d36. Added nil-check for the client, let me know if the error message should be revised.

defer s.accessTokenMutex.Unlock()
return s.refreshTokenLocked()
// Serialize refresh to avoid concurrent token fetches.
s.refreshLock.Lock()

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think s.refreshLock doesn't exist in this branch.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Whoops, somehow did not get committed (fe17047). I also somehow missed running go build to catch that...

Comment thread internal/smdclient/oidc.go Outdated
if err != nil {
return err
}
r, err := http.DefaultClient.Do(req)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s.SMDClient has a built-in http client that is optimized for SMD queries. It is probably better here.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Dumb error on my part... Addressed in 0a95d36.

Signed-off-by: Devon Bautista <17506592+synackd@users.noreply.github.com>
Signed-off-by: Devon Bautista <17506592+synackd@users.noreply.github.com>
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

Successfully merging this pull request may close these issues.

2 participants