<html>
    <head>
      <base href="https://bugs.freedesktop.org/" />
    </head>
    <body><table border="1" cellspacing="0" cellpadding="8">
        <tr>
          <th>Priority</th>
          <td>medium
          </td>
        </tr>

        <tr>
          <th>Bug ID</th>
          <td><a class="bz_bug_link 
          bz_status_NEW "
   title="NEW --- - pa_mainloop_quit() can't make pa_mainloop_run exit"
   href="https://bugs.freedesktop.org/show_bug.cgi?id=56735">56735</a>
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>lennart@poettering.net
          </td>
        </tr>

        <tr>
          <th>Assignee</th>
          <td>pulseaudio-bugs@lists.freedesktop.org
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>pa_mainloop_quit() can't make pa_mainloop_run exit
          </td>
        </tr>

        <tr>
          <th>QA Contact</th>
          <td>pulseaudio-bugs@lists.freedesktop.org
          </td>
        </tr>

        <tr>
          <th>Severity</th>
          <td>critical
          </td>
        </tr>

        <tr>
          <th>Classification</th>
          <td>Unclassified
          </td>
        </tr>

        <tr>
          <th>OS</th>
          <td>Linux (All)
          </td>
        </tr>

        <tr>
          <th>Reporter</th>
          <td>changyp6@gmail.com
          </td>
        </tr>

        <tr>
          <th>Hardware</th>
          <td>Other
          </td>
        </tr>

        <tr>
          <th>Status</th>
          <td>NEW
          </td>
        </tr>

        <tr>
          <th>Version</th>
          <td>unspecified
          </td>
        </tr>

        <tr>
          <th>Component</th>
          <td>clients
          </td>
        </tr>

        <tr>
          <th>Product</th>
          <td>PulseAudio
          </td>
        </tr></table>
      <p>
        <div>
        <pre>I wrote a recording program using pulseaudio's API. version 2.1
I found that pa_mainloop_quit can't make pa_mainloop_run exit.
I did a little digging on this problem, and I think I have found this bug.

In pa_mainloop_quit, pa_mainloop_wakeup is called to make pa_mainloop_poll
unblocked from select.
However, In pa_mainloop_wakeup, only if m->state == STATE_POLLING,
this wake up signal is sent to pa_mainloop_poll.

In pa_mainloop_poll, checking if m->quit is set and setting m->state to
STATE_POLLING is not protected.

So, in this case, in multi-thread program, it is possible that
Mainloop-thread: pa_mainloop_poll checked m->quit is set to 0, m->state !=
STATE_POLLING
Controlling-thread: pa_mainloop_quit->pa_mainloop_wakeup checked m->state !=
STATE_POLLING, failed sending wake up signal
Mainloop-thread: pa_mainloop_polling set m->state = STATE_POLLING and go on
calling pa_poll, and everything stops here.

There is on solution: pa_mainloop_wakeup doesn't check if m->state ==
STATE_POLLING, it just send wake up signal, so pa_mainloop_polling will receive
this signal
Or protect the m->quit checking and m->state setting section in
pa_mainloop_poll
and protect pa_mainloop_quit with on mutex, make these two API won't run in
mixed way.

To work around this bug, I have to call pa_mainloop_quit multiple times to make
sure pa_mainloop_run is quit.

Hope this bug can be fixed sooner.</pre>
        </div>
      </p>
      <hr>
      <span>You are receiving this mail because:</span>
      
      <ul>
          <li>You are the QA Contact for the bug.</li>
          <li>You are the assignee for the bug.</li>
      </ul>
    </body>
</html>