# Capture of self... in a '@Sendable'... Please make it stop!

**URL:** <https://forums.swift.org/t/capture-of-self-in-a-sendable-please-make-it-stop/76463>\
**Category:** Using Swift\
**Created:** [December 8, 2024, 11:58am UTC](https://forums.swift.org/t/capture-of-self-in-a-sendable-please-make-it-stop/76463 "2024-12-08T11:58:54Z")\
**Posts on this page:** 12\
**Page:** 2

<div class="post-metadata">

**Author:** ![Slava\_Pestov](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/slava_pestov/32/30204_2.png) [@Slava\_Pestov](https://forums.swift.org/u/Slava_Pestov)\
**Post date:** [December 9, 2024, 4:57pm UTC](https://forums.swift.org/t/capture-of-self-in-a-sendable-please-make-it-stop/76463/21 "2024-12-09T16:57:33Z")

</div>

> [@jamieQ](#):
>
> i think we can deduce that there must be because the second function is being called in a synchronous context (a work item execution block).

That makes sense, thanks. Perhaps the work function doesn't need to be an _instance method_ of AnalysisLoader, then. @ConfusedVorlon does it actually access any instance members of `self`? Changing it to be a static method or top-level function would also fix the warning.

---

<div class="post-metadata">

**Author:** ![ConfusedVorlon](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/confusedvorlon/32/14854_2.png) [@ConfusedVorlon](https://forums.swift.org/u/ConfusedVorlon)\
**Post date:** [December 9, 2024, 5:06pm UTC](https://forums.swift.org/t/capture-of-self-in-a-sendable-please-make-it-stop/76463/22 "2024-12-09T17:06:33Z")

</div>

> [@Slava\_Pestov](#):
>
> Is there another non-async overload of self.analyse() that gets called inside the closure though? As shown your code leads to infinite recursion.

yes - within the queue, it calls the sync `analyse` function.

or to put it another way, this is the async wrapper around `func analyse()`

> [@hborla](#):
>
> Just to confirm, are you building with minimal concurrency checking?

Confirmed

 ![Screenshot 2024-12-09 at 17.01.02](https://global.discourse-cdn.com/swift/original/3X/3/c/3cee53a15080d9c5893a0d4020c115381706c08f.png)

> [@hborla](#):
>
> If you're building with minimal checking, this sounds like a compiler bug because the Sendable violation should be suppressed.

I got a whole bunch of sendable warnings with 16.2 RC  
(image in the vision thread)

> [@Slava\_Pestov](#):
>
> Perhaps the work function doesn't need to be an _instance method_ of AnalysisLoader, then. @ConfusedVorlon does it actually access any instance members of `self`?

It does.  
Tbh though - the answer to that question would always be case specific. There will be at least some cases where a static method isn't a good solution. I'm more concerned about the direction of travel than the implications on this specific class.

---

<div class="post-metadata">

**Author:** ![Jon\_Shier](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/jon_shier/32/921_2.png) [@Jon\_Shier](https://forums.swift.org/u/Jon_Shier)\
**Post date:** [December 9, 2024, 5:24pm UTC](https://forums.swift.org/t/capture-of-self-in-a-sendable-please-make-it-stop/76463/23 "2024-12-09T17:24:28Z")

</div>

I suggest you also turn on the upcoming features "Region Based Isolation", "Isolated Default Values", and "Infer Sendable for Methods and Key Path Literals". Helps even minimal checking be more intelligent.

---

<div class="post-metadata">

**Author:** ![hborla](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/hborla/32/27563_2.png) [@hborla](https://forums.swift.org/u/hborla)\
**Post date:** [December 9, 2024, 5:42pm UTC](https://forums.swift.org/t/capture-of-self-in-a-sendable-please-make-it-stop/76463/24 "2024-12-09T17:42:43Z")

</div>

> [@ConfusedVorlon](#):
>
> > [@hborla](#):
> >
> > If you're building with minimal checking, this sounds like a compiler bug because the Sendable violation should be suppressed.
> 
> I got a whole bunch of sendable warnings with 16.2 RC

Please file a [GitHub issue](https://github.com/swiftlang/swift/issues/new/choose) or report the issue using [Apple Feedback Assistant](https://developer.apple.com/bug-reporting/) and attach your project.

---

<div class="post-metadata">

**Author:** ![Sean.Batson](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/sean.batson/32/40290_2.png) [@Sean.Batson](https://forums.swift.org/u/Sean.Batson)\
**Post date:** [December 14, 2024, 2:20pm UTC](https://forums.swift.org/t/capture-of-self-in-a-sendable-please-make-it-stop/76463/25 "2024-12-14T14:20:46Z")

</div>

I now understand why the US government is pushing the open source community towards rust and away from c/c++ languages. Next they will place the same recommendations on swift if programmers with this mindset can’t adopt to the best practices related to code quality and safety.

---

<div class="post-metadata">

**Author:** ![malhal](https://avatars.discourse-cdn.com/v4/letter/m/838e76/32.png) [@malhal](https://forums.swift.org/u/malhal)\
**Post date:** [December 14, 2024, 3:13pm UTC](https://forums.swift.org/t/capture-of-self-in-a-sendable-please-make-it-stop/76463/26 "2024-12-14T15:13:59Z")

</div>

`withCheckedContinuation` should init and own the `AnalysisLoader` object.

But your design is wrong. Move this `analyse` async func out of this class into its own controller struct, call that `AnalysisLoader`. Pass your model as a parm into the async func it for it to work on, call that model `AnalysisModel`.

Think of async/await as controller funcs that work on your objects "from the outside". You can't have async funcs inside model classes because if the funcs are called simultaneously your model will be corrupt and now Swift 6 won't allow it.

---

<div class="post-metadata">

**Author:** ![malhal](https://avatars.discourse-cdn.com/v4/letter/m/838e76/32.png) [@malhal](https://forums.swift.org/u/malhal)\
**Post date:** [December 14, 2024, 3:20pm UTC](https://forums.swift.org/t/capture-of-self-in-a-sendable-please-make-it-stop/76463/27 "2024-12-14T15:20:20Z")

</div>

> [@Slava\_Pestov](#):
>
> Changing it to be a static method or top-level function would also fix the warning.

It would make more sense if the async func was moved into its own struct. Then the model class could be passed into the func as a param.

---

<div class="post-metadata">

**Author:** ![ConfusedVorlon](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/confusedvorlon/32/14854_2.png) [@ConfusedVorlon](https://forums.swift.org/u/ConfusedVorlon)\
**Post date:** [December 14, 2024, 3:41pm UTC](https://forums.swift.org/t/capture-of-self-in-a-sendable-please-make-it-stop/76463/28 "2024-12-14T15:41:07Z")

</div>

> [@malhal](#):
>
> But your design is wrong. Move this `analyse` async func out of this class into its own controller struct, call that `AnalysisLoader`. Pass your model as a parm into the async func it for it to work on, call that model `AnalysisModel`.

It's weird for you to so confidently tell me my design is wrong given that you haven't seen the class or what it does.

It has one job which is to run the analyse function.

That function accesses some state that is loaded at init, and breaks the work into various private functions for clarity.

The only code you have seen is the async wrapper around that analyse function. And in fact, this async wrapper is the only non-private method on the class.

Perhaps Swift 6 really does require an async wrapper to exist in it's own controller class. If so - that seems pretty badly designed to me.

---

<div class="post-metadata">

**Author:** ![malhal](https://avatars.discourse-cdn.com/v4/letter/m/838e76/32.png) [@malhal](https://forums.swift.org/u/malhal)\
**Post date:** [December 14, 2024, 4:39pm UTC](https://forums.swift.org/t/capture-of-self-in-a-sendable-please-make-it-stop/76463/29 "2024-12-14T16:39:03Z")

</div>

With Swift you are supposed to choose struct first and change to use class when you need to control identity of some data. So most people wouldn't have put async func in class in the first place because they'd have a struct. I wouldn't call this an "async wrapper".

---

<div class="post-metadata">

**Author:** ![ConfusedVorlon](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/confusedvorlon/32/14854_2.png) [@ConfusedVorlon](https://forums.swift.org/u/ConfusedVorlon)\
**Post date:** [December 14, 2024, 4:47pm UTC](https://forums.swift.org/t/capture-of-self-in-a-sendable-please-make-it-stop/76463/30 "2024-12-14T16:47:53Z")

</div>

> [@malhal](#):
>
> I wouldn't call this an "async wrapper".

What would you like me to call a function which wraps a non-async function so that it can be called asynchronously using swift concurrency?

I'm talking about the kind of wrapping described here..

> **[Wrapping completion handlers into async APIs | WWDC by Sundell & Friends](https://wwdcbysundell.com/2021/wrapping-completion-handlers-into-async-apis/)**
>
> How can we wrap our existing, completion handler-based asynchronous APIs into shiny new async functions, without having to rewrite them from scratch?

---

<div class="post-metadata">

**Author:** ![malhal](https://avatars.discourse-cdn.com/v4/letter/m/838e76/32.png) [@malhal](https://forums.swift.org/u/malhal)\
**Post date:** [December 14, 2024, 4:58pm UTC](https://forums.swift.org/t/capture-of-self-in-a-sendable-please-make-it-stop/76463/31 "2024-12-14T16:58:56Z")

</div>

That wrapping is bad, I think Sundell was a bit too quick to blog that.

---

<div class="post-metadata">

**Author:** ![ConfusedVorlon](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/confusedvorlon/32/14854_2.png) [@ConfusedVorlon](https://forums.swift.org/u/ConfusedVorlon)\
**Post date:** [December 14, 2024, 5:37pm UTC](https://forums.swift.org/t/capture-of-self-in-a-sendable-please-make-it-stop/76463/32 "2024-12-14T17:37:37Z")

</div>

> [@malhal](#):
>
> That wrapping is bad, I think Sundell was a bit too quick to blog that.

Hardly a controversial approach. XCode literally had that as a built-in refactor...

'Add Async Wrapper'

But apart from disapproving of me wrapping non-async code, you didn't say what you wanted me to call my wrapping function...

[Previous page](https://forums.swift.org/t/capture-of-self-in-a-sendable-please-make-it-stop/76463.md?page=1)
