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

fix(NODE-6151): MongoClient connect does not keep Node.js running #4101

Merged
merged 2 commits into from
May 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions src/timeout.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export class Timeout extends Promise<never> {
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) {
Expand All @@ -63,8 +63,8 @@ export class Timeout extends Promise<never> {
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();
}
}
Expand All @@ -78,8 +78,8 @@ export class Timeout extends Promise<never> {
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 {
Expand Down
4 changes: 2 additions & 2 deletions test/unit/timeout.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Loading