Skip to content

Commit

Permalink
Don't risk double uv_close()
Browse files Browse the repository at this point in the history
  • Loading branch information
kasperisager committed Dec 10, 2024
1 parent b258210 commit a30b87f
Showing 1 changed file with 20 additions and 2 deletions.
22 changes: 20 additions & 2 deletions binding.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,10 @@ typedef struct {
js_ref_t *on_end;
js_ref_t *on_close;

js_deferred_teardown_t *teardown;
bool closing;
bool exiting;

js_deferred_teardown_t *teardown;
} bare_tcp_t;

static void
Expand All @@ -34,6 +36,8 @@ bare_tcp__on_connection(uv_stream_t *server, int status) {

bare_tcp_t *tcp = (bare_tcp_t *) server;

if (tcp->closing) return;

js_env_t *env = tcp->env;

js_handle_scope_t *scope;
Expand Down Expand Up @@ -78,6 +82,8 @@ bare_tcp__on_connect(uv_connect_t *req, int status) {

bare_tcp_t *tcp = (bare_tcp_t *) req->data;

if (tcp->exiting) return;

js_env_t *env = tcp->env;

js_handle_scope_t *scope;
Expand Down Expand Up @@ -125,6 +131,8 @@ bare_tcp__on_read(uv_stream_t *stream, ssize_t nread, const uv_buf_t *buf) {

bare_tcp_t *tcp = (bare_tcp_t *) stream;

if (tcp->exiting) return;

js_env_t *env = tcp->env;

js_handle_scope_t *scope;
Expand Down Expand Up @@ -175,6 +183,8 @@ bare_tcp__on_write(uv_write_t *req, int status) {

bare_tcp_t *tcp = (bare_tcp_t *) req->data;

if (tcp->exiting) return;

js_env_t *env = tcp->env;

js_handle_scope_t *scope;
Expand Down Expand Up @@ -219,6 +229,8 @@ bare_tcp__on_shutdown(uv_shutdown_t *req, int status) {

bare_tcp_t *tcp = (bare_tcp_t *) req->data;

if (tcp->exiting) return;

js_env_t *env = tcp->env;

js_handle_scope_t *scope;
Expand Down Expand Up @@ -315,6 +327,8 @@ bare_tcp__on_teardown(js_deferred_teardown_t *handle, void *data) {

tcp->exiting = true;

if (tcp->closing) return;

uv_close((uv_handle_t *) &tcp->handle, bare_tcp__on_close);
}

Expand All @@ -338,7 +352,8 @@ bare_tcp_init(js_env_t *env, js_callback_info_t *info) {
assert(argc == 8);

uv_loop_t *loop;
js_get_env_loop(env, &loop);
err = js_get_env_loop(env, &loop);
assert(err == 0);

js_value_t *handle;

Expand All @@ -354,6 +369,7 @@ bare_tcp_init(js_env_t *env, js_callback_info_t *info) {
}

tcp->env = env;
tcp->closing = false;
tcp->exiting = false;

err = js_get_typedarray_info(env, argv[0], NULL, (void **) &tcp->read.base, (size_t *) &tcp->read.len, NULL, NULL);
Expand Down Expand Up @@ -708,6 +724,8 @@ bare_tcp_close(js_env_t *env, js_callback_info_t *info) {
err = js_get_arraybuffer_info(env, argv[0], (void **) &tcp, NULL);
assert(err == 0);

tcp->closing = true;

uv_close((uv_handle_t *) &tcp->handle, bare_tcp__on_close);

return NULL;
Expand Down

0 comments on commit a30b87f

Please sign in to comment.