Skip to content

Commit a082c2a

Browse files
atesteveEvergreen Agent
authored andcommitted
SERVER-83161 Fix concurrent read to _errMsg from MigrationDestinationManager without acquiring mutex
1 parent 7593e97 commit a082c2a

File tree

4 files changed

+19
-12
lines changed

4 files changed

+19
-12
lines changed

src/mongo/db/s/migration_destination_manager.cpp

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@
131131
#include "mongo/util/namespace_string_util.h"
132132
#include "mongo/util/out_of_line_executor.h"
133133
#include "mongo/util/producer_consumer_queue.h"
134+
#include "mongo/util/scopeguard.h"
134135
#include "mongo/util/str.h"
135136
#include "mongo/util/time_support.h"
136137

@@ -1245,12 +1246,19 @@ void MigrationDestinationManager::_migrateDriver(OperationContext* outerOpCtx,
12451246
invariant(!_min.isEmpty());
12461247
invariant(!_max.isEmpty());
12471248

1248-
boost::optional<MoveTimingHelper> timing;
12491249
boost::optional<Timer> timeInCriticalSection;
1250+
boost::optional<MoveTimingHelper> timing;
1251+
mongo::ScopeGuard timingSetMsgGuard{[this, &timing] {
1252+
// Set the error message to MoveTimingHelper just before it is destroyed. The destructor
1253+
// sends that message (among other things) to the ShardingLogging.
1254+
if (timing) {
1255+
stdx::lock_guard<Latch> sl(_mutex);
1256+
timing->setCmdErrMsg(_errmsg);
1257+
}
1258+
}};
12501259

12511260
if (!skipToCritSecTaken) {
1252-
timing.emplace(
1253-
outerOpCtx, "to", _nss, _min, _max, 8 /* steps */, &_errmsg, _toShard, _fromShard);
1261+
timing.emplace(outerOpCtx, "to", _nss, _min, _max, 8 /* steps */, _toShard, _fromShard);
12541262

12551263
LOGV2(22000,
12561264
"Starting receiving end of chunk migration",

src/mongo/db/s/migration_source_manager.cpp

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -122,8 +122,6 @@ const WriteConcernOptions kMajorityWriteConcern(WriteConcernOptions::kMajority,
122122
WriteConcernOptions::SyncMode::UNSET,
123123
WriteConcernOptions::kWriteConcernTimeoutMigration);
124124

125-
std::string kEmptyErrMsgForMoveTimingHelper;
126-
127125
/*
128126
* Calculates the max or min bound perform split+move in case the chunk in question is splittable.
129127
* If the chunk is not splittable, returns the bound of the existing chunk for the max or min.Finds
@@ -211,7 +209,6 @@ MigrationSourceManager::MigrationSourceManager(OperationContext* opCtx,
211209
_args.getMin(),
212210
_args.getMax(),
213211
6, // Total number of steps
214-
&kEmptyErrMsgForMoveTimingHelper,
215212
_args.getToShard(),
216213
_args.getFromShard()) {
217214
invariant(!shard_role_details::getLocker(_opCtx)->isLocked());

src/mongo/db/s/move_timing_helper.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@ MoveTimingHelper::MoveTimingHelper(OperationContext* opCtx,
5757
const boost::optional<BSONObj>& min,
5858
const boost::optional<BSONObj>& max,
5959
int totalNumSteps,
60-
std::string* cmdErrmsg,
6160
const ShardId& toShard,
6261
const ShardId& fromShard)
6362
: _opCtx(opCtx),
@@ -68,7 +67,6 @@ MoveTimingHelper::MoveTimingHelper(OperationContext* opCtx,
6867
_min(min),
6968
_max(max),
7069
_totalNumSteps(totalNumSteps),
71-
_cmdErrmsg(cmdErrmsg),
7270
_nextStep(0) {}
7371

7472
MoveTimingHelper::~MoveTimingHelper() {
@@ -92,8 +90,8 @@ MoveTimingHelper::~MoveTimingHelper() {
9290
_b.append("note", "success");
9391
}
9492

95-
if (!_cmdErrmsg->empty()) {
96-
_b.append("errmsg", *_cmdErrmsg);
93+
if (!_cmdErrmsg.empty()) {
94+
_b.append("errmsg", _cmdErrmsg);
9795
}
9896

9997
ShardingLogging::get(_opCtx)->logChange(_opCtx,

src/mongo/db/s/move_timing_helper.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929

3030
#pragma once
3131

32+
#include "mongo/db/namespace_string.h"
3233
#include <boost/move/utility_core.hpp>
3334
#include <boost/optional/optional.hpp>
3435
#include <string>
@@ -52,7 +53,6 @@ class MoveTimingHelper {
5253
const boost::optional<BSONObj>& min,
5354
const boost::optional<BSONObj>& max,
5455
int totalNumSteps,
55-
std::string* cmdErrmsg,
5656
const ShardId& toShard,
5757
const ShardId& fromShard);
5858
~MoveTimingHelper();
@@ -65,6 +65,10 @@ class MoveTimingHelper {
6565
_max.emplace(max);
6666
}
6767

68+
void setCmdErrMsg(std::string cmdErrMsg) {
69+
_cmdErrmsg = std::move(cmdErrMsg);
70+
}
71+
6872
void done(int step);
6973

7074
private:
@@ -79,7 +83,7 @@ class MoveTimingHelper {
7983

8084
boost::optional<BSONObj> _min, _max;
8185
const int _totalNumSteps;
82-
const std::string* _cmdErrmsg;
86+
std::string _cmdErrmsg;
8387

8488
int _nextStep;
8589
BSONObjBuilder _b;

0 commit comments

Comments
 (0)