Skip to content

Commit 605e86c

Browse files
author
Sebastien Stormacq
committed
onCancel: resume continuation outside of the lock
1 parent ae18d7b commit 605e86c

File tree

1 file changed

+9
-4
lines changed

1 file changed

+9
-4
lines changed

Sources/AWSLambdaRuntime/Lambda+LocalServer.swift

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -583,7 +583,6 @@ internal struct LambdaHTTPServer {
583583

584584
/// enqueue an element, or give it back immediately to the iterator if it is waiting for an element
585585
public func push(_ invocation: T) {
586-
587586
// if the iterator is waiting for an element on `next()``, give it to it
588587
// otherwise, enqueue the element
589588
let maybeContinuation = self.lock.withLock { state -> CheckedContinuation<T, any Error>? in
@@ -599,6 +598,7 @@ internal struct LambdaHTTPServer {
599598
}
600599
}
601600

601+
// Resume continuation outside the lock to prevent potential deadlocks
602602
maybeContinuation?.resume(returning: invocation)
603603
}
604604

@@ -636,20 +636,25 @@ internal struct LambdaHTTPServer {
636636
}
637637
continuation.resume(throwing: error)
638638
case .none:
639-
// do nothing
639+
// do nothing - continuation is stored in state
640640
break
641641
}
642642
}
643643
} onCancel: {
644-
self.lock.withLock { state in
644+
// Ensure we properly handle cancellation by checking if we have a stored continuation
645+
let continuationToCancel = self.lock.withLock { state -> CheckedContinuation<T, any Error>? in
645646
switch consume state {
646647
case .buffer(let buffer):
647648
state = .buffer(buffer)
649+
return nil
648650
case .continuation(let continuation):
649651
state = .buffer([])
650-
continuation.resume(throwing: CancellationError())
652+
return continuation
651653
}
652654
}
655+
656+
// Resume the continuation outside the lock to avoid potential deadlocks
657+
continuationToCancel?.resume(throwing: CancellationError())
653658
}
654659
}
655660

0 commit comments

Comments
 (0)