Skip to content

Commit 3b3b6b3

Browse files
authored
SWIFT-974 Fix leak in Collection.findOne when encountering DecodingError (#522)
1 parent 1bea597 commit 3b3b6b3

File tree

3 files changed

+15
-1
lines changed

3 files changed

+15
-1
lines changed

Sources/MongoSwift/MongoCollection+Read.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ extension MongoCollection {
5858
) -> EventLoopFuture<T?> {
5959
let options = options.map { FindOptions(from: $0) }
6060
return self.find(filter, options: options, session: session).flatMap { cursor in
61-
cursor.next().afterSuccess { _ in cursor.kill() }
61+
cursor.next().always { _ in _ = cursor.kill() }
6262
}
6363
}
6464

Tests/LinuxMain.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,7 @@ extension MongoCollectionTests {
219219
("testFindOneAndUpdate", testFindOneAndUpdate),
220220
("testNullIds", testNullIds),
221221
("testNSNotFoundSuppression", testNSNotFoundSuppression),
222+
("testFindOneKillsCursor", testFindOneKillsCursor),
222223
]
223224
}
224225

Tests/MongoSwiftSyncTests/MongoCollectionTests.swift

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -550,4 +550,17 @@ final class MongoCollectionTests: MongoSwiftTestCase {
550550
expect(try collection.drop()).toNot(throwError())
551551
expect(try collection.dropIndex("ljasdfjlkasdjf")).toNot(throwError())
552552
}
553+
554+
func testFindOneKillsCursor() throws {
555+
struct Blah: Codable {
556+
let ljadsflkjasdf: String // won't be present in result doc
557+
}
558+
try self.withTestNamespace { _, db, coll in
559+
try coll.insertOne([:])
560+
561+
let codableColl = db.collection(coll.name, withType: Blah.self)
562+
_ = try? codableColl.findOne([:])
563+
// if the underlying cursor is leaked, this will crash in debug mode
564+
}
565+
}
553566
}

0 commit comments

Comments
 (0)