[Home]

Summary:ASTERISK-20414: Timeout antipattern using ast_waitfor_nandfds
Reporter:David M. Lee (dlee)Labels:
Date Opened:2012-09-12 13:19:47Date Closed:2012-11-07 11:04:21.000-0600
Priority:MajorRegression?No
Status:Closed/CompleteComponents:Channels/General
Versions:1.8.15.1 Frequency of
Occurrence
Occasional
Related
Issues:
Environment:Attachments:
Description:While fixing ASTERISK-20375, I noticed a fairly subtle anti-pattern using {{ast_waitfor_nandfds}}.

{code}
int timeout_ms = 3000;
while (timeout_ms) {
 r = ast_waitfor_nandfds(chan, n, fds, nfds, NULL, &outfd, &timeout_ms);
 /* whatever */
}
{code}

If the {{waitfor}} function takes less than one millisecond to return, then it won't adjust {{timeout_ms}}. If that happens consistently, then this loop will never exit. A better approach would be:

{code}
int timeout_ms = 3000;
struct timeval start = ast_tvnow();
while (ast_tvdiff_ms(ast_tvnow(), start) < timeout_ms) {
 int ms = timeout_ms - ast_tvdiff_ms(ast_tvnow(), start);

 if (ms < 0) {
   ms = 0;
 }
 r = ast_waitfor_nandfds(chan, n, fds, nfds, NULL, &outfd, &ms);
 /* whatever */
}
{code}

I've fixed the problem in question [for {{ast_waitfordigit_full}}|https://reviewboard.asterisk.org/r/2109], but we should look for other misuses of this pattern so we can avoid further lockups.
Comments: