Skip to content

fix(python): return the session supplied to LanceDBConnection - #4433

Merged
Xuanwo merged 1 commit into
lancedb:mainfrom
canyang25:cursor/fix-connection-session-68bf
Oct 22, 2026
Merged

Xuanwo merged 1 commit into
lancedb:mainfrom
canyang25:cursor/fix-connection-session-68bf

Conversation

@canyang25

Copy link
Copy Markdown
Contributor

LanceDBConnection.session read AsyncConnection.session, but AsyncConnection has no session attribute, so access raised AttributeError. Store the session passed to the constructor and return that object.

Assisted by Cursor (Grok).

LanceDBConnection.session read AsyncConnection.session, but
AsyncConnection has no session attribute, so access raised
AttributeError. Store the session passed to the constructor and
return that object.

Assisted by Cursor (Grok).

@claude claude 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.

Claude Code Review

This pull request is from a fork — automated review is disabled. A repository maintainer can comment @claude review to run a one-time review.

@github-actions github-actions Bot added bug Something isn't working Python Python SDK labels Oct 5, 2026

@lancedb-gatekeeper lancedb-gatekeeper 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.

⚠️ Gate recommendation: approve with a non-blocking risk.

Returning the caller-supplied session fixes the attribute failure and keeps property inspection synchronous for direct and reconstructed connections. Callers that retain closed connection wrappers should account for the supplied session’s cache lifetime.

# The native connection does not expose the Python Session wrapper.
# Keep the object supplied by the caller so this property stays
# synchronous for debugger inspection.
self._session = session

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

A closed connection wrapper now retains its supplied session’s caches. The new strong reference survives close(), which only closes _conn. Passing a temporary Session and retaining the closed connection can therefore keep populated caches alive up to their configured limits.

This is non-blocking because dropping the closed wrapper releases its session reference. Clearing _session during close() is an alternative if explicit connection cleanup should release that reference.

Reproducer
import gc
import tempfile

import lancedb

with tempfile.TemporaryDirectory() as uri:
    db = lancedb.connect(
        uri,
        session=lancedb.Session(
            index_cache_size_bytes=1024 * 1024,
            metadata_cache_size_bytes=512 * 1024,
        ),
    )
    table = db.create_table("test", [{"id": i} for i in range(100)])
    table.to_arrow()
    del table
    gc.collect()
    before = db.session.size_bytes
    db.close()
    gc.collect()
    assert not db.is_open()
    print(before, db.session.size_bytes)

On this revision, the connection is closed but this prints 5133 5133: the populated session cache is still retained.

@lancedb-gatekeeper lancedb-gatekeeper Bot added K-approved Latest Gatekeeper recommendation permits acceptance. K-risk Latest Gatekeeper recommendation includes a non-blocking risk. labels Oct 5, 2026
@Xuanwo
Xuanwo merged commit 13df747 into lancedb:main Oct 22, 2026
4 checks passed
Xuanwo added a commit that referenced this pull request Oct 26, 2026
Fixes #4400.

Sync remote connections fail on `uri`, `len(db)`, and `name in db`, and
remote tables lack `namespace`, `id`, and `replace_field_metadata`.
Share the paginated connection helpers and table identifier contract
through the base classes, and delegate remote metadata replacement to
the existing async implementation. Unsupported remote session and
read-consistency properties raise explicit `NotImplementedError`s.

Reconciled with main at `3dd57978`, including the synchronous method
signatures from #4498. The local session fix has already landed in
#4433, so this PR preserves its supplied-session-or-None contract and
adds no Rust/PyO3 session access. Namespace connections retain their
writable properties.

Existing custom `Table` subclasses remain instantiable: `namespace`
defaults to a fresh empty list, and deprecated `replace_field_metadata`
delegates to `update_field_metadata` with `replace=True` while retaining
its `None` return. Native and remote overrides are preserved. Regression
tests instantiate an old-style subclass through normal ABC class
creation and verify identifiers, independent namespace lists,
deprecation, and exact metadata delegation.

Historical validation at `03c43f7b` before the latest main integration
and subclass compatibility repair, Ubuntu x86-64, Python 3.12.13:

- Rebuilt the native extension with the repository `ci` profile and
locked Python dependencies.
- 273 tests passed across `test_sync_api_parity.py`, `test_db.py`,
`test_remote_db.py`, and `test_session.py`.
- 84 namespace and catalog compatibility tests passed (357 tests total).
- Python SDK lint and formatting pass (158 files); changed-file checks
and `git diff --check` pass.
- Repository-wide lint/format checks report existing issues in unchanged
`docs/web-tests` files (49 lint findings and 11 formatting files). These
are outside this patch.

Remote tests use the existing HTTP fixtures through the rebuilt native
client; the original aarch64 server deployment was not recreated.

Validation after merging main `3dd57978` and the compatibility repair
(macOS ARM64, uv-managed Python 3.12.12, rebuilt editable native
extension):
- 60 focused parity, signature, and error-contract tests passed.
- 381 database, remote, session, namespace, catalog, and sync parity
tests passed; 2 platform-dependent tests skipped.
- The two new subclass regressions fail against the pre-repair
implementation with the expected abstract-member instantiation error,
then pass with the repair.
- Python SDK Ruff lint/format passes (167 files); repository-wide
findings remain confined to unchanged docs/web-tests (49 lint findings,
11 formatting files).

---------

Co-authored-by: Xuanwo <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working K-approved Latest Gatekeeper recommendation permits acceptance. K-risk Latest Gatekeeper recommendation includes a non-blocking risk. Python Python SDK

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants