Skip to content

fix(memory): save_compression_guidelines non-atomic version bump #1799

@bug-ops

Description

@bug-ops

Summary

save_compression_guidelines in crates/zeph-memory/src/sqlite/compression_guidelines.rs:63-74 uses a read-then-write pattern across two separate transactions to compute the next version number:

let (current_version, _) = self.load_compression_guidelines().await?;
let new_version = current_version + 1;
sqlx::query("INSERT INTO ... VALUES (?, ?, ?)").bind(new_version)...

The schema has no UNIQUE(version) constraint, so two concurrent callers would both insert version=2 silently.

Fix

Use INSERT INTO compression_guidelines SELECT COALESCE(MAX(version),0)+1, ... FROM compression_guidelines to compute and insert in a single atomic statement, or add a UNIQUE(version) constraint to the schema.

Notes

Only one background updater calls this in practice, so the race is unlikely. This is a correctness gap, not a live incident.

Identified by code reviewer (REVIEW-4) during ACON compression guidelines PR (#1647) review.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingenhancementNew feature or requestmemoryzeph-memory crate (SQLite)

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions