From: Andre Noll Date: Mon, 3 Feb 2014 20:06:00 +0000 (+0100) Subject: ao_write: Avoid segfault on exit. X-Git-Tag: v0.5.3~20^2~5 X-Git-Url: http://git.tuebingen.mpg.de/?p=paraslash.git;a=commitdiff_plain;h=5323dab36cfea7ee18f6ade7e38f1218cd7a9ea5 ao_write: Avoid segfault on exit. If para_audiod is terminated by a signal it calls the ->close() method of each active writer. In this method the ao writer calls ao_close() and frees all resources but leaves the play thread active. So the thread continues to run, prints ao_alsa ERROR: write error: File descriptor in bad state and probably crashes afterwards due to accessing freed memory. Fix this by cancelling the thread in ->close() if it exists. Ironically enough, this re-introduces a call to pthread_join(), but it's OK here since we the cancelled thread should exit immediately (so we don't block), and we are about to exit anyway. --- diff --git a/ao_write.c b/ao_write.c index a2e86ed2..12ab77fe 100644 --- a/ao_write.c +++ b/ao_write.c @@ -39,6 +39,10 @@ static void aow_close(struct writer_node *wn) if (!pawd) return; + if (pawd->thread_btrn) { + pthread_cancel(pawd->thread); + pthread_join(pawd->thread, NULL); + } ao_close(pawd->dev); free(pawd); wn->private_data = NULL;