Hi folks,<div><br></div><div>I think I&#39;ve found a race condition with Timers in xorg-server. I found this in the context of using Timers within an Xinput module. As you may know, Xinput modules handle input on a signal handler which interrupts the main thread of execution.</div>

<div><br></div><div>Looking at DoTimer in xorg-server&#39;s os/WaitFor.c file with some annotations added:</div><div><br></div><div><div><font class="Apple-style-span" face="&#39;courier new&#39;, monospace">static void</font></div>

<div><font class="Apple-style-span" face="&#39;courier new&#39;, monospace">DoTimer(OsTimerPtr timer, CARD32 now, OsTimerPtr *prev)</font></div><div><font class="Apple-style-span" face="&#39;courier new&#39;, monospace">{</font></div>

<div><font class="Apple-style-span" face="&#39;courier new&#39;, monospace">    CARD32 newTime;</font></div><div><font class="Apple-style-span" face="&#39;courier new&#39;, monospace"><br></font></div><div><font class="Apple-style-span" face="&#39;courier new&#39;, monospace"><meta charset="utf-8">    // A</font></div>

<div><font class="Apple-style-span" face="&#39;courier new&#39;, monospace">    *prev = timer-&gt;next;  // remote this timer from the list</font></div><div><font class="Apple-style-span" face="&#39;courier new&#39;, monospace"><meta charset="utf-8">    // B</font></div>

<div><font class="Apple-style-span" face="&#39;courier new&#39;, monospace">    timer-&gt;next = NULL;  // clean up </font><span class="Apple-style-span" style="font-family: &#39;courier new&#39;, monospace; ">this timer&#39;s </span><span class="Apple-style-span" style="font-family: &#39;courier new&#39;, monospace; ">next pointer</span></div>

<meta charset="utf-8"><div><font class="Apple-style-span" face="&#39;courier new&#39;, monospace"><meta charset="utf-8">    // C</font></div><div><font class="Apple-style-span" face="&#39;courier new&#39;, monospace">    newTime = (*timer-&gt;callback)(timer, now, timer-&gt;arg);</font></div>

<div><font class="Apple-style-span" face="&#39;courier new&#39;, monospace">    // D</font></div><div><font class="Apple-style-span" face="&#39;courier new&#39;, monospace">    if (newTime)</font></div><div><font class="Apple-style-span" face="&#39;courier new&#39;, monospace"><span class="Apple-tab-span" style="white-space:pre">        </span>TimerSet(timer, 0, newTime, timer-&gt;callback, timer-&gt;arg);</font></div>

<div><font class="Apple-style-span" face="&#39;courier new&#39;, monospace">}</font></div></div><div><br></div><div>Timer callbacks occur on the main thread, but an IO handler can interrupt at any time and schedule or cancel timers. Many timer handlers that worry about this will block SIGIO in their callback, so for now let&#39;s assume that no SIGIO will occur between C and D above. Think about the following sequence of events:</div>

<div><br></div><div><font class="Apple-style-span" face="&#39;courier new&#39;, monospace">Legend:</font></div><div><font class="Apple-style-span" face="&#39;courier new&#39;, monospace"><br></font></div><div><font class="Apple-style-span" face="&#39;courier new&#39;, monospace">Main Thread (not indented)</font></div>

<div><font class="Apple-style-span" face="&#39;courier new&#39;, monospace">        SIGIO handler (indented)</font></div><div><font class="Apple-style-span" face="&#39;courier new&#39;, monospace"><br></font></div><div><font class="Apple-style-span" face="&#39;courier new&#39;, monospace">Sequence of events:</font></div>

<div><font class="Apple-style-span" face="&#39;courier new&#39;, monospace"><br></font></div><div><font class="Apple-style-span" face="&#39;courier new&#39;, monospace">There is 1 live timer (timer #1), which is firing.</font></div>

<div><font class="Apple-style-span" face="&#39;courier new&#39;, monospace">DoTimer() called.</font></div><div><font class="Apple-style-span" face="&#39;courier new&#39;, monospace">Timer removed from list.</font></div><div>

<font class="Apple-style-span" face="&#39;courier new&#39;, monospace">We are at point &quot;B&quot; above</font></div><div><font class="Apple-style-span" face="&#39;courier new&#39;, monospace">        SIGIO occurs</font></div>

<div><font class="Apple-style-span" face="&#39;courier new&#39;, monospace">        Timer #1 is cancelled (noop)</font></div><div><font class="Apple-style-span" face="&#39;courier new&#39;, monospace">        Timer #1 is rescheduled - inserted into timers list</font></div>

<div><font class="Apple-style-span" face="&#39;courier new&#39;, monospace">        SIGIO is complete</font></div><div><font class="Apple-style-span" face="&#39;courier new&#39;, monospace"><br></font></div><div><font class="Apple-style-span" face="&#39;courier new&#39;, monospace">        Another module&#39;s SIGIO fires</font></div>

<div><font class="Apple-style-span" face="&#39;courier new&#39;, monospace">        Timer #2 is scheduled and goes at end of timers list</font></div><div><font class="Apple-style-span" face="&#39;courier new&#39;, monospace">        SIGIO is complete</font></div>

<div><font class="Apple-style-span" face="&#39;courier new&#39;, monospace"><br></font></div>(At this point, we have 2 timers in the timers list, the first of which is inside DoTimer, and we resume the main thread)<div><font class="Apple-style-span" face="&#39;courier new&#39;, monospace"><br>

</font></div><div><font class="Apple-style-span" face="&#39;courier new&#39;, monospace">Timer #1&#39;s next ptr set to NULL</font></div><div><br></div>(At this point, Timer #2 is lost from the timers list!)<div><br>I&#39;m new to the xorg-server code base, but my naive inclination is that when reading and writing the timers linked list, SIGIO (and other signals?) should be blocked. In the example above, if we blocked SIGIO at point A and unblocked it at point C, this particular race condition would be solved, it seems. However, I suspect that there are other problematic cases in WaitFor.c.</div>

<div><br></div><div>Also, to be fair, I haven&#39;t seen this problem occur in a real system; I only noticed it by looking at the code.</div><div><br></div><div>Thanks,</div><div>-andrew</div>