这是indexloc提供的服务,不要输入任何密码
Skip to content

Fix memory leak and consolidate worker initialization #748

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

yamadashy
Copy link
Owner

Summary

This PR addresses memory leaks in worker threads and consolidates duplicate worker initialization logic across multiple modules.

Changes

Memory Leak Fix:

  • Fix TokenCounter memory leak in worker threads by properly freeing resources on worker termination
  • Rename freeTokenCounter to freeTokenCounters for clarity
  • Add debug logging for TokenCounter cleanup

Code Consolidation:

  • Add generic initTaskRunner<T, R> function to processConcurrency.ts
  • Remove duplicate initTaskRunner implementations from 5 modules:
    • fileCollect.ts
    • fileProcess.ts
    • securityCheck.ts
    • calculateAllFileMetrics.ts
    • calculateOutputMetrics.ts
  • Add proper worker pool cleanup with finally blocks
  • Maintain type safety through generic parameters
  • Update test mocks to match new function signatures

Benefits

  • Memory leak prevention: TokenCounter resources are now properly freed
  • Code deduplication: Eliminates 60+ lines of duplicate worker initialization code
  • Type safety: Generic type parameters ensure compile-time type checking
  • Consistent cleanup: All modules now properly cleanup worker pools

Test plan

  • Run npm run test - All 694 tests pass
  • Run npm run lint - No lint errors
  • Verify TokenCounter memory cleanup in worker processes
  • Confirm type safety with TypeScript compiler
  • Test worker pool initialization and cleanup across all modules

🤖 Generated with Claude Code

yamadashy and others added 2 commits July 24, 2025 23:25
TokenCounter instances were not being properly freed when worker threads
were terminated by Tinypool's idle timeout. This caused memory leaks
when using runCli as a library.

Changes:
- Add SIGTERM/SIGINT handlers to fileMetricsWorker and outputMetricsWorker
- Add freeTokenCounters function with proper cleanup and debug logging
- Convert all worker usage to consistent taskRunner pattern with cleanup
- Add cleanupWorkerPool function for explicit worker pool termination
- Update all related tests to match new taskRunner interface

The fix ensures TokenCounter resources are properly freed when workers
terminate, preventing memory accumulation during library usage.

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
Add generic initTaskRunner function to processConcurrency.ts to eliminate
duplicate initialization logic across multiple modules. This reduces code
duplication and provides consistent worker pool management with proper
type safety through generic parameters.

- Add TaskRunner<T, R> interface and initTaskRunner function
- Remove duplicate createTaskRunner wrappers from 5 modules
- Update all deps parameters to use shared initTaskRunner directly
- Maintain type safety with explicit generic type parameters
- Update corresponding test mocks to match new signature

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
@Copilot Copilot AI review requested due to automatic review settings July 24, 2025 14:49
Copy link
Contributor

coderabbitai bot commented Jul 24, 2025

Important

Review skipped

Auto incremental reviews are disabled on this repository.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Walkthrough

This change standardizes concurrency handling by introducing a generic initTaskRunner interface across multiple modules. It replaces custom or inline task runner functions with a shared implementation, adds explicit run and cleanup methods for worker pools, and ensures resource cleanup using finally blocks. Test mocks and related interfaces are updated accordingly.

Changes

Files/Groups Change Summary
src/core/file/fileCollect.ts, src/core/file/fileProcess.ts Refactored to use shared initTaskRunner with explicit run and cleanup methods; added finally blocks for cleanup.
src/core/metrics/calculateAllFileMetrics.ts, src/core/metrics/calculateOutputMetrics.ts Replaced custom task runner logic with shared initTaskRunner; updated to use .run() and .cleanup(); added resource cleanup.
src/core/metrics/tokenCounterFactory.ts, src/core/metrics/workers/fileMetricsWorker.ts, src/core/metrics/workers/outputMetricsWorker.ts Renamed freeTokenCounter to freeTokenCounters, added logging, and updated imports and usage accordingly.
src/core/security/securityCheck.ts Switched to shared initTaskRunner with explicit generics and cleanup; removed local task runner function and updated usage.
src/shared/processConcurrency.ts Renamed initWorker to createWorkerPool; added cleanupWorkerPool, TaskRunner interface, and generic initTaskRunner.
tests/core/file/fileCollect.test.ts, tests/core/file/fileProcess.test.ts, tests/core/metrics/calculateAllFileMetrics.test.ts, tests/core/metrics/calculateOutputMetrics.test.ts, tests/core/security/securityCheck.test.ts, tests/integration-tests/packager.test.ts Refactored test mocks to return objects with run and cleanup methods; updated signatures for consistency with production code.
tests/shared/processConcurrency.test.ts Updated imports and tests for new concurrency API; added tests for initTaskRunner interface.

