Skip to content

Commit 020dfd3

Browse files
committed
Fix Linux test concurrency build issues.
Signed-off-by: Ross Goldberg <[email protected]>
1 parent 5c3d780 commit 020dfd3

File tree

1 file changed

+36
-6
lines changed

1 file changed

+36
-6
lines changed

Sources/ArgumentParser/Parsing/CommandParser.swift

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,12 @@
1010
//===----------------------------------------------------------------------===//
1111

1212
#if swift(>=6.0)
13-
private import class Dispatch.DispatchSemaphore
13+
@preconcurrency private import class Dispatch.DispatchSemaphore
14+
internal import class Foundation.NSLock
1415
internal import class Foundation.ProcessInfo
1516
#else
16-
import class Dispatch.DispatchSemaphore
17+
@preconcurrency import class Dispatch.DispatchSemaphore
18+
import class Foundation.NSLock
1719
import class Foundation.ProcessInfo
1820
#endif
1921

@@ -518,16 +520,44 @@ private func asyncCustomCompletions(
518520
) throws -> [String] {
519521
let (args, completingArgumentIndex, completingPrefix) =
520522
try parseCustomCompletionArguments(from: args)
521-
var completions: [String] = []
523+
524+
let completionsBox = SendableBox<[String]>([])
522525
let semaphore = DispatchSemaphore(value: 0)
526+
523527
Task {
524-
completions = await complete(
525-
args, completingArgumentIndex, completingPrefix
528+
completionsBox.value = await complete(
529+
args,
530+
completingArgumentIndex,
531+
completingPrefix
526532
)
527533
semaphore.signal()
528534
}
535+
529536
semaphore.wait()
530-
return completions
537+
return completionsBox.value
538+
}
539+
540+
// Helper class to make values sendable across concurrency boundaries
541+
private final class SendableBox<T>: @unchecked Sendable {
542+
private let lock = NSLock()
543+
private var _value: T
544+
545+
init(_ value: T) {
546+
self._value = value
547+
}
548+
549+
var value: T {
550+
get {
551+
lock.lock()
552+
defer { lock.unlock() }
553+
return _value
554+
}
555+
set {
556+
lock.lock()
557+
defer { lock.unlock() }
558+
_value = newValue
559+
}
560+
}
531561
}
532562

533563
// MARK: Building Command Stacks

0 commit comments

Comments
 (0)