<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_threaded_mainloop_stop() blocks on pa_mutex_lock(m->mutex)"
   href="https://bugs.freedesktop.org/show_bug.cgi?id=62529">62529</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_threaded_mainloop_stop() blocks on pa_mutex_lock(m->mutex)
          </td>
        </tr>

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

        <tr>
          <th>Severity</th>
          <td>major
          </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>ARM
          </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 recently switched my pulseaudio code to pulseaudio-3.0 and used
pa_threaded_mainloop to use pulseaudio library in multi-threaded application.
I find in some cases, pa_threaded_mainloop_stop() will just block on
pa_mutex_lock(m->mutex) and never quit.

I have read the source code of pa_threaded_mainloop_stop()
pa_threaded_mainloop_run(), it seems that there is one possibility that makes
this happen.
In pa_threaded_mainloop_run(), a thread is created and pa_mainloop_run is
called and protected by pa_mutex_lock(m->mutex) and pa_mutex_unlock(m->mutex)
in the thread;

in pa_threaded_mainloop_stop(), pa_mainloop_quit is also protected by
pa_mutex_lock(m->mutex) and pa_mutex_unlock(m->mutex);

So, here is the problem, in pa_threaded_mainloop_stop(), m->mutex must be
aquired before pa_mainloop_quit() can be called, and in
pa_threaded_mainloop_run(), when pa_mainloop_run() exits, m->mutex can be
unlocked. If pa_mainloop_run() is blocked, the only way to make
pa_mainloop_run() exit is to call pa_mainloop_quit(), but pa_mainloop_quit()
can only be called after pa_mainloop_run() is quit.

This is the deadlock.

I noticed that the mutex is created with flag RECURSIVE, which only makes this
mutex can be locked recursively in the same thread. In my case,
pa_threaded_mainloop_stop() is called from another thread, so the mutex still
can block pa_threaded_mainloop_stop().

This is easy to reproduce, use pa_threaded_mainloop() to write a recording
program, and start/stop repeatedly, sometimes pa_threaded_mainloop_stop() is
deadlocked.</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>