# Sendable Closures that are also isolated and synchronous?

**URL:** <https://forums.swift.org/t/sendable-closures-that-are-also-isolated-and-synchronous/85156>\
**Category:** Using Swift\
**Tags:** concurrency\
**Created:** [March 5, 2026, 6:13am UTC](https://forums.swift.org/t/sendable-closures-that-are-also-isolated-and-synchronous/85156 "2026-03-05T06:13:12Z")\
**Posts on this page:** 1\
**Showing post:** 11

<div class="post-metadata">

**Author:** ![Andropov](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/andropov/32/35955_2.png) [@Andropov](https://forums.swift.org/u/Andropov)\
**Post date:** [March 6, 2026, 11:17am UTC](https://forums.swift.org/t/sendable-closures-that-are-also-isolated-and-synchronous/85156/11 "2026-03-06T11:17:50Z")

</div>

> [@NotTheNHK](#):
>
> Interesting. However, I couldn’t verify that. Could you provide a sample?

In this sample:

```swift
func isMainThread() -> Bool {
    return Thread.isMainThread
}

let task = Task.detached {
    await withTaskCancellationHandler(
        operation: { @MainActor in
            print("operation() started -> isMainActor: \(isMainThread()), thread: \(pthread_self())")
            try? await Task.sleep(for: .seconds(2))
            print("operation() finished -> isMainActor: \(isMainThread()), thread: \(pthread_self())")
        },
        onCancel: {
            print("onCancel() -> isMainActor: \(isMainThread()), thread: \(pthread_self())")
        },
        isolation: MainActor.shared
    )
}

await Task.detached {
    try? await Task.sleep(for: .seconds(1))
    print("cancel() -> isMainActor: \(isMainThread()), thread: \(pthread_self())")
    task.cancel()
}.value

```

I get this output:

```txt
operation() started -> isMainActor: true, thread: 0x202e105c0
cancel() -> isMainActor: false, thread: 0x16fe87000
onCancel() -> isMainActor: false, thread: 0x16fe87000
operation() finished -> isMainActor: true, thread: 0x202e105c0

```

Which makes sense: the Main Actor can't execute both `operation` and `onCancel` concurrently.

You can also see that `cancel()` and `onCancel` happen on the same thread.

---

_[View the full topic](https://forums.swift.org/t/sendable-closures-that-are-also-isolated-and-synchronous/85156)._
