From 7361e94af31142ec018ccad70b0398e3a472eba5 Mon Sep 17 00:00:00 2001 From: Siyuan Zhou Date: Tue, 21 Mar 2017 21:05:45 -0400 Subject: SERVER-28181 Fix the deadlock in OplogFetcher's constructor. --- src/mongo/db/repl/bgsync.cpp | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) (limited to 'src') diff --git a/src/mongo/db/repl/bgsync.cpp b/src/mongo/db/repl/bgsync.cpp index 86cc962c926..79d7d294f63 100644 --- a/src/mongo/db/repl/bgsync.cpp +++ b/src/mongo/db/repl/bgsync.cpp @@ -380,20 +380,18 @@ void BackgroundSync::_produce(OperationContext* opCtx) { _replCoord, _replicationCoordinatorExternalState, this); OplogFetcher* oplogFetcher; try { - auto executor = _replicationCoordinatorExternalState->getTaskExecutor(); - auto config = _replCoord->getConfig(); auto onOplogFetcherShutdownCallbackFn = [&fetcherReturnStatus](const Status& status, const OpTimeWithHash& lastFetched) { fetcherReturnStatus = status; }; - - stdx::lock_guard lock(_mutex); - _oplogFetcher = stdx::make_unique( - executor, + // The construction of OplogFetcher has to be outside bgsync mutex, because it calls + // replication coordinator. + auto oplogFetcherPtr = stdx::make_unique( + _replicationCoordinatorExternalState->getTaskExecutor(), OpTimeWithHash(lastHashFetched, lastOpTimeFetched), source, NamespaceString(rsOplogName), - config, + _replCoord->getConfig(), _replicationCoordinatorExternalState->getOplogFetcherMaxFetcherRestarts(), syncSourceResp.rbid, true /* requireFresherSyncSource */, @@ -404,6 +402,8 @@ void BackgroundSync::_produce(OperationContext* opCtx) { stdx::placeholders::_2, stdx::placeholders::_3), onOplogFetcherShutdownCallbackFn); + stdx::lock_guard lock(_mutex); + _oplogFetcher = std::move(oplogFetcherPtr); oplogFetcher = _oplogFetcher.get(); } catch (const mongo::DBException& ex) { fassertFailedWithStatus(34440, exceptionToStatus()); -- cgit v1.2.1