Skip to content

Conversation

@dgaponov
Copy link

Problem

In development mode with Hot Module Replacement (HMR) enabled, workers were being recompiled on every change, leading to memory leaks. This issue was particularly problematic in rspack environments where repeated compilation cycles would cause significant memory accumulation.

web-infra-dev/rspack#8976 (comment)

Root Cause

The AddWorkerEntryPointPlugin was creating new child compilers for worker entry points on every compilation cycle without tracking whether the worker had already been compiled. This resulted in:

  • Unnecessary repeated worker compilation
  • Memory leaks from accumulated compilation artifacts
  • Degraded development experience due to memory pressure

Solution

  • Added alreadyCompiled flag to track compilation state
  • Prevents worker recompilation after the first successful build
  • Maintains existing functionality while eliminating memory leaks

Changes

  • Modified getCompilerHook function to include compilation tracking
  • Added early return when worker is already compiled
  • No breaking changes to existing API

Testing

  • ✅ Verified worker compilation works correctly on first build
  • ✅ Confirmed no recompilation on subsequent HMR cycles
  • ✅ Tested memory usage remains stable during development
  • ✅ Validated compatibility with both webpack and rspack

Impact

  • Memory Usage: Significantly reduced memory consumption in dev mode
  • Performance: Faster HMR cycles by skipping unnecessary worker compilation
  • Developer Experience: More stable development environment
  • Compatibility: Works with both webpack and rspack bundlers

Fixes memory leak issues reported in development environments using HMR with worker entry points.

Add alreadyCompiled flag to prevent unnecessary worker rebuilds
during development with Hot Module Replacement, which was causing
memory leaks in both webpack and rspack.
@dgaponov
Copy link
Author

@microsoft-github-policy-service agree

@dgaponov
Copy link
Author

@hediet @alexr00
Hi! I'd really appreciate it if you could review my PR. This fix is quite important for my team."

@dgaponov
Copy link
Author

dgaponov commented Oct 3, 2025

@hediet @alexr00
UP

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