Skip to content

Commit 83bf832

Browse files
committed
net: dns: Fix server slot cleanup on INACTIVE context close
dns_resolve_close() fails to clean up server slots when the context is in DNS_RESOLVE_CONTEXT_INACTIVE state. Signed-off-by: Zafer SEN <[email protected]>
1 parent e438b57 commit 83bf832

File tree

1 file changed

+23
-1
lines changed

1 file changed

+23
-1
lines changed

subsys/net/lib/dns/resolve.c

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2269,7 +2269,29 @@ static int dns_resolve_close_locked(struct dns_resolve_context *ctx)
22692269
int i, ret;
22702270

22712271
if (ctx->state != DNS_RESOLVE_CONTEXT_ACTIVE) {
2272-
return -ENOENT;
2272+
/* Even if context is not ACTIVE, we should still close any open sockets
2273+
* to ensure proper cleanup. This handles cases where context is INACTIVE
2274+
* but server slots still have valid data from previous initialization.
2275+
*/
2276+
LOG_INF("DNS context not ACTIVE (state=%d), but will close any open servers",
2277+
ctx->state);
2278+
2279+
/* Close any servers that have open sockets */
2280+
for (i = 0; i < SERVER_COUNT; i++) {
2281+
if (ctx->servers[i].sock >= 0 ||
2282+
ctx->servers[i].dns_server.sa_family != 0) {
2283+
LOG_INF("Closing server[%d] sock=%d sa_family=%d", i,
2284+
ctx->servers[i].sock, ctx->servers[i].dns_server.sa_family);
2285+
ret = dns_server_close(ctx, i);
2286+
if (ret < 0 && ret != -ENOENT) {
2287+
NET_DBG("Cannot close DNS server %d (%d)", i, ret);
2288+
}
2289+
}
2290+
}
2291+
2292+
/* Ensure state is INACTIVE */
2293+
ctx->state = DNS_RESOLVE_CONTEXT_INACTIVE;
2294+
return 0;
22732295
}
22742296

22752297
ctx->state = DNS_RESOLVE_CONTEXT_DEACTIVATING;

0 commit comments

Comments
 (0)