Skip to content

Conversation

Copy link

Copilot AI commented Aug 19, 2025

Problem

The concurrency() function had a critical bug where invalid limit values (≤ 0, NaN, Infinity) would cause tasks to never execute, resulting in infinite hangs. This occurred because the condition workers < limit would never be true for these edge cases.

// These would hang indefinitely before the fix
const limiter = concurrency(0);
await limiter(() => console.log('This never executes'));

const limiter2 = concurrency(-1);  
await limiter2(() => console.log('This never executes either'));

Solution

Added robust input validation and normalization:

  • Zero/negative limits → normalized to minimum of 1
  • Non-integer limits → floored to integers (e.g., 2.7 → 2)
  • NaN/Infinity → defaulted to 1

The fix ensures all edge cases work correctly while maintaining 100% backward compatibility for valid inputs.

Testing

Added comprehensive test coverage with 12 new test cases covering:

  • Invalid limit value handling and normalization
  • Error propagation between concurrent tasks
  • High-volume concurrent execution (100+ tasks)
  • Immediately completing tasks
  • Very large limit values
  • Empty queue scenarios
  • Proper scope/context maintenance

All 388 tests now pass, including the original 132 concurrency tests plus the new edge case tests.

Code Quality

Improved code clarity by:

  • Extracting limit validation logic
  • Replacing confusing comma operators with explicit control flow
  • Adding clear variable naming

Fixes #101.


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Copy link
Author

Copilot AI commented Aug 19, 2025

@streamich 👋 This repository doesn't have Copilot instructions. With Copilot instructions, I can understand the repository better, work faster and produce higher quality PRs.

I can generate a .github/copilot-instructions.md file for you automatically. Click here to open a pre-filled issue and assign it to me. I'll write the instructions, and then tag you for review.

Co-authored-by: streamich <9773803+streamich@users.noreply.github.com>
Copilot AI changed the title [WIP] Harden concurrency() limiter Harden concurrency() limiter with comprehensive edge case handling Aug 19, 2025
Copilot AI requested a review from streamich August 19, 2025 13:49
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.

Harden concurrency() limiter

2 participants