[asterisk-bugs] [JIRA] Created: (ASTERISK-20414) Timeout antipattern using ast_waitfor_nandfds

David M. Lee (JIRA) noreply at issues.asterisk.org
Wed Sep 12 13:20:07 CDT 2012


Timeout antipattern using ast_waitfor_nandfds
---------------------------------------------

                 Key: ASTERISK-20414
                 URL: https://issues.asterisk.org/jira/browse/ASTERISK-20414
             Project: Asterisk
          Issue Type: Bug
      Security Level: None
          Components: Channels/General
    Affects Versions: 1.8.15.1
            Reporter: David M. Lee


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.

--
This message is automatically generated by JIRA.
For more information on JIRA, see: http://www.atlassian.com/software/jira



More information about the asterisk-bugs mailing list