From 020070b9a2cb09f7f5ec777b90eb963380d7b5af Mon Sep 17 00:00:00 2001 From: Bohdan Kurylovych <33369297+borzun@users.noreply.github.com> Date: Thu, 30 Apr 2020 20:52:14 +0300 Subject: [PATCH] Fix data race when stop stream during connection (#184) Fix a data race when user manually stopped stream during connecting to a RTMP server. This is caused by a data race in `obs_output_destroy` method, which call `rtmp_stream_destroy` method before output became active. In such case, the `rtmp_stream_destroy` method will detach `end_data_capture` thread, thus it will result in a potential crash. In order to avoid this, the thread which destroys output should wait till `end_data_capture` will be finished. --- libobs/obs-output.c | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/libobs/obs-output.c b/libobs/obs-output.c index 20d24ff6d6f765..a9ae7a80acd900 100644 --- a/libobs/obs-output.c +++ b/libobs/obs-output.c @@ -207,6 +207,15 @@ void obs_output_destroy(obs_output_t *output) if (output->context.data) { output->info.destroy(output->context.data); output->context.data = NULL; + + // In case when output has started connecting, but haven't started + // capturing data (i.e. not active), the info `destroy` call can + // switch output to active state - in this case it is needed to wait till + // the `end_data_capture_thread` will finish. + // Otherwise we might run into a data race + if (data_capture_ending(output)) + pthread_join(output->end_data_capture_thread, + NULL); } free_packets(output);