Skip to content

Convert synchronous file binary detection to async to eliminate event loop blocking #3286

Closed

Description

@sangwook-dev

What would you like to be added?

Convert synchronous file binary detection operations to asynchronous implementations to eliminate Node.js event loop blocking.

Specific changes:

  • Convert isBinaryFile() function from sync to async using fs.promises API
  • Convert detectFileType() function to async (depends on isBinaryFile)
  • Update all callers to use await with proper error handling
  • Update test cases to handle async functions

Files to be modified:

  • packages/core/src/utils/fileUtils.ts - Core implementation
  • packages/core/src/utils/fileUtils.test.ts - Test updates
  • packages/core/src/tools/read-many-files.ts - Update function calls

Why is this needed?

Performance Problem:
The current implementation uses synchronous file operations (fs.openSync, fs.fstatSync, fs.readSync, fs.closeSync) which completely block the Node.js event loop during file binary detection.

Impact on Users:

  • UI freezing: Terminal interface becomes unresponsive when processing multiple files
  • Poor scalability: Performance degrades dramatically with large codebases
  • Sequential bottleneck: Files must be processed one-by-one instead of concurrently
  • Bad user experience: Commands like "analyze all TypeScript files" cause noticeable delays

Performance Impact:

  • Small projects (10-20 files): 20-30% performance improvement expected
  • Medium projects (50-100 files): 40-60% performance improvement expected
  • Large projects (200+ files): 60-80% performance improvement expected
  • UI responsiveness: Eliminates freezing during file operations

Current Problematic Code:

  export function isBinaryFile(filePath: string): boolean {
    const fd = fs.openSync(filePath, 'r');          // BLOCKS EVENT LOOP
    const fileSize = fs.fstatSync(fd).size;         // BLOCKS EVENT LOOP  
    const bytesRead = fs.readSync(fd, buffer, 0, buffer.length, 0);  // BLOCKS EVENT LOOP
    fs.closeSync(fd);                               // BLOCKS EVENT LOOP
  }

Additional context

Technical Details:

  • This affects all file reading operations that check for binary content
  • The read-many-files tool is particularly impacted when processing multiple files
  • Modern Node.js best practices recommend avoiding sync file operations in application code
  • The async implementation will enable concurrent file processing

Proposed Implementation:

  export async function isBinaryFile(filePath: string): Promise<boolean> {
    try {
      const fileHandle = await fs.promises.open(filePath, 'r');
      try {
        const stats = await fileHandle.stat();
        // ... async implementation with proper resource cleanup
      } finally {
        await fileHandle.close();
      }
    } catch {
      return false;
    }
  }

Breaking Changes:

  • isBinaryFile() signature changes from (string) => boolean to (string) => Promise
  • detectFileType() signature changes from (string) => string to (string) => Promise
  • All callers need to be updated to use await

Testing Strategy:

  • All existing unit tests will be updated to handle async functions
  • Behavior parity with synchronous version will be verified
  • Performance regression tests will be added
  • Integration tests with file processing tools will be updated

Implementation Status:
I have a working implementation ready on the feature/async-file-binary-check branch with all tests passing. This issue is being created following the contribution guidelines that require discussing changes before submitting PRs.

Expected Benefits:

  • ✅ Non-blocking file operations
  • ✅ Improved UI responsiveness
  • ✅ Better scalability for large projects
  • ✅ Enables concurrent file processing
  • ✅ Follows Node.js async best practices

Activity

  1. added
    status/need-triage
    on Jul 5, 2025
  2. added
    area/core
    priority/p1
    and removed
    status/need-triage
    on Jul 5, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area/corepriority/p1

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions