Asterisk Timing Instability Root-Caused to pollmailboxes=yes with ODBC

I finally found the root cause of an Asterisk timing issue that looked like a Linux/kernel/timer problem — but it was not.

The symptom was that Asterisk timing would go off. At first, it looked like something low-level: kernel scheduler, VM timing, timerfd, CPU power states, realtime priority, or logging overhead.

I went through a long list of tests before finding the real cause:

  1. Updated the kernel.

  2. Tried moving to a realtime kernel.

  3. Tested different Asterisk timing sources.

  4. Changed Intel C-state / power-state settings.

  5. Tried pinning Asterisk to specific CPU cores.

  6. Ran Asterisk with realtime scheduling.

  7. Reduced/stopped Asterisk logging to rule out log I/O delays.

None of those solved it.

Then I tested the timing source itself outside of Asterisk. My standalone timerfd test was configured for 100 Hz, and it was stable:

ticks=100 in 1000ms (expected 100)

That did not mean Asterisk should also show 100 ticks. It only proved that the Linux timer source could deliver the rate requested by the standalone test.

Inside Asterisk, the timing test/debug path I was watching expected 50 ticks per second. When healthy, Asterisk showed:

It has been 1000 milliseconds, and we got 50 timer ticks

But when the problem happened, Asterisk showed missed/late tick totals:

It has been 1006 milliseconds, and we got 43 timer ticks
It has been 1000 milliseconds, and we got 44 timer ticks
It has been 1000 milliseconds, and we got 48 timer ticks

I also saw debug from the timerfd acknowledge path:

Expected to acknowledge 1 ticks but got 2 instead
Expected to acknowledge 1 ticks but got 3 instead

That was an important clue. It did not look like timerfd stopped firing. It looked like Asterisk was sometimes late getting back to read/acknowledge the timerfd, so multiple timer expirations had accumulated by the time Asterisk serviced it.

This matched the tick deficit above. If Asterisk is late acknowledging a few timerfd expirations during the one-second window, the higher-level result can show 43–48 ticks instead of the expected 50. The ticks were not simply vanishing; Asterisk was not servicing them promptly.

So the issue was not that Asterisk showed 50 while the standalone test showed 100. Those were different expected rates. The issue was that Asterisk sometimes expected 50 ticks and only received 43, 44, or 48, and sometimes expected to acknowledge 1 timer expiration but found 2 or 3 had already accumulated.

That shifted the investigation away from “bad kernel timer” and toward “what is Asterisk doing internally during the bad window?”

At that point I compiled Asterisk with lock debugging enabled and ran:

core show locks

during the bad timing window.

The strongest single sample was not just that voicemail-related locks existed. It showed a thread actually waiting in the stasis state path while the call path came from voicemail/MWI publishing:

Waiting for Lock ... stasis_state.c ... __state_find_or_add manager->states
ast_mwi_publish_by_mailbox
ast_publish_mwi_state_full
app_voicemail_odbc.so

Other samples during the same bad windows repeatedly showed:

mb_poll_thread
ast_mwi_state_callback_subscribed
internal_ao2_traverse
res_pjsip_mwi

So I did not conclude that the Linux timer itself was bad, and I also did not claim that I had traced the exact timer thread being blocked. The evidence showed that Asterisk was doing periodic voicemail/MWI/stasis work during the same windows where timing ticks were late.

The setting that caused that periodic work was:

pollmailboxes=yes

with:

pollfreq=30

That means every 30 seconds Asterisk was polling voicemail mailboxes, walking MWI subscriptions, checking voicemail counts through ODBC, and publishing MWI updates.

After changing:

pollmailboxes=yes

to:

pollmailboxes=no

the timing immediately became stable.

The main lesson: not every Asterisk timing problem is really a kernel, VM, or timer-source problem.

The troubleshooting path that finally helped was:

  1. Prove the OS timer source works outside Asterisk.

  2. Compare that with what Asterisk expects internally.

  3. Capture the bad window, not just the healthy state.

  4. Look for late timer acknowledgements, not only total tick counts.

  5. Compile with lock debugging if needed.

  6. Run core show locks while timing is actually bad.

  7. Look for repeated internal work that correlates with the missed ticks.

In my case, the hidden cause was voicemail polling and MWI updates with ODBC not the kernel.

  1. What are the side effects/downsides of turning off polling?

  2. Would throwing more powerful iron at it (faster CPU, more cores, faster disk pack, etc.) fix the problem also?

