Skip to content

Commit cafe0d9

Browse files
committed
YT-23585: Fix race between Cancel and GetCanceledError in Canceler
commit_hash:2c1b31d95037975cf4703cb90f81232f880a37e6
1 parent 7d6de11 commit cafe0d9

File tree

1 file changed

+14
-11
lines changed

1 file changed

+14
-11
lines changed

yt/yt/core/concurrency/fiber_scheduler_thread.cpp

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -621,7 +621,17 @@ class TCanceler
621621
void SetFuture(TFuture<void> awaitable)
622622
{
623623
auto guard = Guard(Lock_);
624-
Future_ = std::move(awaitable);
624+
if (!IsCanceled()) {
625+
Future_ = std::move(awaitable);
626+
return;
627+
}
628+
629+
guard.Release();
630+
631+
ErrorSet_.Wait();
632+
633+
YT_ASSERT(!CancelationError_.IsOK());
634+
awaitable.Cancel(CancelationError_);
625635
}
626636

627637
void ResetFuture()
@@ -644,6 +654,8 @@ class TCanceler
644654
future = std::move(Future_);
645655
}
646656

657+
ErrorSet_.NotifyAll();
658+
647659
if (future) {
648660
YT_LOG_DEBUG("Sending cancelation to fiber, propagating to the awaited future (TargetFiberId: %x)",
649661
FiberId_);
@@ -654,12 +666,6 @@ class TCanceler
654666
}
655667
}
656668

657-
TError GetCancelationError() const
658-
{
659-
auto guard = Guard(Lock_);
660-
return CancelationError_;
661-
}
662-
663669
void Run(const TError& error)
664670
{
665671
Cancel(error);
@@ -680,6 +686,7 @@ class TCanceler
680686
const TFiberId FiberId_;
681687

682688
std::atomic<bool> Canceled_ = false;
689+
NThreading::TEvent ErrorSet_;
683690
NThreading::TSpinLock Lock_;
684691
TError CancelationError_;
685692
TFuture<void> Future_;
@@ -1173,10 +1180,6 @@ void WaitUntilSet(TFuture<void> future, IInvokerPtr invoker)
11731180
GetCurrentFiberCanceler();
11741181

11751182
const auto& canceler = NDetail::GetFiberSwitchHandler()->Canceler();
1176-
if (canceler->IsCanceled()) {
1177-
future.Cancel(canceler->GetCancelationError());
1178-
}
1179-
11801183
canceler->SetFuture(future);
11811184
auto finally = Finally([&] {
11821185
canceler->ResetFuture();

0 commit comments

Comments
 (0)