Repository: apple/swift-nio
File: Sources/NIOCore/AsyncSequences/NIOAsyncWriter.swift (Swift)

Code before:

                case .throwError(let error):
                    throw error

                case .suspendTask:
                    // Holding the lock here *should* be safe but because of a bug in the runtime
                    // it isn't, so drop the lock, create the continuation and then try again.
                    //
                    // See https://github.com/swiftlang/swift/issues/85668
                    //
                    // Dropping and reacquiring the lock may result in yields being reordered but
                    // only from the perspective of when this function was entered. For example:
                    //
                    // - T1 calls _yield
                    // - T2 calls _yield
                    // - T2 returns from _yield
                    // - T1 returns from _yield
                    //
                    // This is fine: the async writer doesn't offer any ordering guarantees for
                    // calls made from different threads.
                    //
                    // Within a thread there is no possibility of re-ordering as the call only
                    // returns once the write has been yielded.
                    //
                    // We switched to using NIOUnsafeContinuation after running with
                    // CheckedContinuatuion for more than a year. Now we use NIOUnsafeContinuation
                    // to reduce runtime costs.
                    return try await withNIOUnsafeThrowingContinuation {
                        (continuation: NIOUnsafeContinuation<StateMachine.YieldResult, Error>) in
                        let (action, didSuspend) = self._state.withLockedValue {
                            state -> (NIOAsyncWriter.StateMachine.YieldAction, (@Sendable () -> Void)?) in
                            let yieldAction = state.stateMachine.yield(yieldID: yieldID)
                            switch yieldAction {
                            case .callDidYield, .throwError:
                                return (yieldAction, nil)
                            case .suspendTask:
                                switch state.stateMachine.yield(
                                    continuation: continuation,
                                    yieldID: yieldID
                                ) {
                                case .cancelled:
                                    return (.throwError(CancellationError()), nil)
                                case .suspended:
                                    return (yieldAction, state.didSuspend)
                                }
                            }
                        }

                        switch action {
                        case .callDidYield(let delegate):
                            delegate.didYield(contentsOf: Deque(sequence))

Review comment (suggestion):
Normally I'm not that nitpicky about typos but this one got copypasta'd everywhere:
```suggestion
                    // CheckedContinuation for more than a year. Now we use NIOUnsafeContinuation
```

Code after:

                case .throwError(let error):
                    throw error

                case .suspendTask:
                    // Holding the lock here *should* be safe but because of a bug in the runtime
                    // it isn't, so drop the lock, create the continuation and then try again.
                    //
                    // See https://github.com/swiftlang/swift/issues/85668
                    //
                    // Dropping and reacquiring the lock may result in yields being reordered but
                    // only from the perspective of when this function was entered. For example:
                    //
                    // - T1 calls _yield
                    // - T2 calls _yield
                    // - T2 returns from _yield
                    // - T1 returns from _yield
                    //
                    // This is fine: the async writer doesn't offer any ordering guarantees for
                    // calls made from different threads.
                    //
                    // Within a thread there is no possibility of re-ordering as the call only
                    // returns once the write has been yielded.
                    //
                    // We switched to using NIOUnsafeContinuation after running with
                    // CheckedContinuation for more than a year. Now we use NIOUnsafeContinuation
                    // to reduce runtime costs.
                    return try await withNIOUnsafeThrowingContinuation {
                        (continuation: NIOUnsafeContinuation<StateMachine.YieldResult, Error>) in
                        let (action, didSuspend) = self._state.withLockedValue {
                            state -> (NIOAsyncWriter.StateMachine.YieldAction, (@Sendable () -> Void)?) in
                            let yieldAction = state.stateMachine.yield(yieldID: yieldID)
                            switch yieldAction {
                            case .callDidYield, .throwError:
                                return (yieldAction, nil)
                            case .suspendTask:
                                switch state.stateMachine.yield(
                                    continuation: continuation,
                                    yieldID: yieldID
                                ) {
                                case .cancelled:
                                    return (.throwError(CancellationError()), nil)
                                case .suspended:
                                    return (yieldAction, state.didSuspend)
                                }
                            }
                        }

                        switch action {
                        case .callDidYield(let delegate):
                            delegate.didYield(contentsOf: Deque(sequence))