server: Defer cleaning up client resources on close

When the event is received, the client object may still be processing some
things, so let's allow it to finish.
pull/96/head
Andri Yngvason 2023-10-03 22:15:17 +00:00
parent f8f49196e8
commit dc1d93cadf
1 changed files with 11 additions and 6 deletions

View File

@ -159,7 +159,7 @@ static inline void client_ref(struct nvnc_client* client)
++client->ref; ++client->ref;
} }
static void deferred_client_close(void *obj) static void do_deferred_client_close(void *obj)
{ {
client_unref(obj); client_unref(obj);
} }
@ -169,6 +169,14 @@ static void stop_self(void* obj)
aml_stop(aml_get_default(), obj); aml_stop(aml_get_default(), obj);
} }
static void defer_client_close(struct nvnc_client* client)
{
struct aml_idle* idle = aml_idle_new(stop_self, client,
do_deferred_client_close);
aml_start(aml_get_default(), idle);
aml_unref(idle);
}
static void close_after_write(void* userdata, enum stream_req_status status) static void close_after_write(void* userdata, enum stream_req_status status)
{ {
struct nvnc_client* client = userdata; struct nvnc_client* client = userdata;
@ -180,10 +188,7 @@ static void close_after_write(void* userdata, enum stream_req_status status)
* stays alive while the stream is processing its queue. * stays alive while the stream is processing its queue.
* TODO: Figure out some better resource management for clients * TODO: Figure out some better resource management for clients
*/ */
struct aml_idle* idle = aml_idle_new(stop_self, client, defer_client_close(client);
deferred_client_close);
aml_start(aml_get_default(), idle);
aml_unref(idle);
} }
static int handle_unsupported_version(struct nvnc_client* client) static int handle_unsupported_version(struct nvnc_client* client)
@ -1621,7 +1626,7 @@ static void on_client_event(struct stream* stream, enum stream_event event)
if (event == STREAM_EVENT_REMOTE_CLOSED) { if (event == STREAM_EVENT_REMOTE_CLOSED) {
nvnc_log(NVNC_LOG_INFO, "Client %p (%d) hung up", client, client->ref); nvnc_log(NVNC_LOG_INFO, "Client %p (%d) hung up", client, client->ref);
nvnc_client_close(client); defer_client_close(client);
return; return;
} }