# Rocket.jl - why is teardown logic never called?

**URL:** <https://discourse.julialang.org/t/rocket-jl-why-is-teardown-logic-never-called/123418>\
**Category:** General Usage\
**Tags:** reactiveprogramming, rocket\
**Created:** [December 3, 2024, 4:58pm UTC](https://discourse.julialang.org/t/rocket-jl-why-is-teardown-logic-never-called/123418 "2024-12-03T16:58:40Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![AUK1939](https://avatars.discourse-cdn.com/v4/letter/a/c57346/32.png) [@AUK1939](https://discourse.julialang.org/u/AUK1939)\
**Post date:** [December 3, 2024, 4:58pm UTC](https://discourse.julialang.org/t/rocket-jl-why-is-teardown-logic-never-called/123418/1 "2024-12-03T16:58:40Z")

</div>

Here is an example

```julia
struct MySubscribable <: Subscribable{Int} end

function Rocket.on_subscribe!(subscribable::MySubscribable, actor)
    next!(actor, 1)
    next!(actor, 2)
    next!(actor, 3)
    complete!(actor)
    return MyCustomSubscription()
end

struct MyCustomSubscription <: Teardown
    # some fields here
end

Rocket.as_teardown(::Type{<:MyCustomSubscription}) = UnsubscribableTeardownLogic()

function Rocket.on_unsubscribe!(subscription::MyCustomSubscription)
    # dispose resources here
    println("Unsubscribed!")
end

observable = MySubscribable()
my_subscription = subscribe!(observable, logger())

# this will call the on_unsubscribe! method
unsubscribe!(my_subscription)

```

However if I multicast the observable then the teardown logic is never called

```julia
subject = Subject(Int)
observable = MySubscribable() |> multicast(subject)
my_subscription = subscribe!(observable, logger())
connect(observable)

# teardown logic is not called here - why??
unsubscribe!(my_subscription)

```

Is there any way to force the correct teardown logic to be called when the observable is multicast?

---

<div class="post-metadata">

**Author:** ![g-gundam](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/g-gundam/32/47593_2.png) [@g-gundam](https://discourse.julialang.org/u/g-gundam)\
**Post date:** [December 20, 2024, 6:55am UTC](https://discourse.julialang.org/t/rocket-jl-why-is-teardown-logic-never-called/123418/2 "2024-12-20T06:55:48Z")

</div>

This might not be the most satisfying answer, but the types for `observable` and `my_subscription` are different. In your first example:

```julia-repl
julia> typeof(observable)
MySubscribable

julia> typeof(my_subscription)
MyCustomSubscription

```

In your second example:

```julia-repl
julia> typeof(observable)
ConnectableObservable{Int64, Subject{Int64, AsapScheduler, AsapScheduler}, MySubscribable}

julia> typeof(my_subscription)
Rocket.SubjectSubscription

```

As for why unsubscribing from the multicasted observable doesn’t trickle up to unsubscribe for MyCustomSubscription, I don’t know. Perhaps it’s not a safe assumption they can make.
