Skip to content

[6.2][Concurrency] Fix alreadyLocked in withStatusRecordLock. #81206

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 1, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions stdlib/public/Concurrency/TaskPrivate.h
Original file line number Diff line number Diff line change
Expand Up @@ -829,11 +829,12 @@ struct AsyncTask::PrivateStorage {
// result of the task
auto oldStatus = task->_private()._status().load(std::memory_order_relaxed);
while (true) {
// Task is completing
assert(oldStatus.getInnermostRecord() == NULL &&
"Status records should have been removed by this time!");
assert(!oldStatus.isStatusRecordLocked() &&
"Task is completing, cannot be locked anymore!");

// Don't assert !isStatusRecordLocked(). It can be legitimately true here,
// for example if another thread is canceling this task right as it
// completes.

assert(oldStatus.isRunning());

Expand Down
14 changes: 13 additions & 1 deletion stdlib/public/Concurrency/TaskStatus.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,19 @@ static void withStatusRecordLock(
// see that the is-locked bit is now set, and then wait for the lock.
task->_private().statusLock.lock();

bool alreadyLocked = status.isStatusRecordLocked();
bool alreadyLocked = false;

// `status` was loaded before we acquired the lock. If its is-locked bit is
// not set, then we know that this thread doesn't already hold the lock.
// However, if the is-locked bit is set, then we don't know if this thread
// held the lock or another thread did. In that case, we reload the status
// after acquiring the lock. If the reloaded status still has the is-locked
// bit set, then we know it's this thread. If it doesn't, then we know it was
// a different thread.
if (status.isStatusRecordLocked()) {
status = task->_private()._status().load(std::memory_order_relaxed);
alreadyLocked = status.isStatusRecordLocked();
}

// If it's already locked then this thread is the thread that locked it, and
// we can leave that bit alone here.
Expand Down
33 changes: 33 additions & 0 deletions test/Concurrency/Runtime/cancellation_handler_concurrent.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// RUN: %target-run-simple-swift( -target %target-swift-5.1-abi-triple %import-libdispatch)
// REQUIRES: concurrency
// REQUIRES: executable_test

// REQUIRES: concurrency_runtime
// UNSUPPORTED: freestanding

func recurseABunch(_ call: () async throws -> Void, n: Int = 100) async throws {
try await withTaskCancellationHandler {
if n == 0 {
try await call()
return
}

try await recurseABunch(call, n: n - 1)
} onCancel: {
}
}

for _ in 0..<100_000 {
let task: Task<Void, any Error> = Task {
try await Task.sleep(nanoseconds: UInt64.random(in: 0..<1_000))
try await recurseABunch() {
await withTaskCancellationHandler {
} onCancel: {
}
}
}
Task {
try await Task.sleep(nanoseconds: UInt64.random(in: 0..<1_000))
task.cancel()
}
}