question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

TLSWRAP open handle wrongly detected with MongoDB connections

See original GitHub issue

💥 Regression Report

Running Jest with --detectOpenHandles detects a TLSWRAP potential open handle on MongoDb connections, even if they are properly closed in an “afterAll” enclosure and jest exits without issues. The exact message is:

Jest has detected the following 1 open handle potentially keeping Jest from exiting:

  ●  TLSWRAP

       6 |
       7 | beforeAll(async () => {
    >  8 |   await client.connect()
         |                ^
       9 | })
      10 |
      11 | afterAll(async () => {

      at Object.resolveSRVRecord (node_modules/mongodb/src/connection_string.ts:69:7)
      at Object.connect (node_modules/mongodb/src/operations/connect.ts:51:12)
      at node_modules/mongodb/src/mongo_client.ts:410:7
      at Object.maybePromise (node_modules/mongodb/src/utils.ts:618:3)
      at MongoClient.connect (node_modules/mongodb/src/mongo_client.ts:409:12)
      at app.test.js:8:16
      at TestScheduler.scheduleTests (node_modules/@jest/core/build/TestScheduler.js:333:13)
      at runJest (node_modules/@jest/core/build/runJest.js:387:19)
      at _run10000 (node_modules/@jest/core/build/cli/index.js:408:7)
      at runCLI (node_modules/@jest/core/build/cli/index.js:261:3)

This seems to happen with MongoDb’s official NodeJs driver, and, by extension, any other packages that use that driver (in my case I got it with mongoose and connect-mongo-session)

Last working version

Worked up to version: 26.6.3

Stopped working in version: 27.0.0

To Reproduce

NOTE: A valid MongoDB URI is needed to reproduce this issue.

Within an empty directory:

  1. Create a new empty Node project with:
npm init 
  1. Install jest and mongodb
npm install jest mongodb
  1. Create a app.test.js file with following contents (add a valid MongoDB URI where necessary):
const { MongoClient } = require("mongodb");

// Replace the uri string with your MongoDB connection string.
const uri ='YOUR-MONGODB-URI'
const client = new MongoClient(uri)

beforeAll(async () => {
  await client.connect()
})

afterAll(async () => {
  await client.close()
})

test('testing', () => {
  expect(client).toBeDefined()
})
  1. Run the test with jest and --detectOpenHandles
jest --detectOpenHandles

Expected behavior

No open handles should be found.

Link to repl or repo (highly encouraged)

Use the code snippet provided above.

Run npx envinfo --preset jest

Paste the results here:

System:
    OS: macOS 11.4
    CPU: (8) arm64 Apple M1
  Binaries:
    Node: 16.4.2 - /opt/local/bin/node
    npm: 7.19.1 - /opt/local/bin/npm
  npmPackages:
    jest: ^27.0.0 => 27.0.6 

Thank you for your help! ❤️

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:19
  • Comments:24 (2 by maintainers)

github_iconTop GitHub Comments

9reactions
malthecommented, Nov 5, 2021

The problem seems to be that the Async hooks module in Node doesn’t emit a destroy event for the “TLSWRAP” hook type.

In Jest we have today something like:

if (type === 'PROMISE' ||
    type === 'TIMERWRAP' ||
    type === 'ELDHISTOGRAM' ||
    type === 'PerformanceObserver' ||
    type === 'RANDOMBYTESREQUEST' ||
    type === 'DNSCHANNEL' ||
    type === 'ZLIB'
  ) return;

These are all problematic hook types that are simply ignored. And “TLSWRAP” should probably be added to this list.

(Since there is always an underlying socket object which is what we’re really interested in.)

But to test this, we’d have to set up a TLS session object which is not exactly trivial.

5reactions
vollmerrcommented, Sep 29, 2021

I have the same issue with something as simple as this using axios

import axios from "axios";

it("runs without open handles", async () => {
  await axios.get("https://jsonplaceholder.typicode.com/todos/1");
  expect(true).toBe(true);
});

“axios”: “^0.21.4”, “jest”: “^27.2.4”, “ts-jest”: “^27.0.5”

Read more comments on GitHub >

github_iconTop Results From Across the Web

mongoose.connect leaves open handles after jest test
I am reposting my answer from this question jest and mongoose - jest has detected opened handles as this was my solution for...
Read more >
Troubleshoot Connection Issues — MongoDB Atlas
Close any open connections to your database deployment not currently in use. Scale your cluster to a higher tier to support more concurrent...
Read more >
Using with MongoDB - Jest
Jest MongoDB provides all required configuration to run your tests using MongoDB. ... connection = await MongoClient.connect(globalThis.
Read more >
Connecting Jest and Mongoose - Zell Liew
To connect to a MongoDB, you can use Mongoose's connect command. ... Safe to ignore. if (error.message === 'ns not found') return //...
Read more >
[NODE-3597] Possible open handle in MongoClient.connect
npm test. > app2@1.0.0 test. > jest --detectOpenHandles. PASS __tests__/app2.spec.js. MongoDB Node.js Driver. ✓ should successfully connect ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found