From 2de50036f67c848aac927964bdad26448fbdd218 Mon Sep 17 00:00:00 2001 From: Alessio Nossa Date: Mon, 26 Jan 2026 18:29:41 +0100 Subject: [PATCH] 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 3c9e1ab0ffee3dfc9680c5120ba3035a439939ca. * 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 --- .../AsyncAlgorithms/AsyncShareSequence.swift | 2 +- Tests/AsyncAlgorithmsTests/TestShare.swift | 49 +++++++++++++++++++ 2 files changed, 50 insertions(+), 1 deletion(-) diff --git a/Sources/AsyncAlgorithms/AsyncShareSequence.swift b/Sources/AsyncAlgorithms/AsyncShareSequence.swift index ebe3492..253edf9 100644 --- a/Sources/AsyncAlgorithms/AsyncShareSequence.swift +++ b/Sources/AsyncAlgorithms/AsyncShareSequence.swift @@ -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) in let (resume, cancelled) = state.withLock { state -> (UnsafeContinuation?, Bool) in guard state.buffer.count >= limit else { diff --git a/Tests/AsyncAlgorithmsTests/TestShare.swift b/Tests/AsyncAlgorithmsTests/TestShare.swift index ac81753..35dad3a 100644 --- a/Tests/AsyncAlgorithmsTests/TestShare.swift +++ b/Tests/AsyncAlgorithmsTests/TestShare.swift @@ -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)