@@ -318,18 +318,16 @@ ScopedShardVersionCriticalSection::ScopedShardVersionCriticalSection(OperationCo
318318 }
319319 }
320320
321- forceShardFilteringMetadataRefresh (_opCtx, _nss);
321+ try {
322+ forceShardFilteringMetadataRefresh (_opCtx, _nss);
323+ } catch (const DBException&) {
324+ _cleanup ();
325+ throw ;
326+ }
322327}
323328
324329ScopedShardVersionCriticalSection::~ScopedShardVersionCriticalSection () {
325- UninterruptibleLockGuard noInterrupt (_opCtx->lockState ());
326- // DBLock and CollectionLock are used here to avoid throwing further recursive stale config
327- // errors.
328- Lock::DBLock dbLock (_opCtx, _nss.db (), MODE_IX);
329- Lock::CollectionLock collLock (_opCtx, _nss, MODE_IX);
330- auto * const csr = CollectionShardingRuntime::get (_opCtx, _nss);
331- auto csrLock = CollectionShardingRuntime::CSRLock::lockExclusive (_opCtx, csr);
332- csr->exitCriticalSection (csrLock, _reason);
330+ _cleanup ();
333331}
334332
335333void ScopedShardVersionCriticalSection::enterCommitPhase () {
@@ -344,6 +342,17 @@ void ScopedShardVersionCriticalSection::enterCommitPhase() {
344342 csr->enterCriticalSectionCommitPhase (csrLock, _reason);
345343}
346344
345+ void ScopedShardVersionCriticalSection::_cleanup () {
346+ UninterruptibleLockGuard noInterrupt (_opCtx->lockState ());
347+ // DBLock and CollectionLock are used here to avoid throwing further recursive stale config
348+ // errors.
349+ Lock::DBLock dbLock (_opCtx, _nss.db (), MODE_IX);
350+ Lock::CollectionLock collLock (_opCtx, _nss, MODE_IX);
351+ auto * const csr = CollectionShardingRuntime::get (_opCtx, _nss);
352+ auto csrLock = CollectionShardingRuntime::CSRLock::lockExclusive (_opCtx, csr);
353+ csr->exitCriticalSection (csrLock, _reason);
354+ }
355+
347356Status onShardVersionMismatchNoExcept (OperationContext* opCtx,
348357 const NamespaceString& nss,
349358 boost::optional<ChunkVersion> shardVersionReceived) noexcept {
0 commit comments