Skip to content

feat(NODE-6633): MongoClient.close closes active cursors #4372

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

Merged
merged 8 commits into from
Jan 27, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
fix: track position, helper, tailable interrupt
  • Loading branch information
nbbeeken committed Jan 27, 2025
commit 69c4a1a918ad767c4ac37de2f9fa0ef30a44a790
45 changes: 21 additions & 24 deletions src/cursor/abstract_cursor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -272,10 +272,6 @@ export abstract class AbstractCursor<
throw new MongoRuntimeError('Cursor must be constructed with MongoClient');
}
this.cursorClient = client;

this.cursorClient.s.activeCursors.add(this);
this.once('close', removeActiveCursor);

this.cursorNamespace = namespace;
this.cursorId = null;
this.initialized = false;
Expand Down Expand Up @@ -373,6 +369,7 @@ export abstract class AbstractCursor<
this.signal,
() => void this.close().then(undefined, squashError)
);
this.trackCursor();
}

/**
Expand Down Expand Up @@ -452,6 +449,14 @@ export abstract class AbstractCursor<
await this.close();
}

/** Adds cursor to client's tracking so it will be closed by MongoClient.close() */
private trackCursor() {
this.cursorClient.s.activeCursors.add(this);
if (!this.listeners('close').includes(removeActiveCursor)) {
this.once('close', removeActiveCursor);
}
}

/** Returns current buffered documents length */
bufferedCount(): number {
return this.documents?.length ?? 0;
Expand Down Expand Up @@ -866,21 +871,14 @@ export abstract class AbstractCursor<
this.isClosed = false;
this.isKilled = false;
this.initialized = false;
this.trackCursor();

this.cursorClient.s.activeCursors.add(this);
if (!this.listeners('close').includes(removeActiveCursor)) {
this.once('close', removeActiveCursor);
}

const session = this.cursorSession;
if (session) {
// We only want to end this session if we created it, and it hasn't ended yet
if (session.explicit === false) {
if (!session.hasEnded) {
session.endSession().then(undefined, squashError);
}
this.cursorSession = this.cursorClient.startSession({ owner: this, explicit: false });
// We only want to end this session if we created it, and it hasn't ended yet
if (this.cursorSession.explicit === false) {
if (!this.cursorSession.hasEnded) {
this.cursorSession.endSession().then(undefined, squashError);
}
this.cursorSession = this.cursorClient.startSession({ owner: this, explicit: false });
}
}

Expand Down Expand Up @@ -1017,7 +1015,6 @@ export abstract class AbstractCursor<
private async cleanup(timeoutMS?: number, error?: Error) {
this.abortListener?.[kDispose]();
this.isClosed = true;
const session = this.cursorSession;
const timeoutContextForKillCursors = (): CursorTimeoutContext | undefined => {
if (timeoutMS != null) {
this.timeoutContext?.clear();
Expand All @@ -1039,7 +1036,7 @@ export abstract class AbstractCursor<
!this.cursorId.isZero() &&
this.cursorNamespace &&
this.selectedServer &&
!session.hasEnded
!this.cursorSession.hasEnded
) {
this.isKilled = true;
const cursorId = this.cursorId;
Expand All @@ -1048,7 +1045,7 @@ export abstract class AbstractCursor<
await executeOperation(
this.cursorClient,
new KillCursorsOperation(cursorId, this.cursorNamespace, this.selectedServer, {
session
session: this.cursorSession
}),
timeoutContextForKillCursors()
);
Expand All @@ -1057,11 +1054,11 @@ export abstract class AbstractCursor<
squashError(error);
} finally {
try {
if (session?.owner === this) {
await session.endSession({ error });
if (this.cursorSession?.owner === this) {
await this.cursorSession.endSession({ error });
}
if (!session?.inTransaction()) {
maybeClearPinnedConnection(session, { error });
if (!this.cursorSession?.inTransaction()) {
maybeClearPinnedConnection(this.cursorSession, { error });
}
} finally {
this.emitClose();
Expand Down
4 changes: 2 additions & 2 deletions test/integration/crud/misc_cursors.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const sinon = require('sinon');
const { Writable } = require('stream');
const { once, on } = require('events');
const { setTimeout } = require('timers');
const { ReadPreference, MongoExpiredSessionError } = require('../../mongodb');
const { ReadPreference } = require('../../mongodb');
const { ServerType } = require('../../mongodb');
const { formatSort } = require('../../mongodb');

Expand Down Expand Up @@ -1872,7 +1872,7 @@ describe('Cursor', function () {
expect(cursor).to.have.property('closed', true);

const error = await rejectedEarlyBecauseClientClosed;
expect(error).to.be.instanceOf(MongoExpiredSessionError);
expect(error).to.be.null; // TODO: is this API or just "how it behaved at the time"
});

it('shouldAwaitData', {
Expand Down