Sequence Diagram(s)

sequenceDiagram
  participant Caller
  participant TaskRunner (from initTaskRunner)
  participant WorkerPool

  Caller->>TaskRunner: run(task)
  TaskRunner->>WorkerPool: execute task
  WorkerPool-->>TaskRunner: result
  TaskRunner-->>Caller: result

  Caller->>TaskRunner: cleanup()
  TaskRunner->>WorkerPool: terminate pool
  WorkerPool-->>TaskRunner: cleanup complete
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Possibly related PRs

Suggested labels

enhancement

✨ Finishing Touches
🧪 Generate unit tests
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch feat/memory

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
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

‼️ IMPORTANT
Auto-reply has been disabled for this repository in the CodeRabbit settings. The CodeRabbit bot will not respond to your replies unless it is explicitly tagged.

  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai generate unit tests to generate unit tests for this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Contributor

claude bot commented Jul 24, 2025

Claude finished @yamadashy's task —— View job


PR Review: Fix memory leak and consolidate worker initialization ✅

Overall Assessment: Excellent - This PR demonstrates strong software engineering practices with significant improvements to memory management and code maintainability.

Memory Leak Fix Analysis ✅

The memory leak fix in tokenCounterFactory.ts:25-31 is well-implemented:

Good practices:

  • Properly iterates through all cached TokenCounter instances
  • Calls free() on each instance before clearing the cache
  • Adds debug logging for traceability
  • Function renamed from freeTokenCounter to freeTokenCounters for clarity

Cleanup integration: The cleanup is properly hooked into worker termination via process.on('exit') in both worker files (fileMetricsWorker.ts:42-44 and outputMetricsWorker.ts:28-31).

Code Consolidation Analysis ✅

The generic initTaskRunner<T, R> consolidation in processConcurrency.ts:70-76 is excellent:

Strong design decisions:

  • Type safety preserved: Generic parameters <T, R> maintain compile-time type checking
  • Clean interface: Returns consistent TaskRunner<T, R> interface with run and cleanup methods
  • Proper resource management: Encapsulates both pool creation and cleanup logic
  • Eliminates ~60 lines of duplicate initialization code across 5 modules

Consistent usage pattern: All modules now follow the same pattern:

const taskRunner = deps.initTaskRunner<TaskType, ResultType>(numTasks, workerPath);
try {
  // ... use taskRunner.run(task)
} finally {
  await taskRunner.cleanup(); // Always executed
}

Type Safety & Error Handling ✅

Excellent type safety implementation:

  • Generic constraints ensure compile-time type checking across all modules
  • Type parameters flow correctly: FileCollectTask → RawFile | null, FileMetricsTask → FileMetrics, etc.
  • Dependency injection pattern maintained for testability (deps parameter)

Robust error handling:

  • All modules consistently use try-catch-finally blocks
  • Worker pool cleanup is guaranteed via finally blocks (critical improvement)
  • Error logging maintained with context (src/core/file/fileProcess.ts:59-61)

Test Coverage & Quality ✅

Well-maintained test coverage:

  • All test files updated to use mock initTaskRunner implementations
  • Tests maintain behavioral verification while adapting to new structure
  • Mock implementations properly simulate worker behavior (e.g., tests/core/metrics/calculateAllFileMetrics.test.ts:15-24)
  • Error handling paths are tested (e.g., tests/core/security/securityCheck.test.ts:77-97)

