@@ -590,44 +590,44 @@ where Base.Element: Sendable, Base: _SendableMetatype, Base.AsyncIterator: _Send
590590 }
591591
592592 func next( isolation actor : isolated ( any Actor ) ? , id: Int ) async throws -> Base . Element ? {
593- let iteratingTask = state. withLock { state -> IteratingTask in
594- defer {
595- if case . pending = state. iteratingTask {
596- state. iteratingTask = . starting
597- }
593+ let iteratingTask = state. withLock { state -> IteratingTask in
594+ defer {
595+ if case . pending = state. iteratingTask {
596+ state. iteratingTask = . starting
598597 }
599- return state. iteratingTask
600598 }
601-
602- if case . cancelled = iteratingTask { return nil }
603-
604- if case . pending( let factory) = iteratingTask {
605- let task : Task < Void , Never >
606- // for the fancy dance of availability and canImport see the comment on the next check for details
607- #if swift(>=6.2)
608- if #available( macOS 26 . 0 , iOS 26 . 0 , tvOS 26 . 0 , visionOS 26 . 0 , * ) {
609- task = Task ( name: " Share Iteration " ) { [ factory, self ] in
610- await iterationLoop ( factory: factory)
611- }
612- } else {
613- task = Task . detached ( name: " Share Iteration " ) { [ factory, self ] in
614- await iterationLoop ( factory: factory)
615- }
616- }
617- #else
618- task = Task . detached { [ factory, self ] in
619- await iterationLoop ( factory: factory)
620- }
621- #endif
622- // Known Issue: there is a very small race where the task may not get a priority escalation during startup
623- // this unfortuantely cannot be avoided since the task should ideally not be formed within the critical
624- // region of the state. Since that could lead to potential deadlocks in low-core-count systems.
625- // That window is relatively small and can be revisited if a suitable proof of safe behavior can be
626- // determined.
627- state. withLock { state in
628- precondition ( state. iteratingTask. isStarting)
629- state. iteratingTask = . running( task)
630- }
599+ return state. iteratingTask
600+ }
601+
602+ if case . cancelled = iteratingTask { return nil }
603+
604+ if case . pending( let factory) = iteratingTask {
605+ let task : Task < Void , Never >
606+ // for the fancy dance of availability and canImport see the comment on the next check for details
607+ #if swift(>=6.2)
608+ if #available( macOS 26 . 0 , iOS 26 . 0 , tvOS 26 . 0 , visionOS 26 . 0 , * ) {
609+ task = Task ( name: " Share Iteration " ) { [ factory, self ] in
610+ await iterationLoop ( factory: factory)
611+ }
612+ } else {
613+ task = Task . detached ( name: " Share Iteration " ) { [ factory, self ] in
614+ await iterationLoop ( factory: factory)
615+ }
616+ }
617+ #else
618+ task = Task . detached { [ factory, self ] in
619+ await iterationLoop ( factory: factory)
620+ }
621+ #endif
622+ // Known Issue: there is a very small race where the task may not get a priority escalation during startup
623+ // this unfortuantely cannot be avoided since the task should ideally not be formed within the critical
624+ // region of the state. Since that could lead to potential deadlocks in low-core-count systems.
625+ // That window is relatively small and can be revisited if a suitable proof of safe behavior can be
626+ // determined.
627+ state. withLock { state in
628+ precondition ( state. iteratingTask. isStarting)
629+ state. iteratingTask = . running( task)
630+ }
631631 }
632632
633633 // withTaskPriorityEscalationHandler is only available for the '26 releases and the 6.2 version of
@@ -656,7 +656,6 @@ where Base.Element: Sendable, Base: _SendableMetatype, Base.AsyncIterator: _Send
656656 #else
657657 return try await nextIteration ( id) . get ( )
658658 #endif
659-
660659 }
661660 }
662661
0 commit comments