# addTeardownBlock completion assigned to the @MainActor

**URL:** <https://forums.swift.org/t/addteardownblock-completion-assigned-to-the-mainactor/83255>\
**Category:** Core Libraries\
**Tags:** concurrency, xctest, unit-testing, swift6\
**Created:** [November 17, 2025, 6:00am UTC](https://forums.swift.org/t/addteardownblock-completion-assigned-to-the-mainactor/83255 "2025-11-17T06:00:53Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![dataports](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/dataports/32/43469_2.png) [@dataports](https://forums.swift.org/u/dataports)\
**Post date:** [November 17, 2025, 6:00am UTC](https://forums.swift.org/t/addteardownblock-completion-assigned-to-the-mainactor/83255/1 "2025-11-17T06:00:53Z")

</div>

Hi all,

I’m midway through a Swift 6 migration and have been migrating some unit test files recently and have been struggling a bit with whather to use MainActor in unit tests as a means to isolate certain bits of code under tests, when the code under test wouldn’t necessarily be isolated under MainActor normally in production.

Up until now, I figured this is fine in the context of a “pure” unit test since ideally you’re just testing discrete bits of business logic, and where it runs in the context of a unit test probably doesn’t matter.

However I came across this case where we’re using `addTeardownBlock` from `XCTestCase` to track memory leaks in classes that are being tested.

 ![Screenshot 2025-11-16 at 9.44.57 PM](https://global.discourse-cdn.com/swift/original/3X/2/b/2b2a708a61376d238041f0cdba0677cb14727b96.png)

The heart of the issue seems that not only is `AnyObject` not Sendable, but also that `addTeardownBlock`'s block is supposed to run on MainActor.

Definition according to the documentation:  
`@preconcurrency￼func addTeardownBlock(_ block: @escaping @MainActor () throws -> Void)`

The approach I want to take here is to just isolate the places where this is called to MainActor as a work around, but this brings me to my actual question, which is:  
**Why is `block` on `addTeardownBlock` isolated to the MainActor in the first place?**

---

<div class="post-metadata">

**Author:** ![dataports](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/dataports/32/43469_2.png) [@dataports](https://forums.swift.org/u/dataports)\
**Post date:** [November 17, 2025, 9:06pm UTC](https://forums.swift.org/t/addteardownblock-completion-assigned-to-the-mainactor/83255/3 "2025-11-17T21:06:15Z")

</div>

Thank you for sharing your thoughts and confirmation. I appreciate it!
