Skip to content

Conversation

yash-dayama
Copy link

#2548

Fix Memory Leak in Console Transport

Description
This PR fixes a memory leak issue in the Console transport that occurs when logging a large number of messages in quick succession. The issue was reported in #2548 and occurs in Winston 3.x.x versions.

Problem
The Console transport was using setImmediate to emit the 'logged' event, which under high load (many log messages) would create a large number of pending operations that weren't being cleaned up properly. This led to increased memory usage over time.

Solution
The fix removes the unnecessary setImmediate wrapper when emitting the 'logged' event. Since the console writes are already asynchronous (via stdout/stderr streams), there's no need for the additional async operation. This change significantly reduces memory usage under high load while maintaining the same functionality.

Changes

  • Removed setImmediate wrapper in Console transport's log method
  • Added stress tests to catch memory leaks
  • Verified event listener cleanup

Testing
Added new stress tests that:

  1. Verify memory usage remains stable when logging many messages
  2. Ensure event listeners are properly cleaned up

To run the tests:

# Run with garbage collection enabled for memory tests
node --expose-gc node_modules/mocha/bin/mocha test/unit/winston/transports/console*.test.js

Yash Dayama added 2 commits July 29, 2025 14:42
- Remove unnecessary setImmediate wrapper in Console transport log method

- Add stress tests to verify memory usage and event listener cleanup

- Fix issue winstonjs#2548
DABH added 2 commits August 11, 2025 23:37
This file should be part of the PR
Revert to master version of file, no reason this should have changed
@DABH
Copy link
Contributor

DABH commented Aug 12, 2025

@yash-dayama for some reason package-lock.json is showing as changed, can you please revert that file so it has no changes for this PR?

@DABH
Copy link
Contributor

DABH commented Aug 12, 2025

@yash-dayama I'm also seeing some test failures from the CI system, can you please investigate? Thanks for your time!

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.

2 participants