Skip to content

A write after ensureLoaded() evicts an expired record corrupts the secondary index #2937

Open

Description

@kriszyp

Writing a record right after ensureLoaded() evicted it for expiry corrupts the table's secondary index on both storage engines. It needs a table with no source, a TTL expiration, and a resource method that calls this.ensureLoaded() before writing. On an expired record, ensureLoaded() takes the no-source expiry branch of ensureLoadedFromSource (resources/Table.ts), which evicts the record by nulling entry.value on the loaded entry object in place. The write that follows then diffs its index entries against that mutated entry instead of the stored record.

Reproduce

Observed on main at 2d81ebc (Node 24.21). Target.evict is stubbed so the eviction does not land, which leaves the stored record as the only correct base:

const Target = table({
	database: 'repro',
	table: 'Target',
	attributes: [{ name: 'id', isPrimaryKey: true }, { name: 'value', indexed: true }],
});
Target.setTTLExpiration({ expiration: 0.005 });
Target.evict = () => Promise.resolve();
await Target.put('expired', { value: 1 });
await new Promise((resolve) => setTimeout(resolve, 20));
class LoadsFirst extends Target {
	async put(data, target) {
		await this.ensureLoaded();
		return super.put(data, target);
	}
}
await LoadsFirst.put('expired', { value: 2 });
// search value = 1 / value = 2:
//   RocksDB: value 1 still returns 'expired' (orphaned index entry)
//   LMDB:    value 2 returns nothing (missing index entry)

Expected

The record is { value: 2 } and only the value = 2 index entry exists. The in-place entry.value = null in the expiry branch is the likely root: an entry object the resource (and, on LMDB, the write's ifVersion base) still holds should not be mutated. Evicting through a fresh entry, or reloading the write base after an eviction, would avoid it.

Found while testing a write-path change that reuses the pre-load as the commit base (which is correct on RocksDB for this case); this reproduces on main without it.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    Fields

    Priority

    P2

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions