Repository navigation
Conversation
The handlers raised a login error as soon as FreeTDS reported it. The raise longjmped out of dbopen() and leaked the half-open connection (70 KB to 150 KB for each failed attempt). Keep the first login error. Raise it after dbopen() returns. Close the connection before the raise when the server negotiates a TDS version older than 7.3.
| typedef struct { | ||
| int occurred; | ||
| tinytds_errordata error; | ||
| } tinytds_login_error; |
There was a problem hiding this comment.
I am wondering if you could simplify that to just tinytds_errordata. because if that is not not NULL when returning from dbopen, then an error occured, otherwise not.
There was a problem hiding this comment.
Done in ae50c59: the wrapper struct is gone, the slot is a plain tinytds_errordata.
A pointer alone cannot carry both facts, though. The handlers use the static pointer to know that a login is in progress, so it must be set before dbopen(), even when no error comes. A handler without userdata outside dbopen() (for example from dblogin()) must still raise at once. Thus the slot uses is_message = 1 as "empty": the handlers keep errors only, never info messages.
While testing this, I found a bug in my first commit. For SYBETIME, the handler returned INT_TIMEOUT. During the login, FreeTDS answers INT_TIMEOUT with a cancel and waits again (tdserror: returning TDS_INT_CONTINUE), so a 1 s login timeout took about 100 s. The handler now returns INT_CANCEL for every deferred login error, and the login timeout test checks the duration.
Drop the tinytds_login_error wrapper: the slot is a plain tinytds_errordata. The handlers keep errors only, so is_message = 1 marks an empty slot. Return INT_CANCEL for a deferred login error. During the login, FreeTDS answers INT_TIMEOUT with a cancel and waits again, so a 1 s login timeout took about 100 s. The login timeout test now checks the duration.
Problem
rb_tinytds_connectcallsdbopen(), anddbopen()does the login. During the login, theDBPROCESShas no userdata yet. When the login fails, FreeTDS callstinytds_err_handlerortinytds_msg_handler. These handlers find no userdata, so they raise at once.A raise from C is a
longjmp: it leavesdbopen()before FreeTDS can clean up. Thus FreeTDS never frees the half-open connection:DBPROCESS,tds_alloc_socket,tds_init_connection),dbperror,tds_alloc_client_sqlstate).Each failed attempt leaks 70 KB to 150 KB. A client that retries against a server that is not available loses this memory on each retry.
A related leak is in the same function. If the server negotiates a TDS version older than 7.3,
rb_tinytds_connectraises afterdbopen()returns a connection. At that pointcwrap->closedis still 1, so the free function does not calldbclose(). The open connection leaks.Fix
dbopen(), the handlers keep the first error in atinytds_login_errorstruct. The struct is on the stack ofrb_tinytds_connect. The handlers return to FreeTDS and do not raise. FreeTDS then stops the login and frees its objects.dbopen()returns,rb_tinytds_connectraises the kept error. Ifdbopen()returned a connection, the function closes it first.rb_tinytds_connectcloses the connection.The raised exception does not change. It is the first error that the handlers receive, with the same message, source, severity,
db_error_numberandos_error_number. During the login, info messages stay ignored, as before. Duringdbopen(), the error handler returnsINT_CANCEL, also forSYBETIME. FreeTDS answersINT_TIMEOUTwith a cancel and waits again, so onlyINT_CANCELstops the login. The login timeout test checks that the error comes before 10 seconds.The pointer to the struct is a static variable.
dbopen()runs with the GVL held, so only one login is in progress at a time. A handler reads the pointer only when itsDBPROCESShas no userdata.rb_tinytds_connectmust build the exception without aDBPROCESS. Thus this PR moves the creation of theTinyTds::Errorobject fromrb_tinytds_raise_errorinto a newrb_tinytds_new_errorfunction.rb_tinytds_raise_errorcalls it and keeps its behavior.Measurements
The tool is ruby_memcheck 3.0.1. It runs Ruby under Valgrind and removes the false positives that Ruby causes. A "definitely lost" block is a block with no pointer to it at exit.
mastertds_version: "7.1"tds_version: "7.1"On
master, each attempt adds about 82 KB (wrong username), 71 KB (connection refused) or 148 KB (TDS 7.1). With this PR, the total stays the same for any number of attempts. The 32,879 bytes that remain come from the one-timedbinitallocations (the FreeTDS context).Reproduction script
Validation
bundle exec rake test: 124 tests, 538 assertions, 0 failures, 0 errors.SYBETIMEthrough toxiproxy).dbclose()call, and it checks that a new connection works after the error.binary_name: "tiny_tds") shows no invalid read or write, and no leak fromdbopen(). The remaining 4,136-byte blocks come fromnogvl_setup. Free the non-blocking error array before a raise #613 fixes them. With both PRs, only the one-timedbinitblocks remain.astyle --options=astyle.confdoes not change the C files.standardrbreports no offense intest/client_test.rb.Environment: Ruby 4.0.7 on Linux aarch64, FreeTDS 1.5.19 built from source, SQL Server 2022 CU27 (
mcr.microsoft.com/mssql/server:2022-latest), Valgrind 3.24.0.Note
The draft
v4branch (#603) keeps the samedbopen()call and the same handlers. That branch has the same leaks.