Ted

  1. the issue with timing is always downgraded audio for playbacks and MoH …
  2. the issue is not cpu or compute it is a lock held in the wrong way .a engineering bug in the code .
  1. @TedM It means you can’t externally manipulate the mailbox and have MWI update - if this isn’t needed then nothing changes because for mailbox updates (a voicemail being left, deleted, etc) in Asterisk it is event based and doesn’t poll
  2. @benphone Regarding an engineering bug and lock held in the wrong way - that’s an assumption based on your analysis which is high level, the locking involved in polling doesn’t lock or touch active channels or timers

look how it locks i do not know did not investigate it deeply

but 1 thing i showed it truly harmed the timing of asterisk to bad levels .

since for me setting pollmailboxes=no solved the issue

i am for now continuing on .

Posting this mainly so the next person doesn’t spend as long chasing kernel/hardware theories as I did. And for anyone on the Asterisk dev side — worth knowing that “timing instability” reports aren’t always a clock-source problem. This one was 100% internal to Asterisk.

Wait a minute. This kind of problem ALWAYS happens when interfacing a computer to the Real World. In the Real World you have things that happen that take varying amounts of time - polling for example - the amount of time a poll would take would be dependent on how much stuff is being polled, how many extensions, and how long it takes for the computer to look at things. And you have a timer that is coming in from the real world at regular intervals. If by the nature of what the poll is doing the timer tick have to be locked out - then the only choice is to reduce the length of the code path used by the poll - or increase CPU power to make the code that’s being executed, execute faster - or if the code is blocking waiting for something in the real world to happen - like a disk head to move to a certain spot - then you replace the disk with a faster one - or something like that.

There’s no way that you can convince me that anything a computer does that is time-sensitive can be coded-around if the hardware that the computer is using is just too slow. That’s just fiction.

Your trying to press a non-real time operating system - Linux - into a sort of real-time processing - handing voice calls. The only reason it works at all is because the Linux system is so much faster than voice data - but there are physical limits to this stuff.

I understand the general point about Linux not being hard real-time and hardware limits being real.

But in this case I do not think “hardware too slow” explains the behavior well.

This is a 20-core system, and the issue was reproducible even at night with very low call load. The same machine, same OS, same timer source, and same Asterisk build returned to stable timing after disabling pollmailboxes=yes.

So the evidence does not point to the whole server being too slow for voice. It points to one specific polling path creating enough delay, blocking, or lock contention to prevent Asterisk from servicing timerfd promptly.

The key symptom is also important: the timerfd was still firing. Asterisk expected to acknowledge 1 tick but found 2 or 3 already accumulated. That means the timer source was not the thing stopping; Asterisk was coming back late to read it.

So yes, hardware limits always exist, but this looks like avoidable latency caused by mailbox polling/MWI work, not an overloaded or underpowered server.

But - mailbox polling is dependent on hard disk speed is it not? voicemails are stored as files so to check a box for a voicemail means a disk access. And MWI is also dependent on hardware isn’t it? That is if a message light status on a phone changes then a transaction has to go through pj_sip and could that be blocking waiting for the phone to reply with a status change on a message light?

If polling is frequent, there will be a cache hit on the memory copy of the relevant disk sectors. I assume this issue relates to frequent polling, and I will assume that there isn’t a large backlog of disk writes (forcing out cached copies of sectors already written) and a shortage of RAM.

look like i did not mention this but i am using voicemail odbc

THAT assumes a great deal, namely that the server administrator has setup statistical monitoring of the server.

Many times on these forums I’ll mention SNMP and get “you misspelled the email protocol SMTP” or I’ll mention Nagios and get “dude I’m not married”…

Most of the people buying servers are like “here’s my budget and that there sounds good and I can afford it” Then once the server is online the hardware config is frozen even if performance stats indicate the need for changes.

It also does not help that so many new servers come with Fakeraid “disk array cards” or that so many server admins don’t know what a Fakeraid card is, or that RAID setups using dmraid or md are software RAID and dump a ton of load on the main CPU…

It also does not help that server manufacturers marketing is totally slanted towards CPU power and when you start asking about basics like is the raid card hardware or software you get cricket chirps…

Not to disparage the OP but when I brought up performance his reply was “20 core system” it was NOT the output of top with the extra parameter needed for top to actually include output from all of the cores and show that Asterisk was, in fact, actually using more than 1 core…

Server performance is very misunderstood, you know.