Skip to content

Rescanning databases keeps a stale LMDB environment after its .mdb is removed or replaced #4041

Open

Description

@cb1kenobi

Problem

On the LMDB engine, a database rescan (getDatabases() in resources/databases.ts) keeps using an environment it already opened, even after that database's .mdb file was removed or replaced on disk. readMetaDb takes lmdbDatabaseEnvs.get(path) when one is cached, so later loads read (and write) through the old environment, which still points at the unlinked file.

The sweep that should have marked those environments for deletion never worked: it iterated Object.values(lmdbDatabaseEnvs), but lmdbDatabaseEnvs is a Map, so the loop body never ran (since dcda017, 2025-07-11). #3920 removed that dead loop without changing behavior.

Impact

  • LMDB only (RocksDB, the v5 default, is not affected).
  • Needs the .mdb replaced or removed behind a running Harper, for example a manual file-level restore. Harper's own close paths (closeDatabase) delete the cached environment first.
  • Silent: reads keep returning the old data, and writes land in a file that is no longer reachable by name.

Fix sketch

On rescan, compare each cached environment's file identity (dev/inode) with the path on disk, and close and drop an environment whose file is gone or replaced, through the same close ordering closeDatabase uses (root once, never its dbis). A unit test replaces an open .mdb, rescans, and expects the new contents.

Raised by @kriszyp in review of #3920 as pre-existing.

🤖 Generated by Claude Opus 5.5; posted via @cb1kenobi.

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