Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 38 additions & 3 deletions packages/bun-usockets/src/bsd.c
Original file line number Diff line number Diff line change
Expand Up @@ -1097,6 +1097,7 @@ inline __attribute__((always_inline)) LIBUS_SOCKET_DESCRIPTOR bsd_bind_listen_fd
) {

if (bsd_set_reuse(listenFd, options) != 0) {
*error = LIBUS_ERR;
return LIBUS_SOCKET_ERROR;
}

Expand All @@ -1113,12 +1114,24 @@ inline __attribute__((always_inline)) LIBUS_SOCKET_DESCRIPTOR bsd_bind_listen_fd
if (listenAddr->ai_family == AF_INET6) {
int enabled = (options & LIBUS_SOCKET_IPV6_ONLY) != 0;
if (setsockopt(listenFd, IPPROTO_IPV6, IPV6_V6ONLY, &enabled, sizeof(enabled)) != 0) {
*error = LIBUS_ERR;
return LIBUS_SOCKET_ERROR;
}
}
#endif

if (us_internal_bind_and_listen(listenFd, listenAddr->ai_addr, (socklen_t) listenAddr->ai_addrlen, 512, error)) {
#if defined(_WIN32)
// With SO_EXCLUSIVEADDRUSE set (which Bun does by default when
// LIBUS_LISTEN_EXCLUSIVE_PORT is passed), Windows returns
// WSAEACCES instead of WSAEADDRINUSE when the port is already
// bound. Libuv/Node normalize this to EADDRINUSE — do the same
// so callers get a consistent code cross-platform.
// https://learn.microsoft.com/en-us/windows/win32/winsock/so-exclusiveaddruse
if ((options & LIBUS_LISTEN_EXCLUSIVE_PORT) && *error == WSAEACCES) {
*error = WSAEADDRINUSE;
}
#endif
Comment thread
robobun marked this conversation as resolved.
Resolved
return LIBUS_SOCKET_ERROR;
}

Expand Down Expand Up @@ -1157,6 +1170,9 @@ LIBUS_SOCKET_DESCRIPTOR bsd_create_listen_socket(const char *host, int port, int
snprintf(port_string, 16, "%d", port);

if (getaddrinfo(host, port_string, &hints, &result)) {
// getaddrinfo returns an EAI_* code, not a regular errno. Leave
// *error at its caller-initialized 0 so the caller falls back to the
// generic "port in use?" message.
return LIBUS_SOCKET_ERROR;
}

Comment thread
robobun marked this conversation as resolved.
Resolved
Expand All @@ -1166,6 +1182,9 @@ LIBUS_SOCKET_DESCRIPTOR bsd_create_listen_socket(const char *host, int port, int
if (a->ai_family == AF_INET6) {
listenFd = bsd_create_socket(a->ai_family, a->ai_socktype, a->ai_protocol, NULL);
if (listenFd == LIBUS_SOCKET_ERROR) {
// Preserve the real errno (EMFILE, ENFILE, EACCES, …) so the
// caller can surface it if all addrinfos exhaust.
*error = LIBUS_ERR;
continue;
}

Expand All @@ -1183,6 +1202,7 @@ LIBUS_SOCKET_DESCRIPTOR bsd_create_listen_socket(const char *host, int port, int
if (a->ai_family == AF_INET) {
listenFd = bsd_create_socket(a->ai_family, a->ai_socktype, a->ai_protocol, NULL);
if (listenFd == LIBUS_SOCKET_ERROR) {
*error = LIBUS_ERR;
continue;
}

Expand Down Expand Up @@ -1343,6 +1363,7 @@ static LIBUS_SOCKET_DESCRIPTOR internal_bsd_create_listen_socket_unix(const char
listenFd = bsd_create_socket(AF_UNIX, SOCK_STREAM, 0, NULL);

if (listenFd == LIBUS_SOCKET_ERROR) {
*error = LIBUS_ERR;
return LIBUS_SOCKET_ERROR;
Comment thread
robobun marked this conversation as resolved.
Resolved
}

Expand All @@ -1353,7 +1374,13 @@ static LIBUS_SOCKET_DESCRIPTOR internal_bsd_create_listen_socket_unix(const char
bsd_close_socket(listenFd);
#if defined(_WIN32)
if (shouldSimulateENOENT) {
// Windows' AF_UNIX impl returns the misleading WSAENETDOWN when
// the socket path's parent directory doesn't exist. Translate
// to ERROR_PATH_NOT_FOUND so SystemErrno.init maps to ENOENT.
// Write *error as well — the caller now reads the plumbed value
// instead of thread-local last-error.
SetLastError(ERROR_PATH_NOT_FOUND);
*error = ERROR_PATH_NOT_FOUND;
}
#endif
return LIBUS_SOCKET_ERROR;
Expand All @@ -1368,9 +1395,16 @@ LIBUS_SOCKET_DESCRIPTOR bsd_create_listen_socket_unix(const char *path, size_t l
size_t addrlen = 0;
if (bsd_create_unix_socket_address(path, len, &dirfd_workaround_for_unix_path_len, &server_address, &addrlen)) {
/* The path could not be expressed as a sockaddr_un (the basename
* exceeds sun_path even with the dirfd workaround); surface the errno
* so the caller can report something better than a codeless failure. */
if (error && errno) *error = errno;
* exceeds sun_path even with the dirfd workaround); surface the error
* so the caller can report something better than a codeless failure.
* bsd_create_unix_socket_address signals failure via SetLastError
* (the Win32 channel, NOT Winsock) on Windows and via errno on POSIX,
* so read GetLastError directly on Windows rather than LIBUS_ERR. */
#if defined(_WIN32)
*error = (int)GetLastError();
#else
if (errno) *error = errno;
#endif
return LIBUS_SOCKET_ERROR;
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
}

Expand All @@ -1379,6 +1413,7 @@ LIBUS_SOCKET_DESCRIPTOR bsd_create_listen_socket_unix(const char *path, size_t l
if (__pthread_fchdir(dirfd_workaround_for_unix_path_len) != 0) {
close(dirfd_workaround_for_unix_path_len);
errno = ENAMETOOLONG;
*error = ENAMETOOLONG;
Comment thread
robobun marked this conversation as resolved.
Resolved
return LIBUS_SOCKET_ERROR;
}
}
Expand Down
4 changes: 2 additions & 2 deletions packages/bun-usockets/src/context.c
Original file line number Diff line number Diff line change
Expand Up @@ -375,8 +375,8 @@ struct us_listen_socket_t *us_socket_group_listen(struct us_socket_group_t *grou
us_poll_init(p, listen_socket_fd, POLL_TYPE_SEMI_SOCKET);
if (us_poll_start_rc(p, group->loop, LIBUS_SOCKET_READABLE) != 0) {
/* EPOLL_CTL_ADD failed (e.g. ENOSPC at fs.epoll.max_user_watches).
* Report via both the out-param and thread-local errno: Bun.listen
* reads *error, Bun.serve reads errno. */
* Report via the out-param, which both Bun.listen and Bun.serve read;
* restore errno across the close/free cleanup so it isn't masked. */
int saved_errno = errno;
bsd_close_socket(listen_socket_fd);
us_poll_free(p, group->loop);
Expand Down
40 changes: 28 additions & 12 deletions packages/bun-uws/src/App.h
Original file line number Diff line number Diff line change
Expand Up @@ -697,45 +697,61 @@ struct TemplatedApp {
struct ssl_ctx_st *sslCtxOrNull() { return SSL ? sslCtx : nullptr; }

public:
/* The handler's second argument is the errno from the bind/listen syscall on
* failure, or 0 on success. Callers that ignore it still get the listen socket
* (or nullptr) as the first argument. */

/* Host, port, callback */
TemplatedApp &&listen(const std::string &host, int port, MoveOnlyFunction<void(us_listen_socket_t *)> &&handler) {
TemplatedApp &&listen(const std::string &host, int port, MoveOnlyFunction<void(us_listen_socket_t *, int)> &&handler) {
if (host.empty()) {
return listen(port, std::move(handler));
}
handler(httpContext ? trackListenSocket(httpContext->listen(sslCtxOrNull(), host.c_str(), port, 0)) : nullptr);
int error = 0;
auto *ls = httpContext ? trackListenSocket(httpContext->listen(sslCtxOrNull(), host.c_str(), port, 0, &error)) : nullptr;
handler(ls, ls ? 0 : error);
return std::move(*this);
}

/* Host, port, options, callback */
TemplatedApp &&listen(const std::string &host, int port, int options, MoveOnlyFunction<void(us_listen_socket_t *)> &&handler) {
TemplatedApp &&listen(const std::string &host, int port, int options, MoveOnlyFunction<void(us_listen_socket_t *, int)> &&handler) {
if (host.empty()) {
return listen(port, options, std::move(handler));
}
handler(httpContext ? trackListenSocket(httpContext->listen(sslCtxOrNull(), host.c_str(), port, options)) : nullptr);
int error = 0;
auto *ls = httpContext ? trackListenSocket(httpContext->listen(sslCtxOrNull(), host.c_str(), port, options, &error)) : nullptr;
handler(ls, ls ? 0 : error);
return std::move(*this);
}

/* Port, callback */
TemplatedApp &&listen(int port, MoveOnlyFunction<void(us_listen_socket_t *)> &&handler) {
handler(httpContext ? trackListenSocket(httpContext->listen(sslCtxOrNull(), nullptr, port, 0)) : nullptr);
TemplatedApp &&listen(int port, MoveOnlyFunction<void(us_listen_socket_t *, int)> &&handler) {
int error = 0;
auto *ls = httpContext ? trackListenSocket(httpContext->listen(sslCtxOrNull(), nullptr, port, 0, &error)) : nullptr;
handler(ls, ls ? 0 : error);
return std::move(*this);
}

/* Port, options, callback */
TemplatedApp &&listen(int port, int options, MoveOnlyFunction<void(us_listen_socket_t *)> &&handler) {
handler(httpContext ? trackListenSocket(httpContext->listen(sslCtxOrNull(), nullptr, port, options)) : nullptr);
TemplatedApp &&listen(int port, int options, MoveOnlyFunction<void(us_listen_socket_t *, int)> &&handler) {
int error = 0;
auto *ls = httpContext ? trackListenSocket(httpContext->listen(sslCtxOrNull(), nullptr, port, options, &error)) : nullptr;
handler(ls, ls ? 0 : error);
return std::move(*this);
}

/* options, callback, path to unix domain socket */
TemplatedApp &&listen(int options, MoveOnlyFunction<void(us_listen_socket_t *)> &&handler, std::string_view path) {
handler(httpContext ? trackListenSocket(httpContext->listen_unix(sslCtxOrNull(), path.data(), path.length(), options)) : nullptr);
TemplatedApp &&listen(int options, MoveOnlyFunction<void(us_listen_socket_t *, int)> &&handler, std::string_view path) {
int error = 0;
auto *ls = httpContext ? trackListenSocket(httpContext->listen_unix(sslCtxOrNull(), path.data(), path.length(), options, &error)) : nullptr;
handler(ls, ls ? 0 : error);
return std::move(*this);
}

/* callback, path to unix domain socket */
TemplatedApp &&listen(MoveOnlyFunction<void(us_listen_socket_t *)> &&handler, std::string_view path, int options) {
handler(httpContext ? trackListenSocket(httpContext->listen_unix(sslCtxOrNull(), path.data(), path.length(), options)) : nullptr);
TemplatedApp &&listen(MoveOnlyFunction<void(us_listen_socket_t *, int)> &&handler, std::string_view path, int options) {
int error = 0;
auto *ls = httpContext ? trackListenSocket(httpContext->listen_unix(sslCtxOrNull(), path.data(), path.length(), options, &error)) : nullptr;
handler(ls, ls ? 0 : error);
return std::move(*this);
}

Expand Down
18 changes: 11 additions & 7 deletions packages/bun-uws/src/HttpContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -676,27 +676,31 @@ struct HttpContext {
}, priority);
}

/* Listen to port using this HttpContext. ssl_ctx may be nullptr for plain HTTP. */
us_listen_socket_t *listen(struct ssl_ctx_st *sslCtx, const char *host, int port, int options) {
int error = 0;
/* Listen to port using this HttpContext. ssl_ctx may be nullptr for plain HTTP.
* On failure, *error (if non-null) receives the errno from the bind/listen syscall
* so callers can distinguish EADDRINUSE, EACCES, EADDRNOTAVAIL, etc. */
us_listen_socket_t *listen(struct ssl_ctx_st *sslCtx, const char *host, int port, int options, int *error = nullptr) {
int local_error = 0;
/* HTTP clients always send first (the request, or ClientHello for TLS), so defer
* accept() until data arrives and dispatch the read immediately after accept. */
auto socket = us_socket_group_listen(&group, socketKind(), sslCtx, host, port, options | LIBUS_LISTEN_DEFER_ACCEPT, sizeof(HttpResponseData<SSL>), &error);
auto socket = us_socket_group_listen(&group, socketKind(), sslCtx, host, port, options | LIBUS_LISTEN_DEFER_ACCEPT, sizeof(HttpResponseData<SSL>), &local_error);
// we dont depend on libuv ref for keeping it alive
if (socket) {
us_socket_unref(&socket->s);
}
if (error) *error = local_error;
return socket;
}

/* Listen to unix domain socket using this HttpContext */
us_listen_socket_t *listen_unix(struct ssl_ctx_st *sslCtx, const char *path, size_t pathlen, int options) {
int error = 0;
auto* socket = us_socket_group_listen_unix(&group, socketKind(), sslCtx, path, pathlen, options, sizeof(HttpResponseData<SSL>), &error);
us_listen_socket_t *listen_unix(struct ssl_ctx_st *sslCtx, const char *path, size_t pathlen, int options, int *error = nullptr) {
int local_error = 0;
auto* socket = us_socket_group_listen_unix(&group, socketKind(), sslCtx, path, pathlen, options, sizeof(HttpResponseData<SSL>), &local_error);
// we dont depend on libuv ref for keeping it alive
if (socket) {
us_socket_unref(&socket->s);
}
if (error) *error = local_error;

return socket;
}
Expand Down
Loading
Loading