Skip to content
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

Receiving memory leak warnings when running Jest test suite - A logger instance is created per test and closed using the close method #1706

Closed
1 of 2 tasks
dcs3spp opened this issue Sep 24, 2019 · 4 comments

Comments

@dcs3spp
Copy link

dcs3spp commented Sep 24, 2019

Please tell us about your environment:

  • _winston version?_3.2.1
    • winston@2
    • winston@3
  • _node -v outputs:_v10.6.0
  • Operating System? (macOS)
  • Language? (TypeScript 3.6.3)

What is the problem?

I am using Nestjs and have implemented a logging service that acts as a wrapper for a winston logger instance with a console transport. I am running a set of jest tests inband with caching disabled. Each test creates an instance of the logging service. When each test completes the nestjs test module is closed, which in turn triggers a dispose hook on my logging service. In the dispose hook I am calling the close method on the encapsulated winston logger.

When running the series of jest tests I receive a warning concerning memory leaks....

(node:2812) MaxListenersExceededWarning: Possible EventEmitter memory leak detected. 11 uncaughtException listeners added. 
Use emitter.setMaxListeners() to increase limit

In my service I am creating and disposing of the logger as follows:

public constructor(options: LoggerOptions) {
    this.logger = createLogger({
      exitOnError: true,
      level: options.level,
      transports: [
        new transports.Console({
          format: alignColorsAndTime,
          handleExceptions: true,
          silent: options.silent,
        }),
      ],
    });
  }

public onModuleDestroy() {
    this.close();
}

public close() {
    this.logger.debug('LoggingService::close() closing logger...');
    this.logger.close();
}

What do you expect to happen instead?

No warning messages about memory leaks to appear after calling close on the winston logger instance.

Other information

The beforeEach and afterEach jest test hooks are included in the listing below:

afterEach(async done => {
    try {
      await dispose();
      done();
    } catch (error) {
      testLogger(`afterEach() exception while disposing resources => ${error}`);
    }
  });

  beforeEach(async done => {
    try {
      testModule = await Test.createTestingModule({
        imports: [AppModule.forRoot(appOptions)],
      }).compile();
    } catch (err) {
      testLogger('beforeEach() exception initialising creating test module');
      throw err;
    }

    try {
      app = testModule.createNestApplication();
      await app.init();
    } catch (err) {
      testLogger('beforeEach() exception initialising application');
      try {
        await dispose();
      } catch (innerErr) {
        testLogger(`beforeEach() exception disposing resources => ${err}`);
      }
      throw err;
    }

    done();
  });
@wbt
Copy link
Contributor

wbt commented Jul 7, 2020

I've just submitted a PR that I think would likely fix this, and included a workaround in the description there.

DABH pushed a commit that referenced this issue Jul 11, 2020
for uncaught exceptions and rejections. Closes a small memory leak observed in Issue #1706.
@DABH
Copy link
Contributor

DABH commented Jul 11, 2020

Going to close this for now. If problem persists on master please let me know. Thanks @wbt in the meantime!

@NavidG23
Copy link

I have a large spec file, and I followed the solution as stated here. However, the issue seems to persist with Jest. When I split the test file into 3 smaller files there is no longer a memory leak. Any idea what could be going on here?

@wbt
Copy link
Contributor

wbt commented Jan 27, 2022

@NavidG23 What version of Winston are you using?

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

No branches or pull requests

4 participants