From 88e6325092bf1d1cfa877cd220670fa7cc2fad03 Mon Sep 17 00:00:00 2001 From: Felix Fietkau Date: Fri, 16 May 2025 12:27:07 +0200 Subject: [PATCH] libubus: flush all pending requests on connection loss This avoids running into timeouts when channel connections are lost. Signed-off-by: Felix Fietkau --- libubus-io.c | 4 +++- libubus-req.c | 10 ++++++++++ libubus.h | 1 + 3 files changed, 14 insertions(+), 1 deletion(-) diff --git a/libubus-io.c b/libubus-io.c index 16c1c14..beea3bf 100644 --- a/libubus-io.c +++ b/libubus-io.c @@ -328,8 +328,10 @@ void __hidden ubus_handle_data(struct uloop_fd *u, unsigned int events) if (!ctx->stack_depth) ctx->pending_timer.cb(&ctx->pending_timer); - if (u->eof) + if (u->eof) { + ubus_flush_requests(ctx); ctx->connection_lost(ctx); + } } void __hidden ubus_poll_data(struct ubus_context *ctx, int timeout) diff --git a/libubus-req.c b/libubus-req.c index 3e8d55c..4b718dc 100644 --- a/libubus-req.c +++ b/libubus-req.c @@ -137,6 +137,16 @@ static int64_t get_time_msec(void) return val; } +void ubus_flush_requests(struct ubus_context *ctx) +{ + struct ubus_request *req; + + while (!list_empty(&ctx->requests)) { + req = list_first_entry(&ctx->requests, struct ubus_request, list); + ubus_set_req_status(req, UBUS_STATUS_TIMEOUT); + } +} + int ubus_complete_request(struct ubus_context *ctx, struct ubus_request *req, int req_timeout) { diff --git a/libubus.h b/libubus.h index aa9263c..4f33339 100644 --- a/libubus.h +++ b/libubus.h @@ -264,6 +264,7 @@ int ubus_channel_connect(struct ubus_context *ctx, int fd, ubus_handler_t handler); int ubus_channel_create(struct ubus_context *ctx, int *remote_fd, ubus_handler_t handler); +void ubus_flush_requests(struct ubus_context *ctx); static inline bool ubus_context_is_channel(struct ubus_context *ctx) -- 2.30.2