From 79ccdc559490bcdab6529dc74bba5d694e8d5913 Mon Sep 17 00:00:00 2001 From: Nirbheek Chauhan Date: Thu, 17 Jul 2014 19:55:09 +0530 Subject: server: Fix another potential crash --- src/lib.c | 17 ++++++++++++++++- src/main.c | 6 +++--- 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/src/lib.c b/src/lib.c index 062a8be..a986f30 100644 --- a/src/lib.c +++ b/src/lib.c @@ -31,6 +31,13 @@ invoke_g_hash_table_remove (TranscodeServerCtx *ctx) return FALSE; } +static gboolean +invoke_g_free_client_context (TranscodeClientCtx *ctx) +{ + g_free (ctx); + return FALSE; +} + gboolean stp_on_gst_bus_message (GstBus *bus, GstMessage *msg, @@ -132,6 +139,7 @@ stp_cleanup_transcode_client_ctx (TranscodeClientCtx *ctx) stp_print_status (">>> Doing client cleanup."); g_source_remove (ctx->timeout_handler_id); + ctx->timeout_handler_id = 0; server_ctx->clients = g_list_remove (server_ctx->clients, ctx); @@ -160,9 +168,16 @@ stp_disconnect_cleanup_client (TranscodeClientCtx *ctx) soup_message_body_complete (ctx->msg->response_body); g_source_remove (ctx->timeout_handler_id); + ctx->timeout_handler_id = 0; /* Don't call the "finished" handler and do a double-free */ g_signal_handler_disconnect (ctx->msg, ctx->finished_handler_id); - g_free (ctx); + + /* There might still be functions which use the ctx waiting + * to be invoked on the next main context dispatch, so we + * free this at the end of all those dispatches. */ + g_main_context_invoke_full (NULL, G_PRIORITY_LOW, + (GSourceFunc)invoke_g_free_client_context, + ctx, NULL); } /* Returns a copy of the streamheader GstBuffer */ diff --git a/src/main.c b/src/main.c index 2d3e537..e9be17d 100644 --- a/src/main.c +++ b/src/main.c @@ -124,9 +124,9 @@ invoke_write_client_chunk (TranscodeClientCtx *ctx) stp_print_status ("*"); /* XXX: If samples are coming in too quickly, this can get invoked - * after the client has disconnected and the SoupMessage is invalid - * So, we check if the msg is still a msg before trying to append. */ - if (G_UNLIKELY (!SOUP_IS_MESSAGE (ctx->msg))) + * after the client has disconnected and the SoupMessage is invalid. + * When the client disconnects, timeout_handler_id is set to 0. */ + if (G_UNLIKELY (ctx->timeout_handler_id == 0)) return FALSE; g_signal_emit_by_name (ctx->appsink, "pull-sample", &sample); -- cgit v0.11.2-2-gd1dd