# Remote target arguments not deinitialised

**URL:** <https://forums.swift.org/t/remote-target-arguments-not-deinitialised/64838>\
**Category:** Distributed Actors\
**Created:** [May 5, 2023, 2:59pm UTC](https://forums.swift.org/t/remote-target-arguments-not-deinitialised/64838 "2023-05-05T14:59:28Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![ser-0xff](https://avatars.discourse-cdn.com/v4/letter/s/e9c0ed/32.png) [@ser-0xff](https://forums.swift.org/u/ser-0xff)\
**Post date:** [May 5, 2023, 2:59pm UTC](https://forums.swift.org/t/remote-target-arguments-not-deinitialised/64838/1 "2023-05-05T14:59:28Z")

</div>

Hi  
Recently we found the strange behaviour with remote target arguments that are decoded with decodeNextArgument\<\>()  
In a few words the argument is a struct holding some references to the class objects, like below

```swift
class ClassValue {
    init() { print("ClassValue.init") }
    deinit() { print("ClassValue.deinit") }
}

struct ArgumentType {
    let classValue: ClassValue
    init(_ classValue: ClassValue) { self.classValue = classValue }
}

```

the simplified invocation decoder decodeNextArgument\<\> looks like:

```swift
public func decodeNextArgument<Argument>() throws -> Argument {
    let classValue = ClassValue()
    return ArgumentType(classValue)
}

```

We observe the class value initialiser is called each time we perform a call for distributed actor (we see "ClassValue.init" printed out). We would expect to see "ClassValue.deinit" as well, but actually never see it, what can be treated the argument is not properly deinitialised.

Does anybody know is it expected behaviour or not?  
May be we are missing something or doing something wrong.

Thanks in advance!

---

<div class="post-metadata">

**Author:** ![ser-0xff](https://avatars.discourse-cdn.com/v4/letter/s/e9c0ed/32.png) [@ser-0xff](https://forums.swift.org/u/ser-0xff)\
**Post date:** [May 9, 2023, 12:39pm UTC](https://forums.swift.org/t/remote-target-arguments-not-deinitialised/64838/2 "2023-05-09T12:39:07Z")

</div>

We implemented a minimised test reproducing an issue: [external-reproducers/swift/distributed-system-leak at main · ordo-one/external-reproducers · GitHub](https://github.com/ordo-one/external-reproducers/tree/main/swift/distributed-system-leak)  
Run 'swift test', will see the ClassValue initialiser is being called twice, but deinitialiser only once.

---

<div class="post-metadata">

**Author:** ![ktoso](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/ktoso/32/44526_2.png) [@ktoso](https://forums.swift.org/u/ktoso)\
**Post date:** [May 9, 2023, 2:00pm UTC](https://forums.swift.org/t/remote-target-arguments-not-deinitialised/64838/3 "2023-05-09T14:00:21Z")

</div>

Thanks a lot for reporting, could you please file an issue with that repro on [GitHub - apple/swift: The Swift Programming Language](http://github.com/apple/swift) and I'll look to it as soon as I can? It's pretty unexpected if that were to happen but who knows... I'll make sure (and have been adding "make sure we don't leak" tests all over the place recently anyway, so that fits that focus I have now).

---

<div class="post-metadata">

**Author:** ![ser-0xff](https://avatars.discourse-cdn.com/v4/letter/s/e9c0ed/32.png) [@ser-0xff](https://forums.swift.org/u/ser-0xff)\
**Post date:** [May 11, 2023, 8:47am UTC](https://forums.swift.org/t/remote-target-arguments-not-deinitialised/64838/4 "2023-05-11T08:47:54Z")

</div>

> <https://github.com/apple/swift/issues/65853>
>
> \<!--
> This repository tracks issues related to the implementation of the Swift
> …compiler, standard library, runtime, and tools that provide IDE support for
> Swift (e.g. code completion). If the bug relates to the implementation of a
> proprietary (closed-source) Apple framework such as UIKit, SwiftUI, Combine,
> etc., please report it to https://feedbackassistant.apple.com instead.
> \--\>
> 
> Remote target arguments reconstructed on the remote side of the distributed actor are not properly deinitialised.
> If the remote target argument is a struct with some class instance references then those class instances are never released, which cause a memory leak.
> 
> 
> 
> We implemented minimised \[test\](https://github.com/ordo-one/external-reproducers/tree/main/swift/distributed-system-leak)
> It perform a remote call on the distributed actor with a struct argument having a reference to the ClassValue instance.
> Can find the ClassValue instance initialized twice (first time on the caller side, second time on the remote side), but reinitialised only once (on the caller side):
> \`\`\`
> Test Suite 'All tests' passed at 2023-05-11 11:40:58.103.
> Executed 1 test, with 0 failures (0 unexpected) in 0.003 (0.004) seconds
> assign\<TestActor\>: 1
> actorReady\<TestActor\>: 1
> resolve\<TestActor\>
> init: 0x000060000300c300
> recordArgument
> remoteCallVoid
> init: 0x0000600003008740
> value=100
> deinit: 0x000060000300c300
> \`\`\`
> 
> \<!--
> Explain how to reproduce the problem (in steps if seen fit) and include either
> an inline test case (preferred) or a project that reproduces it. Consider
> reducing the sample to the smallest amount of code possible — a smaller test
> case is easier to reason about and more appealing to сontributors.
> \--\>
> 
> We would expect the second ClassValue instance constructed on the caller side would be deinitialised.
> 
> 
> 
> 
> 
> 
> \<!--
> Include information about the Swift compiler version and, if applicable, the
> Xcode version you are observing the problem in and the deployment target.
> \--\>
> \*\*Environment\*\*
> \- Swift compiler version info
> \`\`\`
> distributed-system-leak % swift --version
> swift-driver version: 1.62.15 Apple Swift version 5.7.2 (swiftlang-5.7.2.135.5 clang-1400.0.29.51)
> Target: arm64-apple-macosx13.0
> \`\`\`
> \- Xcode version info 
> \- Deployment target:
> \`\`\`
> distributed-system-leak % uname -a
> Darwin mbpro.local 22.3.0 Darwin Kernel Version 22.3.0: Mon Jan 30 20:38:37 PST 2023; root:xnu-8792.81.3~2/RELEASE\_ARM64\_T6000 arm64
> \`\`\`

---

<div class="post-metadata">

**Author:** ![ktoso](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/ktoso/32/44526_2.png) [@ktoso](https://forums.swift.org/u/ktoso)\
**Post date:** [May 11, 2023, 9:03am UTC](https://forums.swift.org/t/remote-target-arguments-not-deinitialised/64838/5 "2023-05-11T09:03:55Z")

</div>

Thank you, I’ll have a look

---

<div class="post-metadata">

**Author:** ![ktoso](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/ktoso/32/44526_2.png) [@ktoso](https://forums.swift.org/u/ktoso)\
**Post date:** [May 16, 2023, 8:16am UTC](https://forums.swift.org/t/remote-target-arguments-not-deinitialised/64838/6 "2023-05-16T08:16:41Z")

</div>

Closing the loop here as well, we can confirm this was indeed leaking and remained unnoticed since a while. The fix is here: [[IRGen] Distributed: Destroy loaded arguments after the call by xedin · Pull Request #65874 · apple/swift · GitHub](https://github.com/apple/swift/pull/65874)
