summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBenety Goh <benety@mongodb.com>2022-04-06 16:14:16 -0400
committerEvergreen Agent <no-reply@evergreen.mongodb.com>2022-04-14 11:22:17 +0000
commit841ee6f337ba7b6cc55db999a2455a517838e0e1 (patch)
treefeba165bdb6f1633687a10e9eb4e7e6b11918f9c
parent3484840e3a1eb6203f29cc04cb3e2459c53991dd (diff)
downloadmongo-841ee6f337ba7b6cc55db999a2455a517838e0e1.tar.gz
SERVER-65137 CollectionPtr::restore() should fail (return nullptr) if collection was renamed while yielding
(cherry picked from commit fed598b7d0b6c54d64138379a6ec11cfc601f7ae)
-rw-r--r--src/mongo/db/catalog/collection_catalog.cpp16
-rw-r--r--src/mongo/db/catalog/collection_catalog_test.cpp3
2 files changed, 14 insertions, 5 deletions
diff --git a/src/mongo/db/catalog/collection_catalog.cpp b/src/mongo/db/catalog/collection_catalog.cpp
index ff0fc056e75..205971c76ed 100644
--- a/src/mongo/db/catalog/collection_catalog.cpp
+++ b/src/mongo/db/catalog/collection_catalog.cpp
@@ -1212,9 +1212,19 @@ void CollectionCatalogStasher::reset() {
const Collection* LookupCollectionForYieldRestore::operator()(OperationContext* opCtx,
const UUID& uuid) const {
- auto collection = CollectionCatalog::get(opCtx)->lookupCollectionByUUID(opCtx, uuid).get();
- if (!collection)
+ auto collection = CollectionCatalog::get(opCtx)->lookupCollectionByUUIDForRead(opCtx, uuid);
+
+ // Collection dropped during yielding.
+ if (!collection) {
+ return nullptr;
+ }
+
+ // Collection renamed during yielding.
+ // This check ensures that we are locked on the same namespace and that it is safe to return
+ // the C-style pointer to the Collection.
+ if (collection->ns() != _nss) {
return nullptr;
+ }
// After yielding and reacquiring locks, the preconditions that were used to select our
// ReadSource initially need to be checked again. We select a ReadSource based on replication
@@ -1226,7 +1236,7 @@ const Collection* LookupCollectionForYieldRestore::operator()(OperationContext*
opCtx->recoveryUnit()->setTimestampReadSource(*newReadSource);
}
- return collection;
+ return collection.get();
}
BatchedCollectionCatalogWriter::BatchedCollectionCatalogWriter(OperationContext* opCtx)
diff --git a/src/mongo/db/catalog/collection_catalog_test.cpp b/src/mongo/db/catalog/collection_catalog_test.cpp
index 5da20ea330b..bff2c2db4dd 100644
--- a/src/mongo/db/catalog/collection_catalog_test.cpp
+++ b/src/mongo/db/catalog/collection_catalog_test.cpp
@@ -513,8 +513,7 @@ TEST_F(CollectionCatalogTest, RenameCollection) {
// After renaming the collection, we should fail to restore the CollectionPtr.
yieldableColl.restore();
- ASSERT(yieldableColl);
- ASSERT_EQUALS(yieldableColl, collection);
+ ASSERT_FALSE(yieldableColl);
}
TEST_F(CollectionCatalogTest, LookupNSSByUUIDForClosedCatalogReturnsOldNSSIfDropped) {