Skip to content

fix(session): free the transport when a session closes - #434

Merged
vuanhphung merged 2 commits into
mainfrom
cathleeny/fix-transport-reader-factory-cycle
Nov 22, 2026
Merged

vuanhphung merged 2 commits into
mainfrom
cathleeny/fix-transport-reader-factory-cycle

Conversation

@cathleeny

@cathleeny cathleeny commented Nov 10, 2026 •

Copy link
Copy Markdown
Collaborator

Bug: Closing a Session never freed its transport. ResultReaderFactory held a strong Arc<dyn Transport> back to the SeaRestTransport that owns it, so the two formed a reference cycle. The Python binding runs one tokio runtime per session, and the leaked runtime handle keeps that runtime's IO driver FDs open, so a customer on 1.3.0 hit OSError: [Errno 24] Too many open files.

Changes:

  • factory now holds a Weak reference and upgrades it when it builds a CloudFetch or inline-streaming reader.
  • Readers still take a strong reference, so a result stream that outlives Session::close keeps working.
  • The factory is only reachable through the transport that owns it, so the upgrade can't fail while it is in use.
  • The C ABI and Node bindings share one process-wide runtime, so for them the leak was per-session memory rather than FDs; this fixes both.

Testing

  • New close_releases_transport test opens and closes a session against wiremock and asserts the transport is dropped. It fails on main and passes with this change.
  • Local repro (not committed) that mirrors the PyO3 setup: a multi-thread runtime per session, 30 open/close cycles, counting /proc/self/fd. On main FDs grew by 120 (60 epoll, 30 eventfd, 30 socket, four per session); with this change they grew by 0.
  • cargo fmt --check, cargo clippy --all-targets -- -D warnings and cargo test pass locally on Rust 1.97.1.

`ResultReaderFactory` held a strong `Arc<dyn Transport>` back to the
`SeaRestTransport` that owns it, so the two formed a reference cycle and
the transport was never dropped. Every closed session leaked its
transport, HTTP client and tokio runtime handle. The Python binding runs
one tokio runtime per session, and a live handle keeps that runtime's IO
driver open after shutdown: two epoll fds, one eventfd and one signal
socket per closed session, until the process hits `Too many open files`.

The factory now holds a `Weak` reference and upgrades it when it builds a
CloudFetch or inline-streaming reader. Readers keep a strong reference,
so an open result stream still works after `Session::close`.

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.

@vuanhphung

Copy link
Copy Markdown
Collaborator

There's a similar issue of OAuth providers still retain the HTTP client and form a cycle, which can be fixed separately

@cathleeny
cathleeny enabled auto-merge November 10, 2026 22:32
vuanhphung pushed a commit that referenced this pull request Nov 20, 2026
Dependency from PR #434 (e8920d8), adapted to current main. Exclusive HTTP-pool checkout needs the transport-reader cycle broken so closed sessions can return their clients.

Signed-off-by: cathleeny <58714163+cathleeny@users.noreply.github.com>
Signed-off-by: Vu Anh Phung <vu.phung@databricks.com>
Co-authored-by: Isaac <no-reply@databricks.com>
Resolve the NEXT_CHANGELOG.md conflict by keeping the entries from both sides.

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 Nit

Looks good, with 1 low concern. Switching the factory to a Weak reference breaks the cycle. The factory is reachable only through the transport's &self and there is no Drop path that uses it, so upgrade() cannot fail while the factory is in use.

Other findings

  • ⚪ Nit — The new test covers only open and close with no statement run. It doesn't cover a result stream that outlives Session::close, which should keep working and free the transport once dropped. Add a wiremock test that runs a statement, closes the session, reads the stream, drops it, and asserts upgrade().is_none(). (Found by Claude Opus 5.5 · confirmed by the judge)

vuanhphung added a commit that referenced this pull request Nov 20, 2026
Keep the original entry from #434 and remove this branch duplicate. Verified that exactly one transport-cycle entry remains and the reuse opt-out documentation is preserved.

Signed-off-by: Vu Anh Phung <vu.phung@databricks.com>
Co-authored-by: Isaac <no-reply@databricks.com>
@vuanhphung
vuanhphung added this pull request to the merge queue Nov 22, 2026
Merged via the queue into main with commit f17c240 Nov 22, 2026
29 of 30 checks passed
vuanhphung added a commit that referenced this pull request Nov 22, 2026
Keep the original entry from #434 and remove this branch duplicate. Verified that exactly one transport-cycle entry remains and the reuse opt-out documentation is preserved.

Signed-off-by: Vu Anh Phung <vu.phung@databricks.com>
Co-authored-by: Isaac <no-reply@databricks.com>

This branch was previously deployed

1 inactive deployment
azure-prod — 62bee0c1 Deployed Nov 20, 2026 by cathleeny via followup #3923
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