Skip to content

Commit

Permalink
net: sockets: services: Don't modify pollfd array from other threads
Browse files Browse the repository at this point in the history
pollfd array used with zsock_poll() should not be modified while inside
zsock_poll() function as this could lead to unexpected results. For
instance, k_poll already monitoring some kernel primitive could report
an event, but it will not be processed if the monitored socket file
descriptor in the pollfd array was set to -1. In result,
zsock_poll() may unexpectedly quit prematurely, returning 0 events, even
if it was requested to wait infinitely.

The pollfd arrays used by zsock_poll() (ctx.events) is reinitialized
when the service thread is restarted so modifying it directly when
registering/unregistering service is not really needed. It's enough if
those functions notify the eventfd socket used to restart the services
thread.

Signed-off-by: Robert Lubos <[email protected]>
  • Loading branch information
rlubos committed Jul 5, 2024
1 parent 7a10c7d commit 4aa8663
Showing 1 changed file with 0 additions and 5 deletions.
5 changes: 0 additions & 5 deletions subsys/net/lib/sockets/sockets_service.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ void net_socket_service_foreach(net_socket_service_cb_t cb, void *user_data)
static void cleanup_svc_events(const struct net_socket_service_desc *svc)
{
for (int i = 0; i < svc->pev_len; i++) {
ctx.events[get_idx(svc) + i].fd = -1;
svc->pev[i].event.fd = -1;
svc->pev[i].event.events = 0;
}
Expand Down Expand Up @@ -87,10 +86,6 @@ int z_impl_net_socket_service_register(const struct net_socket_service_desc *svc
svc->pev[i].event = fds[i];
svc->pev[i].user_data = user_data;
}

for (i = 0; i < svc->pev_len; i++) {
ctx.events[get_idx(svc) + i] = svc->pev[i].event;
}
}

/* Tell the thread to re-read the variables */
Expand Down

0 comments on commit 4aa8663

Please sign in to comment.