Architecture & Design Excellence ✅

Benefits achieved:

  1. Memory leak prevention - TokenCounter resources properly freed
  2. Code deduplication - 60+ lines of duplicate code eliminated
  3. Type safety - Generics ensure compile-time correctness
  4. Consistent cleanup - All modules now guarantee worker pool cleanup
  5. Maintainability - Single source of truth for worker initialization logic

Recommendations & Minor Suggestions

Minor enhancements to consider:

  1. Documentation: Consider adding JSDoc comments to the generic initTaskRunner function to document the type parameters and usage patterns.

  2. Performance monitoring: The existing timing logs are good, but consider if the consolidated function could log worker pool initialization time consistently.

No blocking issues identified - this PR is ready for merge.

Conclusion

This PR exemplifies excellent software engineering practices:

  • Fixes a real memory leak with proper resource cleanup
  • Eliminates code duplication while maintaining type safety
  • Improves error handling with guaranteed cleanup
  • Maintains test coverage with appropriate mocks
  • Follows repository conventions (conventional commits, proper testing)

The consolidation approach is architecturally sound and will make future maintenance much easier. Great work! 🎉


Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR addresses memory leaks in worker threads and consolidates duplicate worker initialization logic across multiple modules.

  • Fixes TokenCounter memory leak by properly freeing resources on worker termination
  • Consolidates duplicate worker initialization code into a generic initTaskRunner function
  • Adds proper worker pool cleanup with finally blocks across all modules

Reviewed Changes

