Skip to content

fix(cli): destroy the worker pool after the initial compilation - #142

Merged
Donny/강동윤 (kdy1) merged 1 commit into
mainfrom
kdy1/fix-issue-126
Nov 2, 2026
Merged

Donny/강동윤 (kdy1) merged 1 commit into
mainfrom
kdy1/fix-issue-126

Conversation

@kdy1

Copy link
Copy Markdown
Member

Problem

initialCompilation() in packages/cli/src/swc/dir.ts creates a Piscina worker pool and does not destroy it. A one-shot CLI run exits, so the pool does not stay. But long-lived processes call swcDir() as a library. For example, the @nestjs/cli swc builder calls swcDir() on each file-add event in watch mode. Each call leaks a pool with minThreads idle worker threads.

Change

  • Wrap the pool usage in try/finally and call await workers.destroy() in the finally block. The pool is released when the compilation succeeds and when it fails.
  • Use try/finally instead of Promise.prototype.finally. The lib setting in packages/cli/tsconfig.json does not declare Promise.prototype.finally. This was the review finding on fix(cli): Destroy the worker pool after the initial compilation #130.
  • Add packages/cli/src/swc/__tests__/dir.test.ts. The test mocks piscina and checks that destroy() is called once after a successful compilation and once after a failed compilation.
  • Add a patch changeset for @swc/cli.

The watch path compiles with dirWorker directly and does not use this pool, so watch behavior does not change.

This change is based on the investigation and the approach in #130 by the issue author.

Validation

All local checks below ran on commit e661703:

  • pnpm exec tsc --noEmit in packages/cli: passed.
  • pnpm exec jest in packages/cli: 8 suites, 65 tests passed.
  • Without the dir.ts change, the 2 new tests in dir.test.ts failed.
  • prettier --check on the changed source and test files: passed.

Closes #126

🤖 Generated with Claude Code

swcDir() created a Piscina pool for the initial compilation and never
destroyed it. Long-lived processes that call swcDir() repeatedly, such as
the @nestjs/cli swc builder in watch mode, leaked idle worker threads on
each call.

Destroy the pool in a finally block, so it is released on success and on
failure.

Closes #126

Co-authored-by: 你好怪凡 <9988024+higuaifan@users.noreply.github.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@changeset-bot

changeset-bot Bot commented Nov 2, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: e661703

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@swc/cli Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 2, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-02T02:12:31.808306Z e661703 PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: e6617030c4

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "Codex (@codex) review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "Codex (@codex) address that feedback".

Comment thread .changeset/tidy-pools-rest.md Resolved
@kdy1
Donny/강동윤 (kdy1) enabled auto-merge (squash) November 2, 2026 02:14
@kdy1
Donny/강동윤 (kdy1) merged commit 607a988 into main Nov 2, 2026
14 checks passed
@kdy1
Donny/강동윤 (kdy1) deleted the kdy1/fix-issue-126 branch November 2, 2026 02:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

@swc/cli: swcDir() leaks a Piscina worker pool per invocation when used as a library

1 participant