Relation-broken hook not running in peers relation

I’m building a charm that supports clustering through the use of a peer relation called ha.
Adding new units works as expected but when removing one unit ha-relation-broken is never run. ha-relation-departed is properly called on all units of the relation (although it is called more than I’d expect on the departing machine) as well as the stop hook but no ha-relation-broken.

I’m using reactive and the flag isn’t set either. Does this thread still hold true today ?

I essentially need to run something only on the departing unit before the machine is shutdown and I do not need the relation at that point. Any idea on the issue and/or workaround ?

juju: 2.8.0-focal-amd64
charmstore-client 2.4.0+snap-461+git-13-547c6f2
charm-tools 2.7.4

Yes, the linked thread still holds true, as it is fundamental to the meaning of the relation broken hook, i.e., that the relation itself has been destroyed. For a peer relation, the relation itself will never be destroyed until the application is removed and no units remain, at which point there won’t be anything to call the relation-broken hook on anyway.

Regarding the ha-relation-departed hook being called multiple times on the departing unit, from the perspective of that unit it is actually all of the other units which are “leaving” the relation with it, so it gets the hook called for every other unit.

Determining whether the local unit is the one leaving a relation has been a longstanding problem but it seems that Juju 2.8 actually added a JUJU_DEPARTING_UNIT hook environment variable which you can compare to JUJU_UNIT_NAME to see if you’re running on the unit which is in fact leaving. However, if you don’t need access to any of the relation info anyway, the stop hook is a reasonable place to do your cleanup as well, although I believe there is some discussion about changing the semantics of the stop hook to allow for temporary suspension of a unit; presumably in that case another hook would be created to replace the “shutting down for the last time” semantic.

Juju 2.8 introduced the stop hook. It’s called before the unit is shut down. That should work as a workaround.

Juju 2.8 actually added a JUJU_DEPARTING_UNIT

Exactly what I needed, many thanks.

On another somewhat unrelated note, our charmed application relies on many relations to function. If one of those essential relations is gone it cannot run at all. This becomes a problem when we need to run a command when departing as some other relations may be gone already, preventing our app to work. I couldn’t find a way to run hooks before departing all relations. Ideally I would need a sort of *-relation-departing (with a better name ofc) that would run before all the *-relation_departed hooks.
This would give the charm a chance to interact with the application while it’s still in a stable state.

Is there something similar that Juju already provides ?