Copilot reviewed 16 out of 16 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
src/shared/processConcurrency.ts Adds generic initTaskRunner and cleanupWorkerPool functions
src/core/metrics/tokenCounterFactory.ts Renames function and adds debug logging for resource cleanup
src/core/metrics/workers/*.ts Updates worker termination handlers to use renamed cleanup function
src/core//.ts Replaces module-specific task runners with generic implementation
tests//.test.ts Updates test mocks to match new function signatures

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Summary of Changes

Hello @yamadashy, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request primarily focuses on improving the stability and maintainability of the codebase by addressing a critical memory leak in worker threads and refactoring the worker initialization logic. It introduces a centralized, generic approach for managing worker pools, ensuring proper resource cleanup and reducing code duplication across various modules.

Highlights

  • Memory Leak Fix: Addressed a memory leak related to TokenCounter instances in worker threads by ensuring resources are properly freed upon worker termination. This includes renaming freeTokenCounter to freeTokenCounters for clarity and adding debug logging for cleanup.
  • Worker Initialization Consolidation: Introduced a new generic initTaskRunner<T, R> function in processConcurrency.ts to centralize and standardize the creation and management of worker pools, reducing boilerplate code across multiple modules.
  • Consistent Worker Pool Cleanup: Implemented finally blocks in all modules utilizing worker pools to guarantee that worker resources are consistently cleaned up, even if errors occur during task execution.
  • Enhanced Type Safety: The new generic initTaskRunner maintains type safety for worker tasks and results, improving code reliability and developer experience.
  • Test Suite Updates: Modified existing test mocks to align with the new TaskRunner interface, ensuring continued test coverage and correctness.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point in your pull request via creating an issue comment (i.e. comment on the pull request page) using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in issue comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments or fill out our survey to provide feedback.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request refactors the worker initialization logic, significantly reducing code duplication by introducing a generic initTaskRunner. The addition of finally blocks to ensure worker pool cleanup improves robustness. The memory leak fix in TokenCounter is also well-addressed.

I've left a couple of minor suggestions for improvement, one regarding an unused import and another on enhancing error logging. Overall, this is a high-quality contribution that improves maintainability and reliability.

@@ -1,7 +1,8 @@
import { parentPort } from 'node:worker_threads';
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

This parentPort import seems to be unused in the file. It's best to remove it to keep the code clean and avoid confusion.

await pool.destroy();
logger.debug('Worker pool cleaned up successfully');
} catch (error) {
logger.debug(`Error during worker pool cleanup: ${error}`);
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

Using string interpolation for logging errors (logger.debug(...${error})) can sometimes hide useful information like the stack trace. It's generally better to pass the error object as a separate argument to the logger, like logger.debug('message', error). This allows the logging library to format it properly, often including the stack trace which is very helpful for debugging.

Suggested change
logger.debug(`Error during worker pool cleanup: ${error}`);
logger.debug('Error during worker pool cleanup:', error);

Copy link

codecov bot commented Jul 24, 2025

Codecov Report

Attention: Patch coverage is 76.05634% with 17 lines in your changes missing coverage. Please review.

Project coverage is 89.77%. Comparing base (2ecf7a8) to head (aad3d81).

Files with missing lines Patch % Lines
src/shared/processConcurrency.ts 40.90% 13 Missing ⚠️
src/core/metrics/tokenCounterFactory.ts 33.33% 2 Missing ⚠️
src/core/metrics/workers/fileMetricsWorker.ts 0.00% 1 Missing ⚠️
src/core/metrics/workers/outputMetricsWorker.ts 50.00% 1 Missing ⚠️

❌ Your patch check has failed because the patch coverage (76.05%) is below the target coverage (80.00%). You can increase the patch coverage or adjust the target coverage.

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #748      +/-   ##
==========================================
+ Coverage   89.68%   89.77%   +0.09%     
==========================================
  Files         101      101              
  Lines        5806     5838      +32     
  Branches     1203     1205       +2     
==========================================
+ Hits         5207     5241      +34     
+ Misses        599      597       -2     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Remove unused parentPort import from fileMetricsWorker.ts that was
introduced during the memory leak fix but is not actually used.

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
Copy link

cloudflare-workers-and-pages bot commented Jul 24, 2025

Deploying repomix with  Cloudflare Pages  Cloudflare Pages

Latest commit: aad3d81
Status: ✅  Deploy successful!
Preview URL: https://a6ec3f3f.repomix.pages.dev
Branch Preview URL: https://feat-memory.repomix.pages.dev

View logs

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🧹 Nitpick comments (3)
src/core/metrics/tokenCounterFactory.ts (1)

2-2: LGTM! Good improvements for debugging memory leaks.

The function renaming to freeTokenCounters (plural) better reflects its behavior of freeing multiple token counters. The added debug logging provides valuable visibility into resource cleanup.

Consider making the debug log message more specific by including the encoding information:

-    logger.debug('Freed TokenCounter resources for encoding.');
+    logger.debug(`Freed TokenCounter resources for encoding: ${encoding}.`);

However, this would require capturing the encoding from the Map entries, which might not be worth the complexity for a debug message.

Also applies to: 25-31

tests/integration-tests/packager.test.ts (1)

27-36: LGTM! Well-implemented refactor to match new task runner interface.

The mock has been correctly updated to return an object with run and cleanup methods, aligning with the new standardized task runner pattern. The implementation preserves the original behavior while adding the required cleanup method as a no-op for testing.

The type casting in the run method (task as FileCollectTask and as R) maintains functionality but reduces type safety. This is acceptable for test mocks, but consider if more specific typing could be used if the mock is only used with known types.

src/shared/processConcurrency.ts (1)

53-63: Consider re-throwing errors in cleanup for better error visibility.

While logging errors during cleanup is good, consider whether errors should be re-thrown after logging to ensure callers are aware of cleanup failures. This could be important for debugging memory leaks.

 export const cleanupWorkerPool = async (pool: Tinypool): Promise<void> => {
   try {
     logger.debug('Cleaning up worker pool...');
 
     // Terminate pool which should trigger worker cleanup
     await pool.destroy();
     logger.debug('Worker pool cleaned up successfully');
   } catch (error) {
     logger.debug(`Error during worker pool cleanup: ${error}`);
+    // Re-throw to ensure callers are aware of cleanup failures
+    throw error;
   }
 };
📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 2ecf7a8 and 748ce7c.

📒 Files selected for processing (16)
  • src/core/file/fileCollect.ts (4 hunks)
  • src/core/file/fileProcess.ts (4 hunks)
  • src/core/metrics/calculateAllFileMetrics.ts (7 hunks)
  • src/core/metrics/calculateOutputMetrics.ts (5 hunks)
  • src/core/metrics/tokenCounterFactory.ts (2 hunks)
  • src/core/metrics/workers/fileMetricsWorker.ts (2 hunks)
  • src/core/metrics/workers/outputMetricsWorker.ts (2 hunks)
  • src/core/security/securityCheck.ts (4 hunks)
  • src/shared/processConcurrency.ts (2 hunks)
  • tests/core/file/fileCollect.test.ts (1 hunks)
  • tests/core/file/fileProcess.test.ts (1 hunks)
  • tests/core/metrics/calculateAllFileMetrics.test.ts (1 hunks)
  • tests/core/metrics/calculateOutputMetrics.test.ts (5 hunks)
  • tests/core/security/securityCheck.test.ts (3 hunks)
  • tests/integration-tests/packager.test.ts (1 hunks)
  • tests/shared/processConcurrency.test.ts (3 hunks)
🧰 Additional context used
🧬 Code Graph Analysis (15)
src/core/metrics/workers/outputMetricsWorker.ts (1)
src/core/metrics/tokenCounterFactory.ts (1)
  • freeTokenCounters (25-31)
tests/core/security/securityCheck.test.ts (1)
src/core/security/workers/securityCheckWorker.ts (2)
  • SecurityCheckTask (13-17)
  • SecurityCheckTask (25-42)
src/core/file/fileProcess.ts (2)
src/core/file/workers/fileProcessWorker.ts (2)
  • FileProcessTask (10-13)
  • FileProcessTask (15-21)
src/core/file/fileTypes.ts (1)
  • ProcessedFile (6-9)
src/shared/processConcurrency.ts (1)
src/shared/logger.ts (1)
  • logger (89-89)
tests/integration-tests/packager.test.ts (1)
src/core/file/workers/fileCollectWorker.ts (2)
  • FileCollectTask (9-13)
  • FileCollectTask (15-27)
tests/core/metrics/calculateOutputMetrics.test.ts (1)
src/core/metrics/workers/outputMetricsWorker.ts (2)
  • OutputMetricsTask (9-13)
  • OutputMetricsTask (15-26)
src/core/security/securityCheck.ts (1)
src/core/security/workers/securityCheckWorker.ts (3)
  • SecurityCheckTask (13-17)
  • SecurityCheckTask (25-42)
  • SuspiciousFileResult (19-23)
src/core/file/fileCollect.ts (2)
src/core/file/workers/fileCollectWorker.ts (2)
  • FileCollectTask (9-13)
  • FileCollectTask (15-27)
src/core/file/fileTypes.ts (1)
  • RawFile (1-4)
tests/core/file/fileProcess.test.ts (1)
src/core/file/workers/fileProcessWorker.ts (2)
  • FileProcessTask (10-13)
  • FileProcessTask (15-21)
src/core/metrics/calculateAllFileMetrics.ts (2)
src/core/metrics/workers/fileMetricsWorker.ts (2)
  • FileMetricsTask (12-17)
  • FileMetricsTask (19-28)
src/core/metrics/workers/types.ts (1)
  • FileMetrics (1-5)
src/core/metrics/workers/fileMetricsWorker.ts (1)
src/core/metrics/tokenCounterFactory.ts (1)
  • freeTokenCounters (25-31)
tests/core/metrics/calculateAllFileMetrics.test.ts (1)
src/core/metrics/workers/fileMetricsWorker.ts (2)
  • FileMetricsTask (12-17)
  • FileMetricsTask (19-28)
tests/shared/processConcurrency.test.ts (1)
src/shared/processConcurrency.ts (2)
  • createWorkerPool (26-51)
  • initTaskRunner (70-76)
tests/core/file/fileCollect.test.ts (1)
src/core/file/workers/fileCollectWorker.ts (2)
  • FileCollectTask (9-13)
  • FileCollectTask (15-27)
src/core/metrics/tokenCounterFactory.ts (1)
src/shared/logger.ts (1)
  • logger (89-89)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (13)
  • GitHub Check: Test (windows-latest, 24.x)
  • GitHub Check: Test (macos-latest, 22.x)
  • GitHub Check: Test (windows-latest, 23.x)
  • GitHub Check: Test (macos-latest, 23.x)
  • GitHub Check: Test (windows-latest, 22.x)
  • GitHub Check: Test (windows-latest, 21.x)
  • GitHub Check: Test (windows-latest, 20.x)
  • GitHub Check: Build and run (windows-latest, 22.x)
  • GitHub Check: Test coverage
  • GitHub Check: Build and run with Bun (ubuntu-latest, latest)
  • GitHub Check: Build and run with Bun (windows-latest, latest)
  • GitHub Check: Build and run with Bun (macos-latest, latest)
  • GitHub Check: auto-review
🔇 Additional comments (32)
src/core/metrics/workers/outputMetricsWorker.ts (1)

3-3: LGTM! Consistent function renaming applied.

The import and usage have been correctly updated from freeTokenCounter to freeTokenCounters, maintaining consistency with the renamed function in tokenCounterFactory.ts.

Also applies to: 30-30

src/core/metrics/workers/fileMetricsWorker.ts (1)

5-5: LGTM! Consistent function renaming applied.

The import and usage have been correctly updated from freeTokenCounter to freeTokenCounters, maintaining consistency with the renamed function in tokenCounterFactory.ts.

Also applies to: 43-43

tests/shared/processConcurrency.test.ts (2)

4-9: LGTM! Test correctly updated for renamed function.

The test has been properly updated to test createWorkerPool instead of the previous function name, maintaining the same validation logic for Tinypool initialization.

Also applies to: 68-89


91-112: LGTM! Good test coverage for the new task runner interface.

The new test suite properly validates that initTaskRunner returns an object with the expected run and cleanup methods, ensuring the interface contract is maintained. The mocking setup appropriately simulates the Tinypool behavior.

src/core/metrics/calculateOutputMetrics.ts (4)

3-3: LGTM: Import consolidation implemented correctly.

The import of initTaskRunner from the shared concurrency module aligns with the PR objective to consolidate duplicate worker initialization logic.


19-22: LGTM: Task runner initialization follows the new shared interface correctly.

The generic type parameters <OutputMetricsTask, number> correctly match the worker's input/output types, and the worker script URL is properly resolved using import.meta.url.


42-42: LGTM: Task execution updated to use the new TaskRunner interface.

The calls to taskRunner.run(task) correctly replace the previous direct function calls, maintaining the same functionality while using the standardized interface.

Also applies to: 54-54


65-68: Excellent: Critical memory leak fix implemented.

The finally block ensures that taskRunner.cleanup() is always called, preventing memory leaks by properly freeing worker pool resources regardless of success or failure. This directly addresses the memory leak issue mentioned in the PR objectives.

src/core/file/fileCollect.ts (4)

4-4: LGTM: Consistent import consolidation.

The import aligns with the shared concurrency module pattern used across other files in this refactor.


18-21: LGTM: Task runner initialization properly typed.

The generic parameters <FileCollectTask, RawFile | null> correctly reflect the worker's interface, and the number of tasks is appropriately set to the number of file paths.


40-40: LGTM: Task execution correctly updated.

The change to taskRunner.run(task) maintains the same functionality while using the standardized TaskRunner interface.


57-60: Excellent: Consistent resource cleanup implementation.

The finally block with taskRunner.cleanup() ensures proper worker pool cleanup, preventing memory leaks and maintaining consistency across all modules.

tests/core/file/fileCollect.test.ts (1)

24-33: LGTM: Test mock properly adapted to new TaskRunner interface.

The mock function correctly:

  • Accepts generic type parameters and the required parameters
  • Returns an object with run and cleanup methods matching the TaskRunner interface
  • Delegates to the actual worker function while maintaining type safety
  • Provides a no-op cleanup method appropriate for testing

This ensures tests continue to work with the new concurrency abstraction.

tests/core/file/fileProcess.test.ts (1)

22-31: LGTM: Consistent test mock implementation.

The mock implementation follows the same pattern as other test files, correctly adapting to the new TaskRunner interface while maintaining proper delegation to the fileProcessWorker for accurate testing.

src/core/file/fileProcess.ts (4)

4-4: LGTM: Import consolidation consistent with other files.

The import change aligns with the shared concurrency module pattern used throughout this refactor.


24-27: LGTM: Task runner initialization with correct type parameters.

The generic parameters <FileProcessTask, ProcessedFile> accurately reflect the worker's input and output types, and the task count is appropriately set to the number of raw files.


45-45: LGTM: Task execution follows standardized pattern.

The use of taskRunner.run(task) is consistent with the new TaskRunner interface implemented across all modules.


62-65: Excellent: Memory leak prevention implemented consistently.

The finally block ensures proper cleanup of worker pool resources, completing the memory leak fix across all modules that use worker threads.

tests/core/metrics/calculateAllFileMetrics.test.ts (1)

15-24: LGTM! Mock correctly implements the new task runner interface.

The mock properly returns an object with run and cleanup methods, aligning with the new TaskRunner interface introduced in the production code.

src/core/metrics/calculateAllFileMetrics.ts (2)

56-59: Excellent addition of cleanup in finally block.

The finally block ensures that the worker pool is always cleaned up, even if an error occurs during metrics calculation. This directly addresses the memory leak issue mentioned in the PR objectives.


116-119: Consistent cleanup pattern applied.

Good to see the same cleanup pattern applied in calculateSelectiveFileMetrics. This ensures consistent resource management across both functions.

src/shared/processConcurrency.ts (2)

26-51: Good rename for clarity.

Renaming initWorker to createWorkerPool better reflects the function's purpose of creating a Tinypool instance rather than initializing a single worker.


70-76: Clean implementation of the generic task runner.

The generic initTaskRunner function effectively consolidates the duplicate worker initialization logic mentioned in the PR objectives. The use of generics provides type safety while maintaining flexibility.

tests/core/security/securityCheck.test.ts (2)

42-51: Mock correctly implements the new interface.

The mock task runner properly returns an object with run and cleanup methods, matching the production TaskRunner interface.


79-88: Good error handling test coverage.

The error handling test correctly verifies that errors are propagated from the task runner's run method while still maintaining the cleanup interface.

src/core/security/securityCheck.ts (1)

86-89: Proper resource cleanup implemented.

The finally block ensures that the worker pool is cleaned up regardless of success or failure, preventing memory leaks as mentioned in the PR objectives.

tests/core/metrics/calculateOutputMetrics.test.ts (6)

9-18: LGTM! Clean implementation of the new TaskRunner interface.

The mock correctly implements the new interface pattern with proper generic type parameters and appropriate test-specific behavior (no-op cleanup).


49-58: Error handling mock correctly implemented.

The mock properly throws errors in the run method while maintaining the same interface structure as other mocks.


99-110: Parallel processing mock implementation looks good.

The mock correctly tracks chunk processing and returns consistent values for parallel processing test scenarios.


125-134: Parallel error handling mock correctly implemented.

The mock appropriately throws errors for testing parallel processing error scenarios while maintaining interface consistency.


150-161: Chunk tracking mock implementation is effective.

The mock properly collects processed chunks and returns content length, enabling comprehensive validation of chunking behavior.


1-176: Excellent refactoring to align with the new TaskRunner interface.

The test file has been systematically updated to work with the new generic initTaskRunner interface. All mock implementations are consistent, maintain type safety, and preserve the original test logic while adapting to the new worker pool pattern with proper cleanup methods.

The issue where Bun runtime causes process termination when calling
pool.destroy() on Tinypool is a known compatibility problem. This
implementation skips the problematic destroy() call in Bun while
maintaining proper cleanup in Node.js environments.

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
- Add encoding name to TokenCounter cleanup debug messages
- Improves debugging experience by showing which encoding was freed
- Addresses PR review feedback for better observability

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
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