Skip to content

[Bug]: Unable to Close Persistent Client #5868

@tnorlund

Description

@tnorlund

What happened?

What happened: SQLite file locking issues causing corruption
What you expected: A close() method or context manager support
Current workaround: The fragile GC-based approach you're using
Proposed solution: Adding proper resource management

I'm using AWS's S3 to quickly move chroma DBs around. I'm corrupting the SQLite db while attempting to upload to S3. Here's my workaround thus far.

import os
import tempfile
import shutil
import boto3
import chromadb
from pathlib import Path

def lambda_handler(event, context):
    """AWS Lambda handler that creates ChromaDB delta and uploads to S3."""
    temp_dir = tempfile.mkdtemp()
    bucket = "my-chromadb-bucket"

    try:
        # Create ChromaDB client and collection
        chroma_client = chromadb.PersistentClient(path=temp_dir)
        collection = chroma_client.get_or_create_collection("words")

        # Add some embeddings
        collection.upsert(
            ids=["word_1", "word_2"],
            embeddings=[[0.1, 0.2, 0.3], [0.4, 0.5, 0.6]],
            documents=["hello", "world"],
            metadatas=[{"pos": 1}, {"pos": 2}]
        )

        # PROBLEM: ChromaDB client is still open, SQLite files are locked
        # No close() method available: chroma_client.close()  # ❌ AttributeError

        # Try to upload SQLite files to S3
        s3_client = boto3.client("s3")
        persist_path = Path(temp_dir)

        for file_path in persist_path.rglob("*"):
            if file_path.is_file():
                s3_key = f"snapshots/{file_path.name}"
                # ❌ This fails or uploads corrupted files because SQLite is locked
                s3_client.upload_file(str(file_path), bucket, s3_key)
                # Error: "unable to open database file" or corrupted database

        # Even setting client to None doesn't help immediately
        chroma_client = None  # SQLite connections still open!

        # Files may still be locked here, causing corruption when uploaded

    finally:
        # Cleanup
        shutil.rmtree(temp_dir, ignore_errors=True)

Versions

Versions: ChromaDB >=1.3.3, Python 3.12.0, macOS/Linux

Relevant log output

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions