Hi all.
I am using Asterisk 20.1.0 in a production environment. I am running into an issue that appears to be related to Stasis devicestate topics and ao2 reference counting, and I was hoping you might have some insight or suggestions.
In my scenario, calls are initiated from an external application via ARI using the originate operation. After the call is created, it is passed into a Stasis application, where the business logic is implemented. This Stasis application uses AudioSocket as part of the media handling.
After each call, when I run stasis show topics, I see a new entry like:
devicestate:all/AudioSocket/MP2D66QC:62330-98f1f148-0714-4384-b1ec
These entries keep accumulating and never seem to disappear. Once the number of such topics reaches around 100,000, Asterisk starts logging messages of the form:
FRACK!, Failed assertion Excessive refcount 100000 reached on ao2 object
From my understanding of astobj2 and the Stasis code in main/stasis.c, this suggests that there is a reference counting leak related to these devicestate topics or their subscriptions. Over time, this looks like it could lead to instability or incorrect behavior of the system, especially under high load.
I have tested this behavior not only on Asterisk 20.1.0 but also after upgrading to Asterisk 22.5.0, and the problem still occurs in the same way (the devicestate topics continue to accumulate and eventually trigger the excessive refcount assertion).
Right now, I am investigating how to access and inspect the underlying ao2 containers (for topics and subscriptions) from a custom module, so that I can see exactly what is being kept alive and why. The idea is to build a module that iterates over the relevant Stasis topics (those matching the devicestate:all/AudioSocket/... pattern), inspects their subscribers and reference counts, and helps identify what is leaking.
My questions are:
-
Have you seen this pattern before with devicestate topics (especially in combination with ARI‑initiated calls, Stasis applications, and AudioSocket) where
stasis show topicsgrows without bound and eventually hits theExcessive refcount 100000assertion? -
Do you know of any existing fixes, configuration changes, or best practices that prevent these devicestate topics from accumulating (for example, known patches, options to avoid per‑call topic creation, or recommended unsubscribe/cleanup patterns)?
-
From your perspective, is the approach of writing a dedicated inspection/cleanup module to work with the Stasis topic/subscription containers reasonable, or would you recommend a different direction (e.g., changing how the ARI application uses
originateand Stasis, or integrating with existing Stasis APIs in a specific way)?
If you have any pointers to relevant commits, issues, or documentation, or any high‑level guidance on how you would approach this, that would be extremely helpful.