@@ -576,7 +576,7 @@ internal struct LambdaHTTPServer {
576576
577577 enum State : ~ Copyable {
578578 case buffer( Deque < T > )
579- case continuation( CheckedContinuation < T , any Error > ? )
579+ case continuation( CheckedContinuation < T , any Error > )
580580 }
581581
582582 private let lock = Mutex < State > ( . buffer( [ ] ) )
@@ -610,29 +610,34 @@ internal struct LambdaHTTPServer {
610610
611611 return try await withTaskCancellationHandler {
612612 try await withCheckedThrowingContinuation { ( continuation: CheckedContinuation < T , any Error > ) in
613- let ( nextAction, nextError ) = self . lock. withLock { state -> ( T ? , PoolError ? ) in
613+ let nextAction : Result < T , PoolError > ? = self . lock. withLock { state -> Result < T , PoolError > ? in
614614 switch consume state {
615615 case . buffer( var buffer) :
616616 if let first = buffer. popFirst ( ) {
617617 state = . buffer( buffer)
618- return ( first, nil )
618+ return . success ( first)
619619 } else {
620620 state = . continuation( continuation)
621- return ( nil , nil )
621+ return nil
622622 }
623623
624624 case . continuation( let previousContinuation) :
625625 state = . buffer( [ ] )
626- return ( nil , PoolError ( cause: . nextCalledTwice( [ previousContinuation, continuation ] ) ) )
626+ return . failure ( PoolError ( cause: . nextCalledTwice( previousContinuation) ) )
627627 }
628628 }
629629
630- if let nextError,
631- case let . nextCalledTwice( continuations) = nextError. cause
632- {
633- for continuation in continuations { continuation? . resume ( throwing: nextError) }
634- } else if let nextAction {
635- continuation. resume ( returning: nextAction)
630+ switch nextAction {
631+ case . success( let action) :
632+ continuation. resume ( returning: action)
633+ case . failure( let error) :
634+ if case let . nextCalledTwice( prevContinuation) = error. cause {
635+ prevContinuation. resume ( throwing: error)
636+ }
637+ continuation. resume ( throwing: error)
638+ case . none:
639+ // do nothing
640+ break
636641 }
637642 }
638643 } onCancel: {
@@ -642,7 +647,7 @@ internal struct LambdaHTTPServer {
642647 state = . buffer( buffer)
643648 case . continuation( let continuation) :
644649 state = . buffer( [ ] )
645- continuation? . resume ( throwing: CancellationError ( ) )
650+ continuation. resume ( throwing: CancellationError ( ) )
646651 }
647652 }
648653 }
@@ -662,7 +667,7 @@ internal struct LambdaHTTPServer {
662667 }
663668
664669 enum Cause {
665- case nextCalledTwice( [ CheckedContinuation < T , any Error > ? ] )
670+ case nextCalledTwice( CheckedContinuation < T , any Error > )
666671 }
667672 }
668673 }
0 commit comments