Skip to content

Commit

Permalink
opensl: Attempt to avoid a race with active callbacks during shutdown.
Browse files Browse the repository at this point in the history
Based on examining crash dumps and investigating OpenSL workarounds in
liboboe, there is a potential race between active buffer callbacks and
stream shutdown, resulting in a buffer callback attempting to use a new
buffer after stream destroy has freed the backing allocation.

Ideally there would be a concrete event to watch for to ensure the
last callbacks have completed before destroying the stream, but I'm not
aware of one and the existing workaround in liboboe relies on an
arbitrary ("long enough") sleep between stopping and destroying streams.
  • Loading branch information
kinetiknz committed Mar 21, 2023
1 parent 63c148f commit 2071354
Showing 1 changed file with 10 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/cubeb_opensl.c
Expand Up @@ -1655,6 +1655,16 @@ opensl_stream_destroy(cubeb_stream * stm)
{
assert(stm->draining || stm->shutdown);

// If we're still draining at stream destroy time, pause the streams now so we
// can destroy them safely.
if (stm->draining) {
opensl_stream_stop(stm);
}
// Sleep for 10ms to give active streams time to pause so that no further
// buffer callbacks occur. Inspired by the same workaround (sleepBeforeClose)
// in liboboe.
usleep(10 * 1000);

if (stm->playerObj) {
(*stm->playerObj)->Destroy(stm->playerObj);
stm->playerObj = NULL;
Expand Down

0 comments on commit 2071354

Please sign in to comment.