You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@metaskills as I mentioned in the previous comment, other db gems do behave differently - they don't block other threads, but tinytds blocks other threads
My bad, I missed that. I wonder if that is something we can fix at our bindings to FreeTDS. I see we removed at one time the rb_thread_blocking_region and used rb_thread_blocking_region which leads back to this commit (3eb33c62). Maybe our error handler needs to block? Do you see anything that could be changed?
I've tried to implement this for tiny_tds v4 and ran into trouble.
Under the hood, the C code of tiny_tds and the FreeTDS library share a construct named userdata. When tiny_tds hands off the GVL, FreeTDS communicates issues to this userdata construct. When the GVL is re-acquired, the state of this userdata is inspected and any eventual errors are raised. It is important to know that we cannot access resources in Ruby when handing off the GVL, which is needed to support non-blocking threads during connection attempts.
The other thing to know is that FreeTDS errors are communicated to our code via a callback function. In this callback function, we have access to the DBPROCESS object. The DBPROCESS object is what FreeTDS returns after the connection is initialised. The main problem now is that we can only communicate the pointer to our userdata construct when we have a DBPROCESS project. So while FreeTDS might call our callback function with issues during the connection setup, we have not yet had a chance to tell it the pointer to userdata. And so we don't have a way to communicate errors back to Ruby.
I also initially tried using rb_thread_call_with_gvl, but the Ruby documentation warns about undefined behaviour when raising errors within the called method (which is exactly what we would have to do).
There is likely also some C shenanigans to store a global variable using the userdata variable pointer, but I am not proficient enough in C or Ruby C to say whether this has unintended side effects.
So, for the sake of system integrity / avoiding undefined behaviour, I would close this issue as I can't implement an optimisation in this area.
Note: I also quickly looked into the pg gem code. There, the result of the “connection start” function indicates whether the connection was successfully established. Which would be ideal for us as well, if FreeTDS worked that way.
Activity
metaskills commentedon Nov 28, 2018
I would think expected behavior for Ruby and compared to other DB connection gems. You can lower the connect timeout if you want.
ebeigarts commentedon Nov 28, 2018
@metaskills we will probably lower the
connect_timeoutfor now, or skip using threads.At least
pgdoesn't block - https://gist.github.com/08c1b336952df04f0e4645f376d995dcAnd
mysql2also doesn't block - https://gist.github.com/866e58a6fbc3cfda3f2a7268607c49aametaskills commentedon Nov 28, 2018
Cool, if you find that other DB connection gems behave differently, please do let us know so we can make this better.
ebeigarts commentedon Nov 28, 2018
@metaskills as I mentioned in the previous comment, other db gems do behave differently - they don't block other threads, but tinytds blocks other threads
metaskills commentedon Nov 28, 2018
My bad, I missed that. I wonder if that is something we can fix at our bindings to FreeTDS. I see we removed at one time the
rb_thread_blocking_regionand usedrb_thread_blocking_regionwhich leads back to this commit (3eb33c62). Maybe our error handler needs to block? Do you see anything that could be changed?andyundso commentedon Dec 13, 2025
I've tried to implement this for
tiny_tdsv4 and ran into trouble.Under the hood, the C code of
tiny_tdsand the FreeTDS library share a construct nameduserdata. Whentiny_tdshands off the GVL, FreeTDS communicates issues to thisuserdataconstruct. When the GVL is re-acquired, the state of thisuserdatais inspected and any eventual errors are raised. It is important to know that we cannot access resources in Ruby when handing off the GVL, which is needed to support non-blocking threads during connection attempts.The other thing to know is that FreeTDS errors are communicated to our code via a callback function. In this callback function, we have access to the
DBPROCESSobject. TheDBPROCESSobject is what FreeTDS returns after the connection is initialised. The main problem now is that we can only communicate the pointer to ouruserdataconstruct when we have aDBPROCESSproject. So while FreeTDS might call our callback function with issues during the connection setup, we have not yet had a chance to tell it the pointer touserdata. And so we don't have a way to communicate errors back to Ruby.I also initially tried using
rb_thread_call_with_gvl, but the Ruby documentation warns about undefined behaviour when raising errors within the called method (which is exactly what we would have to do).There is likely also some C shenanigans to store a global variable using the userdata variable pointer, but I am not proficient enough in C or Ruby C to say whether this has unintended side effects.
So, for the sake of system integrity / avoiding undefined behaviour, I would close this issue as I can't implement an optimisation in this area.
Note: I also quickly looked into the pg gem code. There, the result of the “connection start” function indicates whether the connection was successfully established. Which would be ideal for us as well, if FreeTDS worked that way.