Skip to content

Commit cd49def

Browse files
committed
[Distributed] fix deinitialization of result buffer
1 parent ddfddcd commit cd49def

File tree

1 file changed

+7
-3
lines changed

1 file changed

+7
-3
lines changed

stdlib/public/Distributed/DistributedActorSystem.swift

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -531,7 +531,7 @@ extension DistributedActorSystem {
531531
}
532532

533533
// Decode the return type
534-
func allocateReturnTypeBuffer<R>(_: R.Type) -> UnsafeMutableRawPointer? {
534+
func doAllocateReturnTypeBuffer<R>(_: R.Type) -> UnsafeMutableRawPointer? {
535535
return UnsafeMutableRawPointer(UnsafeMutablePointer<R>.allocate(capacity: 1))
536536
}
537537

@@ -549,15 +549,18 @@ extension DistributedActorSystem {
549549
errorCode: .typeDeserializationFailure)
550550
}
551551

552-
guard let resultBuffer = _openExistential(returnTypeFromTypeInfo, do: allocateReturnTypeBuffer) else {
552+
guard let resultBuffer = _openExistential(returnTypeFromTypeInfo, do: doAllocateReturnTypeBuffer) else {
553553
throw ExecuteDistributedTargetError(
554554
message: "Failed to allocate buffer for distributed target return type",
555555
errorCode: .typeDeserializationFailure)
556556
}
557557

558+
var executeDistributedTargetHasThrown = false
558559
func doDestroyReturnTypeBuffer<R>(_: R.Type) {
559560
let buf = resultBuffer.assumingMemoryBound(to: R.self)
560-
buf.deinitialize(count: 1)
561+
if !executeDistributedTargetHasThrown {
562+
buf.deinitialize(count: 1)
563+
}
561564
buf.deallocate()
562565
}
563566

@@ -592,6 +595,7 @@ extension DistributedActorSystem {
592595
)
593596
}
594597
} catch {
598+
executeDistributedTargetHasThrown = true
595599
try await handler.onThrow(error: error)
596600
}
597601
}

0 commit comments

Comments
 (0)