Skip to content

fix(sysdb): delete embedding_metadata_array rows on collection delete - #7635

Open
nandinitiw wants to merge 1 commit into
chroma-core:mainfrom
nandinitiw:fix/delete-collection-list-metadata-leak
Open

nandinitiw wants to merge 1 commit into
chroma-core:mainfrom
nandinitiw:fix/delete-collection-list-metadata-leak

Conversation

@nandinitiw

Copy link
Copy Markdown

Fixes #7594

Problem

After delete_collection(), a newly created collection's first record can silently inherit list-valued metadata from the deleted collection's first record. Scalar metadata is cleaned up correctly — only list-valued metadata leaks, and it happens whether or not the new collection reuses the deleted one's name, and survives a process restart (genuine on-disk state, not a cache).

Root cause

delete_collection_with_conn in rust/sysdb/src/sqlite.rs deletes scalar embedding_metadata rows but never touches embedding_metadata_array — the separate table introduced in migration 00006-metadata-array-support.sqlite.sql for list-valued metadata (one row per array element).

Combined with embeddings.id being declared INTEGER PRIMARY KEY without AUTOINCREMENT (migration 00001-embedding-metadata.sqlite.sql), SQLite is free to reuse that rowid once the table empties out. So:

  1. Original collection's first record gets embeddings.id = 1; its list metadata (e.g. {"tags": ["stale"]}) is written to embedding_metadata_array with id = 1.
  2. delete_collection wipes the embeddings row (id = 1 gone) but leaves the orphaned embedding_metadata_array row with id = 1 behind.
  3. A newly created collection's first inserted record can also get embeddings.id = 1 (id space is reused once the table is empty/low).
  4. The metadata segment's get() query (rust/segment/src/sqlite_metadata.rs) joins embedding_metadata_array purely on id, with no segment/collection scoping — it finds the orphaned row and merges the deleted collection's list metadata into the new record's result.

Fix

Add the missing DELETE FROM embedding_metadata_array to delete_collection_with_conn, scoped and ordered identically to the existing embedding_metadata delete (must run before the embeddings rows are removed, since it resolves ids via a join against embeddings).

Testing

Added test_delete_collection_cleans_array_metadata in rust/sysdb/src/sqlite.rs: inserts an embeddings row and an embedding_metadata_array row directly against a collection's segment, deletes the collection via sysdb.delete_collection, and asserts embedding_metadata_array is empty afterward.

Verified the test is a real regression guard, not a false positive — reverted the fix locally and confirmed the test fails (left: 1, right: 0) against the buggy code, then confirmed it passes once the fix is restored.

Full chroma-sysdb (19 tests) and chroma-segment (83 tests, including all existing array-metadata tests) suites pass with no regressions. cargo fmt --check and cargo clippy -- -D warnings are both clean.

delete_collection_with_conn deleted scalar embedding_metadata but never
touched embedding_metadata_array (added later for list-valued metadata
support in migration 00006). Combined with embeddings.id being declared
without AUTOINCREMENT (so SQLite can reuse the rowid after all rows are
deleted), a record in a newly created collection can be assigned the
same internal id as a deleted collection's record, and the metadata
segment's get() query - which joins embedding_metadata_array purely on
that id with no segment/collection scoping - resurfaces the orphaned
list-valued metadata from the deleted collection.

Adds the matching DELETE FROM embedding_metadata_array, scoped and
ordered identically to the existing embedding_metadata delete (must
run before embeddings rows are removed, since it resolves ids via a
join against embeddings).

Regression test inserts an embeddings row + embedding_metadata_array
row directly against a collection's segment, deletes the collection,
and asserts embedding_metadata_array is empty afterward - fails
without the fix (1 orphaned row) and passes with it.

Fixes chroma-core#7594

@claude claude Bot left a comment

Copy link
Copy Markdown
Contributor

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

Copy link
Copy Markdown

Reviewer Checklist

Please leverage this checklist to ensure your code review is thorough before approving

Testing, Bugs, Errors, Logs, Documentation

  • Can you think of any use case in which the code does not behave as intended? Have they been tested?
  • Can you think of any inputs or external events that could break the code? Is user input validated and safe? Have they been tested?
  • If appropriate, are there adequate property based tests?
  • If appropriate, are there adequate unit tests?
  • Should any logging, debugging, tracing information be added or removed?
  • Are error messages user-friendly?
  • Have all documentation changes needed been made?
  • Have all non-obvious changes been commented?

System Compatibility

  • Are there any potential impacts on other parts of the system or backward compatibility?
  • Does this change intersect with any items on our roadmap, and if so, is there a plan for fitting them together?

Quality

  • Is this code of a unexpectedly high quality (Readability, Modularity, Intuitiveness)

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: delete_collection leaves list-valued metadata behind, which reappears on records written to the next collection

1 participant