From 03dce720baf91ff67eb82431f8d6ad24b4f4d657 Mon Sep 17 00:00:00 2001 From: Christoph Lipka Date: Mon, 7 Nov 2016 15:26:35 +0900 Subject: Event handling: Fix connection destroy bug It might happen that an event is part of the epoll event queue that belongs to a connection which was destroyed before the event is handled. Due to this, the event handling main loop might stop and the daemon exits. This misbehavior is fixed with this patch. Signed-off-by: Christoph Lipka --- src/daemon/dlt_daemon_connection.c | 4 ++++ src/daemon/dlt_daemon_event_handler.c | 4 ++++ 2 files changed, 8 insertions(+) diff --git a/src/daemon/dlt_daemon_connection.c b/src/daemon/dlt_daemon_connection.c index 51f975d..b22f4b7 100644 --- a/src/daemon/dlt_daemon_connection.c +++ b/src/daemon/dlt_daemon_connection.c @@ -171,6 +171,7 @@ STATIC void dlt_connection_destroy_receiver(DltConnection *con) default: (void) dlt_receiver_free(con->receiver); free(con->receiver); + con->receiver = NULL; break; } } @@ -320,6 +321,9 @@ void dlt_connection_destroy(DltConnection *to_destroy) { close(to_destroy->receiver->fd); dlt_connection_destroy_receiver(to_destroy); + /* connection pointer might be in epoll queue and used even after destroying + * it. To make sure it is not used anymore, connection type is invalidated */ + to_destroy->type = DLT_CONNECTION_TYPE_MAX; free(to_destroy); } diff --git a/src/daemon/dlt_daemon_event_handler.c b/src/daemon/dlt_daemon_event_handler.c index 9ee3bc1..cfe61a4 100644 --- a/src/daemon/dlt_daemon_event_handler.c +++ b/src/daemon/dlt_daemon_event_handler.c @@ -137,6 +137,10 @@ int dlt_daemon_handle_event(DltEventHandler *pEvent, type = con->type; fd = con->receiver->fd; } + else /* connection might have been destroyed in the meanwhile */ + { + continue; + } /* First of all handle epoll error events * We only expect EPOLLIN or EPOLLOUT -- cgit v1.2.1