Skip to content
Open
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
## (unreleased)

* Free the errors that a non-blocking call collects before the extension raises them. Each SQL Server error no longer leaks about 4 KB of native memory.

## 3.4.1

* Register the C globals that hold Ruby classes and modules with the GC, so raising `TinyTds::Error` after GC compaction no longer crashes with a segmentation fault. Fixes #608.
Expand Down
26 changes: 19 additions & 7 deletions ext/tiny_tds/result.c
Original file line number Diff line number Diff line change
Expand Up @@ -121,16 +121,30 @@ static void nogvl_cleanup(DBPROCESS *client)
exceptions based on errors from SQL Server.
*/
short int i;
short int errors_length = userdata->nonblocking_errors_length;

for (i = 0; i < userdata->nonblocking_errors_length; i++) {
tinytds_errordata error = userdata->nonblocking_errors[i];
/*
A raise longjmps out of this function and skips the code after it.
Thus copy the errors into a buffer that Ruby manages, and free the malloc'd array first.
RB_ALLOCV_N registers the buffer with the GC: if a raise skips RB_ALLOCV_END, the GC frees it.
*/
VALUE errors_buffer;
tinytds_errordata *errors = RB_ALLOCV_N(tinytds_errordata, errors_buffer, errors_length);
MEMCPY(errors, userdata->nonblocking_errors, tinytds_errordata, errors_length);
free(userdata->nonblocking_errors);
userdata->nonblocking_errors = NULL;
userdata->nonblocking_errors_length = 0;
userdata->nonblocking_errors_size = 0;

for (i = 0; i < errors_length; i++) {
tinytds_errordata error = errors[i];

// lookahead to drain any info messages ahead of raising error
if (!error.is_message) {
short int j;

for (j = i; j < userdata->nonblocking_errors_length; j++) {
tinytds_errordata msg_error = userdata->nonblocking_errors[j];
for (j = i; j < errors_length; j++) {
tinytds_errordata msg_error = errors[j];

if (msg_error.is_message) {
rb_tinytds_raise_error(client, msg_error);
Expand All @@ -141,9 +155,7 @@ static void nogvl_cleanup(DBPROCESS *client)
rb_tinytds_raise_error(client, error);
}

free(userdata->nonblocking_errors);
userdata->nonblocking_errors_length = 0;
userdata->nonblocking_errors_size = 0;
RB_ALLOCV_END(errors_buffer);
}

static RETCODE nogvl_dbsqlok(DBPROCESS *client)
Expand Down
Loading