Skip to content

TinyTds::Client.new blocks other threads #421

Closed as not planned
Closed as not planned

Description

@ebeigarts

Environment

ProductName: Mac OS X
ProductVersion: 10.13.5
BuildVersion: 17F77
TinyTDS Version: 2.1.2
FreeTDS Version: 1.00.104
Ruby: 2.3.1

Description

TinyTds::Client.new blocks the other threads for 60s if the mssql server is not reachable. Am I missing something or this is the expected behavior?

Example:

https://gist.github.com/ebeigarts/85c5d6603f8dab78b04addcff2ef3d0c

ruby 2.3.1
tiny_tds 2.1.2
MAIN: 2018-11-28 13:20:17 +0200
THR1: 2018-11-28 13:20:17 +0200
THR2: 2018-11-28 13:20:17 +0200 START
MAIN: 2018-11-28 13:20:22 +0200
THR2: 2018-11-28 13:20:22 +0200 FAIL Unable to connect: Adaptive Server is unavailable or does not exist (10.193.15.203:1433)
THR1: 2018-11-28 13:20:22 +0200
MAIN: 2018-11-28 13:20:23 +0200
THR1: 2018-11-28 13:20:23 +0200
MAIN: 2018-11-28 13:20:24 +0200
THR1: 2018-11-28 13:20:24 +0200
MAIN: 2018-11-28 13:20:25 +0200
THR1: 2018-11-28 13:20:25 +0200
THR1: 2018-11-28 13:20:26 +0200
MAIN: 2018-11-28 13:20:26 +0200
THR1: 2018-11-28 13:20:27 +0200
MAIN: 2018-11-28 13:20:27 +0200
THR1: 2018-11-28 13:20:28 +0200
MAIN: 2018-11-28 13:20:28 +0200
MAIN: 2018-11-28 13:20:29 +0200
THR1: 2018-11-28 13:20:29 +0200
THR1: 2018-11-28 13:20:30 +0200
MAIN: 2018-11-28 13:20:30 +0200

Activity

  1. metaskills commented on Nov 28, 2018

    @metaskills
    Member

    I would think expected behavior for Ruby and compared to other DB connection gems. You can lower the connect timeout if you want.

  2. ebeigarts commented on Nov 28, 2018

    @ebeigarts
    Author

    @metaskills we will probably lower the connect_timeout for now, or skip using threads.

    At least pg doesn't block - https://gist.github.com/08c1b336952df04f0e4645f376d995dc

    ruby 2.3.1
    pg
    THR2: 2018-11-28 14:56:57 +0200 START
    THR1: 2018-11-28 14:56:57 +0200
    MAIN: 2018-11-28 14:56:57 +0200
    THR1: 2018-11-28 14:56:58 +0200
    MAIN: 2018-11-28 14:56:58 +0200
    THR1: 2018-11-28 14:56:59 +0200
    MAIN: 2018-11-28 14:56:59 +0200
    MAIN: 2018-11-28 14:57:00 +0200
    THR1: 2018-11-28 14:57:00 +0200
    MAIN: 2018-11-28 14:57:01 +0200
    THR1: 2018-11-28 14:57:01 +0200
    THR2: 2018-11-28 14:57:02 +0200 FAIL timeout expired
    THR1: 2018-11-28 14:57:02 +0200
    MAIN: 2018-11-28 14:57:02 +0200
    THR1: 2018-11-28 14:57:04 +0200
    MAIN: 2018-11-28 14:57:04 +0200
    MAIN: 2018-11-28 14:57:05 +0200
    THR1: 2018-11-28 14:57:05 +0200
    THR1: 2018-11-28 14:57:06 +0200
    MAIN: 2018-11-28 14:57:06 +0200
    MAIN: 2018-11-28 14:57:07 +0200
    THR1: 2018-11-28 14:57:07 +0200
    

    And mysql2 also doesn't block - https://gist.github.com/866e58a6fbc3cfda3f2a7268607c49aa

    ruby 2.3.1
    mysql2
    THR1: 2018-11-28 15:05:52 +0200
    THR2: 2018-11-28 15:05:52 +0200 START
    MAIN: 2018-11-28 15:05:52 +0200
    THR1: 2018-11-28 15:05:53 +0200
    MAIN: 2018-11-28 15:05:53 +0200
    THR1: 2018-11-28 15:05:54 +0200
    MAIN: 2018-11-28 15:05:54 +0200
    MAIN: 2018-11-28 15:05:55 +0200
    THR1: 2018-11-28 15:05:55 +0200
    MAIN: 2018-11-28 15:05:56 +0200
    THR1: 2018-11-28 15:05:56 +0200
    THR2: 2018-11-28 15:05:57 +0200 FAIL Can't connect to MySQL server on '10.193.15.203' (60)
    MAIN: 2018-11-28 15:05:57 +0200
    THR1: 2018-11-28 15:05:57 +0200
    MAIN: 2018-11-28 15:05:58 +0200
    THR1: 2018-11-28 15:05:58 +0200
    MAIN: 2018-11-28 15:05:59 +0200
    THR1: 2018-11-28 15:05:59 +0200
    THR1: 2018-11-28 15:06:00 +0200
    MAIN: 2018-11-28 15:06:00 +0200
    THR1: 2018-11-28 15:06:01 +0200
    MAIN: 2018-11-28 15:06:01 +0200
    
  3. metaskills commented on Nov 28, 2018

    @metaskills
    Member

    Cool, if you find that other DB connection gems behave differently, please do let us know so we can make this better.

  4. ebeigarts commented on Nov 28, 2018

    @ebeigarts
    Author

    @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

  5. metaskills commented on Nov 28, 2018

    @metaskills
    Member

    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?

  6. andyundso commented on Dec 13, 2025

    @andyundso
    Member

    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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions