Skip to content

Bug: SQLite VectorStore doesn't close connection to Database-File correctly #13375

@MarvinPogoda

Description

@MarvinPogoda

Describe the bug
When using the SQLite Vector Store Connector (e.g. SqliteCollection) the created Database-File is still accessed by the connector even though the collection is already disposed.
Accessing the file will then result in a "file is used by another process"-Exception.

To Reproduce
Steps to reproduce the behavior:
Create a process that is using this class and calls TestVectorStore.
The process shouldn't be terminating after the call.
Check if temp.db can be deleted.

using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.AI;
using Microsoft.Extensions.VectorData;
using Microsoft.SemanticKernel.Connectors.SqliteVec;

namespace VectorLiteTest;

public class VectorLiteTest
{
    public async Task TestVectorStore()
    {
        using (SqliteCollection<string, Data> collection =
               new SqliteCollection<string, Data>($"Data Source=./temp.db", "Collection",
                                                  new SqliteCollectionOptions()
                                                  {EmbeddingGenerator = new MockEmbeddingGenerator()}))
        {
            await collection.EnsureCollectionExistsAsync();
            Data[] dataList =
            [
                new Data
                {
                    Id = "Id",
                    Metadata = "Metadata"
                }
            ];
            await collection.UpsertAsync(dataList);
        }
    }

    private class MockEmbeddingGenerator : IEmbeddingGenerator<string, Embedding<float>>
    {
        public Task<Embedding<float>> GenerateEmbeddingAsync(string input,
                                                             CancellationToken cancellationToken = default)
        {
            return Task.FromResult(new Embedding<float>(new float[] {0.1f, 0.2f, 0.3f}));
        }

        public Task<GeneratedEmbeddings<Embedding<float>>> GenerateAsync(
            IEnumerable<string> values, EmbeddingGenerationOptions options = null,
            CancellationToken cancellationToken = new CancellationToken())
        {
            GeneratedEmbeddings<Embedding<float>> generatedEmbeddings = new GeneratedEmbeddings<Embedding<float>>();
            generatedEmbeddings.Add(new Embedding<float>(new float[] {0.1f, 0.2f, 0.3f}));
            return Task.FromResult(generatedEmbeddings);
        }

        public void Dispose()
        {
            //DO NOTHING
        }

        public object GetService(Type serviceType, object serviceKey = null)
        {
            return null;
        }
    }

    private class Data
    {
        [VectorStoreKey] public string Id { get; set; }

        [VectorStoreData] public string Metadata { get; set; } = string.Empty;

        [VectorStoreVector(Dimensions: 3)] public string Embedding => Metadata;
    }
}

Expected behavior
The .db should be able to be accessed/deleted after the SqliteCollection is closed

Platform

  • Language: [C#]
  • Source: [Microsoft.SemanticKernel.Connectors.SqliteVec Version 1.67.1-preview ]
  • AI model: doesn't matter
  • IDE: [Rider]
  • OS: [Windows]

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions