Skip to content

fix(auth): hold the HTTP client weakly in OAuth providers - #435

Merged
cathleeny merged 4 commits into
mainfrom
cathleeny/fix-oauth-http-client-cycle
Nov 19, 2026
Merged

cathleeny merged 4 commits into
mainfrom
cathleeny/fix-oauth-http-client-cycle

Conversation

@cathleeny

@cathleeny cathleeny commented Nov 10, 2026 •

Copy link
Copy Markdown
Collaborator

Bug: Closing an OAuth session never freed its HTTP client. DatabricksHttpClient owns its auth provider, and the M2M, M2M private-key and U2M providers (plus the Azure service principal provider built on M2M) each held a strong Arc<DatabricksHttpClient> back to it. Runs into the same issueToo many open files as ES-2348909, and #434 alone does not fix it for OAuth.

Changes:

  • Providers now store a Weak and upgrade it only when a token fetch actually runs, matching FederationProvider.
  • Cached-token lookups never touch the client, and a background refresh that runs after the session closes fails quietly (refresh errors are already ignored).

Testing

  • New provider_does_not_retain_http_client tests for M2M, JWT M2M and U2M: install the provider on a client, drop the client, and assert both are freed.
  • New M2M test: once the client is gone, get_auth_header returns an error instead of panicking.
  • Local repro (not committed) that mirrors the PyO3 setup: a multi-thread runtime per session, 30 OAuth M2M open/close cycles, counting /proc/self/fd. With only fix(session): free the transport when a session closes #434, FDs grew by 120 (60 epoll, 30 eventfd, 30 socket). With fix(session): free the transport when a session closes #434 and this PR together they grew by 0, and PAT sessions stayed at 0.
  • cargo fmt --check, cargo clippy --all-targets -- -D warnings and cargo test pass locally on Rust 1.97.1.

`DatabricksHttpClient` owns its auth provider, and the M2M, M2M
private-key and U2M providers (and the Azure service principal provider
built on M2M) each held a strong `Arc<DatabricksHttpClient>` back to it.
The pair formed a reference cycle, so closing an OAuth session leaked the
HTTP client, the provider and the tokio runtime handle the provider
captures. In the Python binding, which runs one runtime per session, that
handle keeps the runtime's FDs open, the same symptom as the transport
cycle fixed in #434.

The providers now store a `Weak` and upgrade it only when a token fetch
runs, matching `FederationProvider`. Their constructors take
`&Arc<DatabricksHttpClient>` so the caller keeps ownership, and a shared
`upgrade_http_client` helper replaces federation's inline upgrade.

Signed-off-by: cathleeny <58714163+cathleeny@users.noreply.github.com>
Co-authored-by: Isaac <no-reply@databricks.com>

@oss-reviewer-bot oss-reviewer-bot Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

✅ No issues identified by the review bot.

Resolve the NEXT_CHANGELOG.md conflict by keeping both Fixed entries.

Signed-off-by: cathleeny <58714163+cathleeny@users.noreply.github.com>
Co-authored-by: Isaac <no-reply@databricks.com>

@oss-reviewer-bot oss-reviewer-bot Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🔀 Cross-reviewed: two independent reviews (Claude Opus 5.5 · GPT-6.1 Sol), checked by a judge model (Claude Sonnet 5.5).

Verdict: 1 High

The fix itself looks correct: holding the client weakly breaks the client↔provider cycle for M2M, JWT, U2M and Azure SP, and the new tests cover it. One medium concern: the public constructor signatures and their semantics change on a 1.x crate.

Comment thread src/auth/oauth/m2m.rs Resolved
Label the constructor signature change as a Rust API break, as v1.2.0 did for TelemetryConfig::enabled, and note the invalid_state error after the client is dropped.

Signed-off-by: cathleeny <58714163+cathleeny@users.noreply.github.com>
Co-authored-by: Isaac <no-reply@databricks.com>

@oss-reviewer-bot oss-reviewer-bot Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🔀 Cross-reviewed: two independent reviews (Claude Opus 5.5 · GPT-6.1 Sol), checked by a judge model (Claude Sonnet 5.5).

Verdict: 1 Low · 1 Nit

Looks good, with 1 low concern. Switching the providers to Weak breaks the client↔provider cycle the same way FederationProvider does. The cached-token path never upgrades the client, and every caller in the crate has been updated to the new &Arc signatures.

This overview is Claude Opus 5.5's initial take; GPT-6.1 Sol added findings it doesn't mention — see the findings below.

Other findings

  • ⚪ Nit — The test asserts only the message substring, though the changelog promises an invalid_state error. Add an assertion on err.code being the invalid-state code. (Found by Claude Opus 5.5 · confirmed by the judge)

Comment thread src/auth/oauth/m2m.rs Resolved
The changelog promises a token fetch after the client is dropped fails
with invalid_state; check the error code, not only the message.

Co-authored-by: Isaac <no-reply@databricks.com>
Signed-off-by: cathleeny <58714163+cathleeny@users.noreply.github.com>

@oss-reviewer-bot oss-reviewer-bot Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🔀 Cross-reviewed: two independent reviews (Claude Opus 5.5 · GPT-6.1 Sol), checked by a judge model (Claude Sonnet 5.5).

Verdict: 1 Medium

Looks good, with 1 medium concern. Holding the client through a Weak breaks the cycle. Upgrades happen only inside the refresh closures, so cached-token lookups never touch the client, and the new tests check that both objects are freed. The one concern is that the public constructor signatures changed in a 1.x crate.

Comment thread src/auth/oauth/m2m.rs Resolved
@cathleeny
cathleeny added this pull request to the merge queue Nov 19, 2026
Merged via the queue into main with commit 223ef56 Nov 19, 2026
33 of 34 checks passed

This branch was previously deployed

1 inactive deployment
azure-prod — c7d6a36e Deployed Nov 19, 2026 by cathleeny via followup #3755
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants