Asterisk users:
I finally found the time to replicate the issue with Asterisk segfaulting in res_speech with "Object is likely destroyed" message.
Scenario
The problem was actually no that hard to reproduce and the behavior is very consistent. You need to use a call generator placing one call at a time hitting dialplan which makes use of Generic Speech API of Asterisk. After about hundred calls being placed (the number may differ and do not actually matter), Asterisk would crash in the same manner.
Root Cause
The backtrace clearly points to the function ast_speech_new() in res_speech.c. However, the problem is coming from the function which invokes ast_speech_new() such as the function speech_create() in app_speech_utils.c.
It looks like the second parameter to asp_speech_new() is supposed to be allocated using ast_format_cap_alloc, whereas an existing object is referenced by ast_channel_nativeformats(chan) instead.
This is purely a problem of Asterisk, external modules using res_speech API cannot have any influence or impact
on the exhibition of the problem.
Affected Versions
This issue was first introduced in Asterisk 13, where ast_format_cap came into the place, and all the upstream versions seem to be affected.
How To Address
The workaround is simple enough. You may use the attached patch, which is made for Asterisk 13.11.2. Since the file ast_speech_utils.c has not been changed significantly, it should be straightforward to apply the same patch to all the upstream versions. Here is the diff to consider
+#include "asterisk/format_cache.h"
+ struct ast_format_cap *cap;
+ if (!(cap = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT))) {
+ pbx_builtin_setvar_helper(chan, "ERROR", "1");
+ return 0;
+ }
+ ast_format_cap_append(cap, ast_format_slin, 0);
+
/* Request a speech object */
- speech = ast_speech_new(data, ast_channel_nativeformats(chan));
+ speech = ast_speech_new(data, cap);
Summary
The patched version has been running for a while having processed thousands of calls without any issues. Those of you experiencing the same problem: please apply the patch and post back the results. This will need to be reported to Asterisk as well. Someone please take care of that. It is not clear when I'll have the time to get back to this topic next time.