apple / apple/swift-async-algorithms

Can MultiProducerSingleConsumerAsyncChannel.Source be Sendable?

Open
#402 8 comments 0 reactions 0 assignees View on GitHub
Dominant language
Swift
Stars
3.7k
Forks
226
Avg merge
10d 3h
Merged PRs (30d)
1

Description

I would like to use MPSCAsyncChannel as following:
```
private final class ShardedData: Sendable {
typealias Channel = MultiProducerSingleConsumerAsyncChannel
let shards: UniqueArray

func send(idx: Int, value: Int) async throws {
try await shards[idx].send(value)
}
}
```

Unfortunately Source is mutating on send() and non-sendable itself.
I wonder if that is intentional and if it is safe to workaround it wrapping into `@uncheked Sendable` class:
```
private final class ShardedData: Sendable {
typealias Channel = MultiProducerSingleConsumerAsyncChannel

final class SourceWrap: @unchecked Sendable {
var source: Channel.Source
init(source: consuming Channel.Source) {
self.source = source
}
}

let shards: UniqueArray

func send(idx: Int, value: Int) async throws {
try await shards[idx].source.send(value)
}
}
```

or/and as:
```
private nonisolated(unsafe) var shards: UniqueArray
```

Contributor guide

Open the contributing guide

Research direction

Start with MultiProducerSingleConsumerAsyncChannel.Source and its mutating send() entry point; the issue names no files or tests. Determine whether the requested Sendable usage is safe, including the proposed wrappers, and document the maintainers' decision or resulting API direction.

Written by the indexing model from the issue text.

Assessment

Tech stack
swift
Domain
api
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.