Skip to content

[Concurrency] TaskExecutors may be non-swift objects; dont swift_release them #75059

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
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
6 changes: 5 additions & 1 deletion stdlib/public/Concurrency/TaskStatus.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -772,7 +772,11 @@ void AsyncTask::dropInitialTaskExecutorPreferenceRecord() {
//
// This should not be done for withTaskExecutorPreference executors,
// however in that case, we would not enter this function here to clean up.
swift_release(executorIdentityToRelease);
//
// NOTE: This MUST NOT assume that the object is a swift object (and use
// swift_release), because a dispatch_queue_t conforms to TaskExecutor,
// and may be passed in here; in which case swift_releasing it would be incorrect.
swift_unknownObjectRelease(executorIdentityToRelease);
}

/**************************************************************************/
Expand Down
49 changes: 49 additions & 0 deletions test/Concurrency/Runtime/async_task_executor_nsobject.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// RUN: %target-run-simple-swift( -Xfrontend -disable-availability-checking %import-libdispatch -parse-as-library )

// REQUIRES: executable_test
// REQUIRES: concurrency
// REQUIRES: libdispatch

// REQUIRES: OS=macosx
// REQUIRES: objc_interop

// REQUIRES: concurrency_runtime
// UNSUPPORTED: back_deployment_runtime

import Dispatch
import StdlibUnittest
import _Concurrency

import Foundation
import Darwin

// Sneaky trick to make this type objc reference counted.
//
// This test specifically checks that our reference counting accounts for existence of
// objective-c types as TaskExecutors -- which was a bug where we'd swift_release
// obj-c excecutors by accident (rdar://131151645).
final class NSQueueTaskExecutor: NSData, TaskExecutor, @unchecked Sendable {
public func enqueue(_ _job: consuming ExecutorJob) {
let job = UnownedJob(_job)
DispatchQueue.main.async {
job.runSynchronously(on: self.asUnownedTaskExecutor())
}
}
}

@main struct Main {
static func main() async {
var taskExecutor: (any TaskExecutor)? = NSQueueTaskExecutor()

let task = Task(executorPreference: taskExecutor) {
dispatchPrecondition(condition: .onQueue(DispatchQueue.main))
try? await Task.sleep(for: .seconds(2))
return 12
}

taskExecutor = nil

let num = await task.value
assert(num == 12)
}
}