diff --git a/src/bsd.c b/src/bsd.c index d35eae9..02c65cc 100644 --- a/src/bsd.c +++ b/src/bsd.c @@ -306,7 +306,11 @@ LIBUS_SOCKET_DESCRIPTOR bsd_create_socket(int domain, int type, int protocol) { LIBUS_SOCKET_DESCRIPTOR created_fd = socket(domain, type | flags, protocol); +#if defined(SOCK_CLOEXEC) && defined(SOCK_NONBLOCK) + return apple_no_sigpipe(created_fd); +#else return bsd_set_nonblocking(apple_no_sigpipe(created_fd)); +#endif } void bsd_close_socket(LIBUS_SOCKET_DESCRIPTOR fd) { @@ -400,7 +404,11 @@ LIBUS_SOCKET_DESCRIPTOR bsd_accept_socket(LIBUS_SOCKET_DESCRIPTOR fd, struct bsd internal_finalize_bsd_addr(addr); +#if defined(SOCK_CLOEXEC) && defined(SOCK_NONBLOCK) + return apple_no_sigpipe(accepted_fd); +#else return bsd_set_nonblocking(apple_no_sigpipe(accepted_fd)); +#endif } int bsd_recv(LIBUS_SOCKET_DESCRIPTOR fd, void *buf, int length, int flags) { diff --git a/src/loop.c b/src/loop.c index 8f3b696..98a5d90 100644 --- a/src/loop.c +++ b/src/loop.c @@ -360,7 +360,8 @@ void us_internal_dispatch_ready_poll(struct us_poll_t *p, int error, int events) /* If we filled the entire recv buffer, we need to immediately read again since otherwise a * pending hangup event in the same even loop iteration can close the socket before we get * the chance to read again next iteration */ - if (length == LIBUS_RECV_BUFFER_LENGTH && s && !us_socket_is_closed(0, s)) { + if (length == LIBUS_RECV_BUFFER_LENGTH && s && !us_socket_is_closed(0, s) + && (us_poll_events(&s->p) & LIBUS_SOCKET_READABLE)) { goto read_more; }