Skip to content

Commit e8c25a4

Browse files
committed
[Distributed] fix deinitialization of result buffer
1 parent cdd8fe9 commit e8c25a4

File tree

1 file changed

+12
-6
lines changed

1 file changed

+12
-6
lines changed

stdlib/public/Distributed/DistributedActorSystem.swift

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

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

538538
let maybeReturnTypeFromTypeInfo =
@@ -549,18 +549,23 @@ 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-
func destroyReturnTypeBuffer<R>(_: R.Type) {
559-
resultBuffer.assumingMemoryBound(to: R.self).deallocate()
558+
var executeDistributedTargetHasThrown = false
559+
func doDestroyReturnTypeBuffer<R>(_: R.Type) {
560+
let buf = resultBuffer.assumingMemoryBound(to: R.self)
561+
if !executeDistributedTargetHasThrown {
562+
buf.deinitialize(count: 1)
563+
}
564+
buf.deallocate()
560565
}
561566

562567
defer {
563-
_openExistential(returnTypeFromTypeInfo, do: destroyReturnTypeBuffer)
568+
_openExistential(returnTypeFromTypeInfo, do: doDestroyReturnTypeBuffer)
564569
}
565570

566571
do {
@@ -590,6 +595,7 @@ extension DistributedActorSystem {
590595
)
591596
}
592597
} catch {
598+
executeDistributedTargetHasThrown = true
593599
try await handler.onThrow(error: error)
594600
}
595601
}

0 commit comments

Comments
 (0)