Skip to content

fix(lockfile): wait for stream close before returning - #12408

Merged
zkochan merged 1 commit into
mainfrom
fix-win-tests
Jun 14, 2026
Merged

zkochan merged 1 commit into
mainfrom
fix-win-tests

Conversation

@zkochan

@zkochan zkochan commented Jun 14, 2026 •

Copy link
Copy Markdown
Member

Summary

  • wait for early-exited lockfile read streams to close before returning
  • add a regression test that immediately renames over a non-env lockfile after streamReadFirstYamlDocument() returns
  • add a patch changeset for @pnpm/lockfile.fs and pnpm

Fixes the Windows EPERM repro from https://github.com/pnpm/pnpm/actions/runs/27505503206/job/81298623383.

Tests

  • pnpm --filter @pnpm/lockfile.fs test
  • PNPM_REGISTRY_MOCK_PORT=7769 NODE_OPTIONS="$NODE_OPTIONS --experimental-vm-modules --disable-warning=ExperimentalWarning --disable-warning=DEP0169" pnpm exec jest test/install/dedupe.ts -t "uses the lockfile written by pacquet for post-install checks"
  • pre-push hook ran pnpm compile/bundle/lint metadata checks while pushing fix-win-tests

Written by an agent (Codex, GPT-5).

Summary by CodeRabbit

  • Bug Fixes
    • Resolved a resource cleanup issue in lockfile reading operations where file handles were not properly released in certain code paths. File handles are now correctly closed in all scenarios, including early-exit cases. This prevents subsequent file system operations on lockfiles from failing due to retained open file streams.

@qodo-free-for-open-source-projects

qodo-free-for-open-source-projects Bot commented Jun 14, 2026 •

Copy link
Copy Markdown

Code Review by Qodo

🐞 Bugs (0) 📘 Rule violations (0) 📎 Requirement gaps (0)

Grey Divider

Great, no issues found!

Qodo reviewed your code and found no material issues that require review

Grey Divider

Qodo Logo

@coderabbitai

coderabbitai Bot commented Jun 14, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro Plus

Run ID: 6f55b51b-b442-4257-8250-c3c29f97bb61

📥 Commits

Reviewing files that changed from the base of the PR and between 5f63458 and f62f84c.

📒 Files selected for processing (3)
  • .changeset/slow-windows-lockfile-streams.md
  • lockfile/fs/src/yamlDocuments.ts
  • lockfile/fs/test/yamlDocuments.test.ts

📝 Walkthrough

Walkthrough

streamReadFirstYamlDocument in lockfile/fs/src/yamlDocuments.ts is updated so all early-exit and normal-exit paths call a new closeStream helper that awaits the close event after calling stream.destroy(), preventing file descriptor leaks on Windows. A regression test and patch-level changeset entry are included.

Changes

Lockfile read stream teardown fix

Layer / File(s) Summary
closeStream helper and updated exit paths
lockfile/fs/src/yamlDocuments.ts
Adds a closeStream(stream: ReadStream) helper that guards against double-close and awaits the close event, then wires it into every early-exit and normal-exit branch of streamReadFirstYamlDocument, replacing inline stream.destroy() calls.
Regression test and changeset
lockfile/fs/test/yamlDocuments.test.ts, .changeset/slow-windows-lockfile-streams.md
Adds a test that asserts streamReadFirstYamlDocument returns null for a non---- lockfile and verifies the file handle is released by performing a renameSync after the call. Includes a patch-level changeset for @pnpm/lockfile.fs and pnpm.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Possibly related PRs

  • pnpm/pnpm#11654: Modifies the same streamReadFirstYamlDocument function in lockfile/fs/src/yamlDocuments.ts, adding CRLF→LF normalization in the YAML chunk parsing path.
  • pnpm/pnpm#11605: Touches the same lockfile YAML first-document extraction logic that this PR hardens with proper stream teardown.

Suggested labels

area: lockfile

🐇 A stream left open on Windows would stall,
No rename could happen, no write, no install at all.
Now closeStream awaits the close event's chime,
Each file descriptor freed every single time.
Hop hop — the lockfile rewrites right on cue! 🎉

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 50.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title 'fix(lockfile): wait for stream close before returning' directly describes the main change: ensuring lockfile read streams fully close before returning.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix-win-tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@qodo-free-for-open-source-projects

Copy link
Copy Markdown

PR Summary by Qodo

Fix lockfile YAML streaming to await stream close on early exit (Windows EPERM)
🐞 Bug fix 🧪 Tests ⚙️ Configuration changes 🕐 10-20 Minutes

Grey Divider

Walkthroughs

Description
• Await ReadStream close when exiting early from env-lockfile prefix detection.
• Prevent Windows EPERM by releasing file handles before lockfile rewrites/renames.
• Add regression test and patch changeset for pnpm + @pnpm/lockfile.fs.
Diagram
graph TD
  A["Lockfile read/write callers"] --> B["streamReadFirstYamlDocument()"] --> C["closeStream()"] --> D["node:fs ReadStream"] --> E[("Filesystem")]
  A --> F["write-file-atomic rename/replace"] --> E
Loading
High-Level Assessment

The following are alternative approaches to this PR:

1. Use stream/promises.finished() to await shutdown
  • ➕ Standard Node primitive for awaiting stream completion/closure
  • ➕ Avoids manual 'close' event wiring
  • ➖ Still needs explicit destroy for early-exit cases
  • ➖ Behavior nuances across Node versions may need validation
2. Switch to fs.open + incremental reads (no ReadStream)
  • ➕ Deterministic handle lifetime via try/finally close
  • ➕ Avoids stream event ordering and early-return hazards
  • ➖ More code and complexity to manage buffering and encoding
  • ➖ Loses some of the ergonomic benefits of async iterators over streams

Recommendation: The current approach (explicitly destroying the ReadStream and awaiting its close event on all early-return/error paths) is a good minimal fix for the Windows EPERM repro while preserving the streaming/early-stop optimization. Consider stream/promises.finished() only if you want a more standard abstraction, but the existing closeStream() helper is straightforward and targeted.

Grey Divider

File Changes

Bug fix (1)
yamlDocuments.ts Await ReadStream closure before returning from streaming YAML reader +19/-5

Await ReadStream closure before returning from streaming YAML reader

• Replaces fire-and-forget stream.destroy() early exits with an awaited closeStream() helper to ensure file handles are released before callers proceed. Ensures cleanup runs on non-env lockfiles, successful early stop after finding the separator, and on error paths.

lockfile/fs/src/yamlDocuments.ts


Tests (1)
yamlDocuments.test.ts Regression test: non-env lockfile stream closes before rename +13/-0

Regression test: non-env lockfile stream closes before rename

• Adds a test that calls streamReadFirstYamlDocument() on a non-env YAML file and immediately renames over it, guarding against Windows EPERM when a read handle remains open.

lockfile/fs/test/yamlDocuments.test.ts


Other (1)
slow-windows-lockfile-streams.md Add patch changeset for Windows stream-close fix +6/-0

Add patch changeset for Windows stream-close fix

• Introduces a changeset bumping @pnpm/lockfile.fs and pnpm with a note about awaiting stream closure before rewriting lockfiles.

.changeset/slow-windows-lockfile-streams.md


Grey Divider

Qodo Logo

@zkochan
zkochan merged commit d50d691 into main Jun 14, 2026
14 of 15 checks passed
@zkochan
zkochan deleted the fix-win-tests branch June 14, 2026 21:37
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.

1 participant