Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
Jarred-Sumner authored Feb 6, 2025
1 parent 1684c62 commit 2770eca
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 8 deletions.
30 changes: 24 additions & 6 deletions packages/bun-usockets/src/crypto/openssl.c
Original file line number Diff line number Diff line change
Expand Up @@ -1589,22 +1589,40 @@ struct us_listen_socket_t *us_internal_ssl_socket_context_listen_unix(
socket_ext_size, error);
}

// https://github.com/oven-sh/bun/issues/16995
static void us_internal_zero_ssl_data_for_connected_socket_before_onopen(struct us_internal_ssl_socket_t *s) {
s->ssl = NULL;
s->ssl_write_wants_read = 0;
s->ssl_read_wants_write = 0;
s->fatal_error = 0;
s->handshake_state = HANDSHAKE_PENDING;
}

// TODO does this need more changes?
struct us_connecting_socket_t *us_internal_ssl_socket_context_connect(
struct us_socket_t *us_internal_ssl_socket_context_connect(
struct us_internal_ssl_socket_context_t *context, const char *host,
int port, int options, int socket_ext_size, int* is_connected) {
return us_socket_context_connect(
int port, int options, int socket_ext_size, int* is_connecting) {
struct us_internal_ssl_socket_t *s = (struct us_internal_ssl_socket_t *)us_socket_context_connect(
2, &context->sc, host, port, options,
sizeof(struct us_internal_ssl_socket_t) - sizeof(struct us_socket_t) +
socket_ext_size, is_connected);
socket_ext_size, is_connecting);
if (*is_connecting) {
us_internal_zero_ssl_data_for_connected_socket_before_onopen(s);
}

return (struct us_socket_t*)s;
}
struct us_internal_ssl_socket_t *us_internal_ssl_socket_context_connect_unix(
struct us_socket_t *us_internal_ssl_socket_context_connect_unix(
struct us_internal_ssl_socket_context_t *context, const char *server_path,
size_t pathlen, int options, int socket_ext_size) {
return (struct us_internal_ssl_socket_t *)us_socket_context_connect_unix(
struct us_socket_t *s = (struct us_socket_t *)us_socket_context_connect_unix(
0, &context->sc, server_path, pathlen, options,
sizeof(struct us_internal_ssl_socket_t) - sizeof(struct us_socket_t) +
socket_ext_size);
if (s) {
us_internal_zero_ssl_data_for_connected_socket_before_onopen((struct us_internal_ssl_socket_t*) s);
}
return s;
}

static void ssl_on_open_without_sni(struct us_internal_ssl_socket_t *s, int is_client, char *ip, int ip_length) {
Expand Down
4 changes: 2 additions & 2 deletions packages/bun-usockets/src/internal/internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -400,11 +400,11 @@ struct us_listen_socket_t *us_internal_ssl_socket_context_listen_unix(
us_internal_ssl_socket_context_r context, const char *path,
size_t pathlen, int options, int socket_ext_size, int* error);

struct us_connecting_socket_t *us_internal_ssl_socket_context_connect(
struct us_socket_t *us_internal_ssl_socket_context_connect(
us_internal_ssl_socket_context_r context, const char *host,
int port, int options, int socket_ext_size, int* is_resolved);

struct us_internal_ssl_socket_t *us_internal_ssl_socket_context_connect_unix(
struct us_socket_t *us_internal_ssl_socket_context_connect_unix(
us_internal_ssl_socket_context_r context, const char *server_path,
size_t pathlen, int options, int socket_ext_size);

Expand Down
8 changes: 8 additions & 0 deletions test/js/web/websocket/websocket.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -817,3 +817,11 @@ describe("websocket in subprocess", () => {
expect(await subprocess.exited).toBe(0);
});
});

it("#16995", async () => {
const publicAddress = new URL("https://1.1.1.1:3000");
for (let i = 0; i < 4096; i++) {
const socket = new WebSocket(publicAddress.toString());
socket.close();
}
});

0 comments on commit 2770eca

Please sign in to comment.