Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Plug memory leaks from failed connections #144

Merged
merged 2 commits into from
Jan 4, 2024
Merged
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
9 changes: 5 additions & 4 deletions contrib/ruby/ext/trilogy-ruby/cext.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,7 @@ static void mark_trilogy(void *ptr)
static void free_trilogy(void *ptr)
{
struct trilogy_ctx *ctx = ptr;
if (ctx->conn.socket != NULL) {
trilogy_free(&ctx->conn);
}
trilogy_free(&ctx->conn);
xfree(ptr);
}

Expand Down Expand Up @@ -309,15 +307,18 @@ static int try_connect(struct trilogy_ctx *ctx, trilogy_handshake_t *handshake,
int rc = args.rc;

if (rc != TRILOGY_OK) {
trilogy_sock_close(sock);
return rc;
}

/* replace the default wait callback with our GVL-aware callback so we can
escape the GVL on each wait operation without going through call_without_gvl */
sock->wait_cb = _cb_ruby_wait;
rc = trilogy_connect_send_socket(&ctx->conn, sock);
if (rc < 0)
if (rc < 0) {
trilogy_sock_close(sock);
return rc;
}

while (1) {
rc = trilogy_connect_recv(&ctx->conn, handshake);
Expand Down
6 changes: 5 additions & 1 deletion contrib/ruby/ext/trilogy-ruby/extconf.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,12 @@

# concatenate trilogy library sources to allow the compiler to optimise across
# source files

trilogy_src_dir = File.realpath("src", __dir__)
File.binwrite("trilogy.c",
Dir["#{__dir__}/src/**/*.c"].map { |src| File.binread(src) }.join)
Dir["#{trilogy_src_dir}/**/*.c"].map { |src|
%{#line 1 "#{src}"\n} + File.binread(src)
}.join)

$objs = %w[trilogy.o cast.o cext.o]
$CFLAGS << " -I #{__dir__}/inc -std=gnu99 -fvisibility=hidden"
Expand Down
7 changes: 6 additions & 1 deletion src/buffer.c
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,9 @@ int trilogy_buffer_putc(trilogy_buffer_t *buffer, uint8_t c)
return TRILOGY_OK;
}

void trilogy_buffer_free(trilogy_buffer_t *buffer) { free(buffer->buff); }
void trilogy_buffer_free(trilogy_buffer_t *buffer)
{
free(buffer->buff);
buffer->buff = NULL;
buffer->len = buffer->cap = 0;
}