From 8872dece0ca65a565114d67bf4cbe734a5b996ce Mon Sep 17 00:00:00 2001 From: Andri Yngvason Date: Tue, 4 Jul 2023 23:40:29 +0000 Subject: [PATCH] server: Defer client_unref in close_after_write This ensures that the stream object stays alive while its write queue is being processed. --- src/server.c | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/src/server.c b/src/server.c index 05d2bee..fd72193 100644 --- a/src/server.c +++ b/src/server.c @@ -146,12 +146,31 @@ static inline void client_ref(struct nvnc_client* client) ++client->ref; } +static void deferred_client_close(void *obj) +{ + client_unref(obj); +} + +static void stop_self(void* obj) +{ + aml_stop(aml_get_default(), obj); +} + static void close_after_write(void* userdata, enum stream_req_status status) { struct nvnc_client* client = userdata; nvnc_log(NVNC_LOG_DEBUG, "close_after_write(%p): ref %d", client, client->ref); - nvnc_client_close(client); + stream_close(client->net_stream); + + /* This is a rather hacky way of making sure that the client object + * stays alive while the stream is processing its queue. + * TODO: Figure out some better resource management for clients + */ + struct aml_idle* idle = aml_idle_new(stop_self, client, + deferred_client_close); + aml_start(aml_get_default(), idle); + aml_unref(idle); } static int handle_unsupported_version(struct nvnc_client* client)