libubus: flush all pending requests on connection loss
authorFelix Fietkau <nbd@nbd.name>
Fri, 16 May 2025 10:27:07 +0000 (12:27 +0200)
committerFelix Fietkau <nbd@nbd.name>
Fri, 16 May 2025 10:27:50 +0000 (12:27 +0200)
This avoids running into timeouts when channel connections are lost.

Signed-off-by: Felix Fietkau <nbd@nbd.name>
libubus-io.c
libubus-req.c
libubus.h

index 16c1c14e786eff73fa9dc4f4afd4f52b92de343a..beea3bf9df6d9001bf8d53137a93898b9cf47126 100644 (file)
@@ -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)
index 3e8d55c93f7d9adda3ae58a221103a0658b87244..4b718dc5a5237fe846e4c839014ebe9b7a266845 100644 (file)
@@ -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)
 {
index aa9263c8084d54d934f6aaf56379ca35b2e9edef..4f333391794517e1fa759de7057cb69af719310e 100644 (file)
--- 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)