Skip to content

Commit

Permalink
MongoDB: fix cursor destructors
Browse files Browse the repository at this point in the history
unsure how this hasn't triggered before - checked and fixed on MongoDB 7 now
  • Loading branch information
WebFreak001 authored and s-ludwig committed Feb 14, 2024
1 parent 7e2cece commit 4c96c6e
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 5 deletions.
4 changes: 4 additions & 0 deletions mongodb/vibe/db/mongo/connection.d
Original file line number Diff line number Diff line change
Expand Up @@ -631,6 +631,10 @@ final class MongoConnection {
{
Bson command = Bson.emptyObject;
auto parts = collection.findSplit(".");
if (!parts[2].length)
throw new MongoDriverException(
"Attempted to call killCursors with non-fully-qualified collection name: '"
~ collection ~ "'");
command["killCursors"] = Bson(parts[2]);
command["cursors"] = () @trusted { return cursors; } ().serializeToBson; // NOTE: "escaping" scope here
runCommand!Bson(parts[0], command);
Expand Down
19 changes: 14 additions & 5 deletions mongodb/vibe/db/mongo/cursor.d
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ module vibe.db.mongo.cursor;
public import vibe.data.bson;
public import vibe.db.mongo.impl.crud;

import vibe.core.log;

import vibe.db.mongo.connection;
import vibe.db.mongo.client;

Expand Down Expand Up @@ -115,10 +117,15 @@ struct MongoCursor(DocType = Bson) {
if( m_data ) m_data.refCount++;
}

~this()
~this() @safe
{
if( m_data && --m_data.refCount == 0 ){
m_data.killCursors();
try {
m_data.killCursors();
} catch (MongoException e) {
logWarn("MongoDB failed to kill cursors: %s", e.msg);
logDiagnostic("%s", (() @trusted => e.toString)());
}
}
}

Expand Down Expand Up @@ -383,8 +390,8 @@ private deprecated abstract class LegacyMongoCursorData(DocType) : IMongoCursorD

final void killCursors()
@safe {
if (m_cursor == 0) return;
auto conn = m_client.lockConnection();
if (m_cursor == 0) return;
conn.killCursors(m_collection, () @trusted { return (&m_cursor)[0 .. 1]; } ());
m_cursor = 0;
}
Expand Down Expand Up @@ -419,6 +426,7 @@ private class MongoFindCursor(DocType) : IMongoCursorData!DocType {
MongoClient m_client;
Bson m_findQuery;
string m_database;
string m_ns;
string m_collection;
long m_cursor;
int m_batchSize;
Expand Down Expand Up @@ -507,15 +515,16 @@ private class MongoFindCursor(DocType) : IMongoCursorData!DocType {

final void killCursors()
@safe {
if (m_cursor == 0) return;
auto conn = m_client.lockConnection();
conn.killCursors(m_collection, () @trusted { return (&m_cursor)[0 .. 1]; } ());
if (m_cursor == 0) return;
conn.killCursors(m_ns, () @trusted { return (&m_cursor)[0 .. 1]; } ());
m_cursor = 0;
}

final void handleReply(long id, string ns, size_t count)
{
m_cursor = id;
m_ns = ns;
// The qualified collection name is reported here, but when requesting
// data, we need to send the database name and the collection name
// separately, so we have to remove the database prefix:
Expand Down

0 comments on commit 4c96c6e

Please sign in to comment.