From 7e0d9e689859e20ad0fd7aae047fc349e24355c9 Mon Sep 17 00:00:00 2001 From: Warren James Date: Mon, 6 May 2024 17:06:15 -0400 Subject: [PATCH] fix(NODE-6151): MongoClient connect does not keep Node.js running (#4101) --- src/timeout.ts | 10 +++++----- test/unit/timeout.test.ts | 4 ++-- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/timeout.ts b/src/timeout.ts index fadf9727a6..7d0af2383d 100644 --- a/src/timeout.ts +++ b/src/timeout.ts @@ -41,7 +41,7 @@ export class Timeout extends Promise { public timedOut = false; /** Create a new timeout that expires in `duration` ms */ - private constructor(executor: Executor = () => null, duration: number) { + private constructor(executor: Executor = () => null, duration: number, unref = false) { let reject!: Reject; if (duration < 0) { @@ -63,8 +63,8 @@ export class Timeout extends Promise { this.timedOut = true; reject(new TimeoutError(`Expired after ${duration}ms`)); }, this.duration); - // Ensure we do not keep the Node.js event loop running - if (typeof this.id.unref === 'function') { + if (typeof this.id.unref === 'function' && unref) { + // Ensure we do not keep the Node.js event loop running this.id.unref(); } } @@ -78,8 +78,8 @@ export class Timeout extends Promise { this.id = undefined; } - public static expires(durationMS: number): Timeout { - return new Timeout(undefined, durationMS); + public static expires(durationMS: number, unref?: boolean): Timeout { + return new Timeout(undefined, durationMS, unref); } static is(timeout: unknown): timeout is Timeout { diff --git a/test/unit/timeout.test.ts b/test/unit/timeout.test.ts index 571050ce41..3fafc21b35 100644 --- a/test/unit/timeout.test.ts +++ b/test/unit/timeout.test.ts @@ -23,12 +23,12 @@ describe('Timeout', function () { beforeEach(() => { timeout = Timeout.expires(2000); }); - it('creates a timeout instance that will not keep the Node.js event loop active', function () { + it.skip('creates a timeout instance that will not keep the Node.js event loop active', function () { expect(timeout).to.have.property('id'); // @ts-expect-error: accessing private property const id = timeout.id; expect(id?.hasRef()).to.be.false; - }); + }).skipReason = 'Skipping until further work during CSOT implementation'; it('throws a TimeoutError when it expires', async function () { try { await timeout;