Skip to content

fix: replace infinite loop in logger color assignment with modulo indexing#118

Open
theanshsonkar wants to merge 2 commits intometacall:masterfrom
theanshsonkar:fix/logger-infinite-loop
Open

fix: replace infinite loop in logger color assignment with modulo indexing#118
theanshsonkar wants to merge 2 commits intometacall:masterfrom
theanshsonkar:fix/logger-infinite-loop

Conversation

@theanshsonkar
Copy link
Copy Markdown

Issue Fix : #116

Problem : The function assignColorToWorker in the file src/utils/logger.ts used a do...while loop to generate a random unused color from the list of ANSI color codes for each deployment. When all 16 colors were allocated (17th deployment onwards), every random pick would fail the uniqueness check, causing an infinite loop that froze the entire server since Node.js is single-threaded. There was even a TODO comment in the code acknowledging this problem.

Fix
Added a module-level 'colorIndex' counter
Replaced the random 'do...while' loop with modulo indexing: "ANSICode[colorIndex % ANSICode.length]"
Colors now cycle through the 16-color palette deterministically
No infinite loop possible regardless of how many deployments are created

Changes : "src/utils/logger.ts" — replaced loop, added "colorIndex", updated the comments also

Testing : All 14 existing tests passed with no changes required

AI Disclosure : AI tools (Claude) were used to help understand the codebase structure and verify the fix approach. All changes were reviewed, tested, and are fully understood by me.

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.

fix: infinite loop in logger color assignment when all ANSI codes are allocated

1 participant