Skip to content

Package importer: coordinate in-place imports with forced swaps, and bound DirLock held files #156

Closed
pnpm/pnpm
#17182

Description

@zkochan

Follow-ups left by pnpm/pnpm#17091 (concurrent forced imports into one node_modules/.pnpm), raised by the review bots there and verified against the code.

  1. A non-forced private import is not coordinated with a forced swap. In pacquet, an import of an absent private target populates it in place without taking the per-target swap lock, while a concurrent forced import of the same target (holding the lock) removes the target before renaming its stage in. A forced and a non-forced install of the same project running at once can therefore interrupt the in-place import with NotFound errors, or leave its last files inside the replacement tree. This predates the PR (the old stage-and-swap removed the target the same way); the PR serializes forced swaps among themselves only. Taking the swap lock on the in-place path would put a lock acquisition on the cold-install hot path of the isolated linker, so it needs a design decision: a cheaper claim protocol for private targets, or accepting that mixed forced/non-forced installs of one project are unsupported.

  2. DirLock leaves one held file per lock key, forever. pnpm_fs::DirLock proves a holder's liveness through an OS lock on <user lock root>/pnpm-dir-locks-<uid>/<hash>.held, keyed by the lock path, and never removes that file. The global virtual store slot lock already leaves one per built slot. The importer's swap lock (PR 17091) adds one per forced import of an occupied target, so pnpm install --force over an existing node_modules leaves one file per package per project path under $XDG_RUNTIME_DIR (a size-capped tmpfs on Linux) or LocalAppData on Windows. Options: remove the held file on release with an inode check so a waiter that opened the old inode falls back to the age-based judgement, or key the swap lock more coarsely.


Written by an agent (Claude Code, claude-fable-5-1).

Activity

  1. zkochan commented on Oct 18, 2026

    @zkochan
    MemberAuthor

    Two more residuals from the same PR, both Windows-only after pnpm/pnpm#17091 landed the atomic exchange for Linux and macOS:

    1. The move-aside fallback leaves the target absent between two renames. Windows has no atomic dirent exchange, so replace_target moves the occupied target aside and renames the stage in. An importer that found the target absent and renames its own stage in without the swap lock can land in that vacancy if it stalled with its stage ready. Its tree is equivalent and the swap that follows keeps or replaces it, but writes it makes between returning and that replacement are lost. Closing it means either a Windows exchange primitive or taking the swap lock on the absent-target path on Windows, which costs a lock and a permanent held file per package of a fresh hoisted install.

    2. The TypeScript multi-process forced-import test is skipped on Windows. importIndexedDir.concurrent.test.ts runs four forced imports of one package in separate processes on POSIX. On Windows a rename onto an occupied directory fails with EPERM, which rename-overwrite answers by removing the target and retrying for up to a minute per process, so four contending processes exceed the test budget. That path predates the PR; concurrent forced imports on Windows go through it and deserve their own look.


    Written by an agent (Claude Code, claude-fable-5-1).

  2. zkochan commented on Oct 18, 2026

    @zkochan
    MemberAuthor

    pnpm/pnpm#17113 addresses items 2 (as far as the importer is concerned) and 3: the importer's swap lock becomes one lock per install, keyed by the install's modules directory, so a forced reinstall leaves one held file per install rather than one per package, and the absent-target fast path holds it too, which closes the move-aside fallback's vacancy for forced importers. DirLock itself still leaves one held file per key (item 2 for the slot build lock), and items 1 and 4 remain open.


    Written by an agent (Claude Code, claude-fable-5-1).

  3. zkochan commented on Oct 19, 2026

    @zkochan
    MemberAuthor

    pnpm/pnpm#17113 merged as 2e734a64cf. Status of the four items:

    1. Forced swaps vs. non-forced in-place imports: open. Still needs a design that does not put a lock acquisition on the isolated linker's cold-install hot path.
    2. DirLock held files: the importer now leaves one per install (keyed by the modules directory, held for the linking phase only) instead of one per package. The primitive itself still never removes a held file, so the global virtual store slot lock keeps leaving one per built slot; that part is open.
    3. Move-aside vacancy on Windows: closed for forced importers, which now take turns for their whole linking phase. An importer outside an install (patch, deploy, cargo and config dependencies) still replaces unserialized.
    4. The multi-process TypeScript test is still POSIX-only; rename-overwrite's EPERM path on Windows is unchanged.

    Written by an agent (Claude Code, claude-fable-5-1).

  4. self-assigned this
    on Oct 20, 2026
  5. zkochan commented on Oct 21, 2026

    @zkochan
    MemberAuthor

    Opened pnpm/pnpm#17182 to resolve this issue across both Rust (pnpm-deps-restorer and pnpm-fs) and TypeScript (@pnpm/fs.indexed-pkg-importer):

    1. Coordinate non-forced private imports with forced swaps (pnpm-deps-restorer):
      • Pass swap_lock: Some(swap_lock) on all slot_import_opts branches in create_virtual_dir_by_snapshot.rs.
      • Stage absent private targets (!opts.safe_to_skip) and attempt a lock-free rename_with_retry(&paths.stage, dir_path) when !opts.force so uncontended cold installs never acquire ModulesSwapLock, falling back to swap_lock.hold() only when the target was concurrently occupied while staging (leaving a completed directory or non-directory dirent in place when !opts.force).
      • Hold swap_lock before repairing an existing incomplete private target (repair_existing_dir, re-checking marker_present under the lock) or replacing a non-directory dirent at a private target (replace_non_dir).
    2. Bound DirLock .held files (pnpm-fs):
      • Unlink <hash>.held on release while holding its exclusive OS lock (LockedHeldFile::remove_and_unlock), and verify via path_still_names after try_lock() in try_lock_held_file_once that the locked descriptor still names held_path on disk so a waiter that opened a just-unlinked inode reopens the current path while dead-holder takeover stays instant.
    3. Coordinate forced & non-forced imports on Windows (@pnpm/fs.indexed-pkg-importer):
      • Replace rename-overwrite in commitStagedDir with overwriteDirSync (which moves an existing directory aside on both POSIX and Windows using a swap path distinct from fastPathTemp(newDir) instead of deleting it in place and spinning for 60s on Windows EPERM), guard tryExclusiveImport's rimrafSync(newDir) cleanup with sameCreatedDirectory, and enable the multi-process concurrent forced import test on Windows.

    This covers the full issue.

    Written by an agent (Antigravity, Gemini 4 Argon).

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

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions