mirror of
https://github.com/apple/swift-async-algorithms
synced 2026-11-22 04:10:40 +00:00
Fix unbuffered share deadlock (#387)
* Introduce test_share_with_no_buffering
* Update test_share_with_no_buffering implementation
* Check limit bigger than 0 in AsyncShareSequence iterate
* Revert "Check limit bigger than 0 in AsyncShareSequence iterate"
This reverts commit 3c9e1ab0ff.
* Check limit bigger than 0 in iterate in AsyncShareSequence
* Hand correct the lint failures in TestShare.swift
* An additional hand-edit lint fix for TestShare.swift
---------
Co-authored-by: Philippe Hausler <phausler@apple.com>
This commit is contained in:
1 parent
ba540240d3
commit
2de50036f6
2 files changed
+50
-1
No files matched your search
@@ -423,7 +423,7 @@ where Base.Element: Sendable, Base: SendableMetatype, Base.AsyncIterator: Sendab
|
||||
}
|
||||
|
||||
func iterate() async -> Bool {
|
||||
if let limit {
|
||||
if let limit, limit > 0 {
|
||||
let cancelled = await withUnsafeContinuation { (continuation: UnsafeContinuation<Bool, Never>) in
|
||||
let (resume, cancelled) = state.withLock { state -> (UnsafeContinuation<Bool, Never>?, Bool) in
|
||||
guard state.buffer.count >= limit else {
|
||||
|
||||
@@ -130,6 +130,55 @@ final class TestShare: XCTestCase {
|
||||
XCTAssertEqual(results2.withLock { $0 }.sorted(), [1, 2, 3, 4, 5])
|
||||
}
|
||||
|
||||
func test_share_with_no_buffering() async {
|
||||
let shared = [1, 2, 3, 4, 5].async.share(bufferingPolicy: .bounded(0))
|
||||
|
||||
let expectation1 = XCTestExpectation(description: "Consumer 1 finished looping")
|
||||
let expectation2 = XCTestExpectation(description: "Consumer 2 finished looping")
|
||||
|
||||
let results1 = Mutex([Int]())
|
||||
let results2 = Mutex([Int]())
|
||||
let gate1 = Gate()
|
||||
let gate2 = Gate()
|
||||
|
||||
let consumer1 = Task {
|
||||
var iterator = shared.makeAsyncIterator()
|
||||
gate2.open()
|
||||
await gate1.enter()
|
||||
while let value = await iterator.next(isolation: nil) {
|
||||
results1.withLock { $0.append(value) }
|
||||
// Add some delay to consumer 1
|
||||
try? await Task.sleep(for: .milliseconds(1))
|
||||
}
|
||||
expectation1.fulfill()
|
||||
}
|
||||
|
||||
let consumer2 = Task {
|
||||
var iterator = shared.makeAsyncIterator()
|
||||
gate1.open()
|
||||
await gate2.enter()
|
||||
while let value = await iterator.next(isolation: nil) {
|
||||
results2.withLock { $0.append(value) }
|
||||
}
|
||||
expectation2.fulfill()
|
||||
}
|
||||
|
||||
await fulfillment(of: [expectation1, expectation2], timeout: 5)
|
||||
|
||||
await consumer1.value
|
||||
await consumer2.value
|
||||
|
||||
// Both consumers should receive all elements
|
||||
XCTAssertEqual(results1.withLock { $0 }.sorted(), [1, 2, 3, 4, 5])
|
||||
XCTAssertEqual(results2.withLock { $0 }.sorted(), [1, 2, 3, 4, 5])
|
||||
}
|
||||
|
||||
func test_share_with_no_buffering_multiple() async {
|
||||
for _ in 0..<10 {
|
||||
await test_share_with_no_buffering()
|
||||
}
|
||||
}
|
||||
|
||||
func test_share_with_unbounded_buffering() async {
|
||||
let source = [1, 2, 3, 4, 5]
|
||||
let shared = source.async.share(bufferingPolicy: .unbounded)
|
||||
|
||||
Reference in new issue
Block a user