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.
3 tasks done
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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. ForSYBETIME, the handler now returnsINT_TIMEOUTto FreeTDS, which stops the login.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.