bluetooth-policy: do A2DP profile restoring a bit later
authorTanu Kaskinen <tanuk@iki.fi>
Wed, 12 Oct 2016 14:20:39 +0000 (17:20 +0300)
committerTanu Kaskinen <tanuk@iki.fi>
Mon, 19 Dec 2016 23:26:30 +0000 (01:26 +0200)
commit2250dbfd6968bd9ce295fc7bef8595b2c6ef6a44
tree4cb99b614d676c61407d65c6ade6bcc705a642e7
parent60695e3d84c98e4153edd5dda9dbf1dd7fde0de0
bluetooth-policy: do A2DP profile restoring a bit later

This fixes a crash that happens if the bluetooth headset is the only
non-monitor source in the system and the last "phone" stream dies.
When the stream dies, the native protocol calls pa_source_output_unlink()
and would call pa_source_output_unref() next, but without this patch,
things happen during the unlinking, and the unreffing ends up being
performed on a stream that is already freed.

pa_source_output_unlink() fires the "unlink" hook before doing anything
else. module-bluetooth-policy then switches the headset profile from HSP
to A2DP within that hook. The HSP source gets removed, and at this point
the dying stream is still connected to it, and needs to be rescued.
Rescuing fails, because there are no other sources in the system, so the
stream gets killed. The native protocol has a kill callback, which again
calls pa_source_output_unlink() and pa_source_output_unref(). This is
the point where the native protocol drops its own reference to the
stream, but another unref call is waiting to be executed once we return
from the original unlink call.

I first tried to avoid the double unreffing by making it safe to do
unlinking recursively, but I found out that there's code that assumes
that once unlink() returns, unlinking has actually occurred (a
reasonable assumption), and at least with my implementation this was not
guaranteed. I now think that we must avoid situations where unlinking
happens recursively. It's just too hairy to deal with. This patch moves
the bluetooth profile switch to happen at a time when the dead stream
isn't any more connected to the source, so it doesn't have to be
rescued or killed.

BugLink: https://bugs.freedesktop.org/show_bug.cgi?id=97906
src/modules/bluetooth/module-bluetooth-policy.c