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-20 10:41:09 +0000
commit43253c80d557e82f96c175d01c95953d506cceda (patch)
tree0d1fafe84383736c298415fcc73ee507727ecd1d
parentc87e1c23421bf79614baf500fda6622bd90f674e (diff)
downloadmongo-43253c80d557e82f96c175d01c95953d506cceda.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 89272400de5..2ca8ce0d020 100644
--- a/src/mongo/db/catalog/collection_catalog.cpp
+++ b/src/mongo/db/catalog/collection_catalog.cpp
@@ -1198,9 +1198,19 @@ void CollectionCatalogStasher::reset() {
const Collection* LookupCollectionForYieldRestore::operator()(OperationContext* opCtx,
CollectionUUID 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
@@ -1212,7 +1222,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 6f356361158..0de39027d4f 100644
--- a/src/mongo/db/catalog/collection_catalog_test.cpp
+++ b/src/mongo/db/catalog/collection_catalog_test.cpp
@@ -500,8 +500,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) {