Skip to content

Commit

Permalink
We are not allowed to immediately free listen sockets
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex Hultman authored and Alex Hultman committed Aug 24, 2019
1 parent 2b34391 commit db79be3
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 6 deletions.
20 changes: 14 additions & 6 deletions src/context.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,21 @@ int default_ignore_data_handler(struct us_socket_t *s) {
/* Shared with SSL */

void us_listen_socket_close(int ssl, struct us_listen_socket_t *ls) {
us_internal_socket_context_unlink(ls->s.context, &ls->s);

us_poll_stop((struct us_poll_t *) &ls->s, ls->s.context->loop);
bsd_close_socket(us_poll_fd((struct us_poll_t *) &ls->s));
/* us_listen_socket_t extends us_socket_t so we close in similar ways */
if (!us_socket_is_closed(0, &ls->s)) {
us_internal_socket_context_unlink(ls->s.context, &ls->s);
us_poll_stop((struct us_poll_t *) &ls->s, ls->s.context->loop);
bsd_close_socket(us_poll_fd((struct us_poll_t *) &ls->s));

/* Link this socket to the close-list and let it be deleted after this iteration */
ls->s.next = ls->s.context->loop->data.closed_head;
ls->s.context->loop->data.closed_head = &ls->s;

/* Any socket with prev = context is marked as closed */
ls->s.prev = (struct us_socket_t *) ls->s.context;
}

/* Listen sockets have no complex callbacks using it, so we free it immediately here */
us_poll_free((struct us_poll_t *) &ls->s, ls->s.context->loop);
/* We cannot immediately free a listen socket as we can be inside an accept loop */
}

void us_internal_socket_context_unlink(struct us_socket_context_t *context, struct us_socket_t *s) {
Expand Down
6 changes: 6 additions & 0 deletions src/loop.c
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,12 @@ void us_internal_dispatch_ready_poll(struct us_poll_t *p, int error, int events)
us_internal_socket_context_link(listen_socket->s.context, s);

listen_socket->s.context->on_open(s, 0, bsd_addr_get_ip(&addr), bsd_addr_get_ip_length(&addr));

/* Exit accept loop if listen socket was closed in on_open handler */
if (us_socket_is_closed(0, &listen_socket->s)) {
break;
}

} while ((client_fd = bsd_accept_socket(us_poll_fd(p), &addr)) != LIBUS_SOCKET_ERROR);
}
}
Expand Down

0 comments on commit db79be3

Please sign in to comment.