summaryrefslogtreecommitdiff
path: root/src/mongo/db/cancelable_operation_context.cpp
diff options
context:
space:
mode:
authorMax Hirschhorn <max.hirschhorn@mongodb.com>2021-04-28 21:36:47 +0000
committerEvergreen Agent <no-reply@evergreen.mongodb.com>2021-04-28 22:34:37 +0000
commitfb796e3eda46b39668a00e1e2caceba34f98d31c (patch)
tree017843bdf1eab04b3ce798b3db949c210af0aaab /src/mongo/db/cancelable_operation_context.cpp
parent09e418805e9019cb56e54d240c7c7d9bcb95e339 (diff)
downloadmongo-fb796e3eda46b39668a00e1e2caceba34f98d31c.tar.gz
SERVER-56436 Immediately kill OperationContext if already canceled.
Diffstat (limited to 'src/mongo/db/cancelable_operation_context.cpp')
-rw-r--r--src/mongo/db/cancelable_operation_context.cpp26
1 files changed, 17 insertions, 9 deletions
diff --git a/src/mongo/db/cancelable_operation_context.cpp b/src/mongo/db/cancelable_operation_context.cpp
index f00457694bd..abcee949cb8 100644
--- a/src/mongo/db/cancelable_operation_context.cpp
+++ b/src/mongo/db/cancelable_operation_context.cpp
@@ -41,15 +41,23 @@ CancelableOperationContext::CancelableOperationContext(ServiceContext::UniqueOpe
ExecutorPtr executor)
: _sharedBlock{std::make_shared<SharedBlock>()},
_opCtx{std::move(opCtx)},
- _markKilledFinished{cancelToken.onCancel()
- .thenRunOn(std::move(executor))
- .then([sharedBlock = _sharedBlock, opCtx = _opCtx.get()] {
- if (!sharedBlock->done.swap(true)) {
- stdx::lock_guard<Client> lk(*opCtx->getClient());
- opCtx->markKilled(ErrorCodes::Interrupted);
- }
- })
- .semi()} {}
+ _markKilledFinished{[&] {
+ if (cancelToken.isCanceled()) {
+ // This thread owns _opCtx so it isn't necessary to acquire the Client mutex.
+ _opCtx->markKilled(ErrorCodes::Interrupted);
+ return makeReadyFutureWith([] {}).semi();
+ }
+
+ return cancelToken.onCancel()
+ .thenRunOn(std::move(executor))
+ .then([sharedBlock = _sharedBlock, opCtx = _opCtx.get()] {
+ if (!sharedBlock->done.swap(true)) {
+ stdx::lock_guard<Client> lk(*opCtx->getClient());
+ opCtx->markKilled(ErrorCodes::Interrupted);
+ }
+ })
+ .semi();
+ }()} {}
CancelableOperationContext::~CancelableOperationContext() {
if (_sharedBlock->done.swap(true)) {