Skip to content

Timeouts not fully applied when opening connections #3227

Closed

Description

@roji

When opening a connection, the socket connection process (Connect) is covered by timeouts, but all subsequent I/O isn't (e.g. authentication).

See #3226, /cc @russellfoster @vonzshik

Activity

  1. added this to the 5.0.0 milestone on Oct 10, 2020
  2. self-assigned this
    on Oct 10, 2020
  3. roji commented on Oct 10, 2020

    @roji
    MemberAuthor

    @russellfoster can you please post a comment here so I can co-assign you for contribution credit?

  4. russellfoster commented on Oct 11, 2020

    @russellfoster

    Hi, Thanks for this. So does this mean you no longer need me to redo the pull request?

  5. added a commit that references this issue on Oct 12, 2020
    bfdacc8
  6. roji commented on Oct 12, 2020

    @roji
    MemberAuthor

    Yeah, that's right - I'm sorry your PR didn't make it in, but a more significant redo was needed (including for the test mocking infrastructure)... And raising an issue is just as important as fixing it. In any case, I'm co-assigning you for credit on this issue.

  7. added a commit that references this issue on Oct 12, 2020
    e780f90
  8. russellfoster commented on Oct 12, 2020

    @russellfoster

    No problem, just glad it is fixed. So, will this be backported? We are on 3.2.7 and I see several breaking changes in 4.x (timezones, numeric data types) that may mean we can't migrate at this time.

    Thanks for all of the attention to this issue!

  9. roji commented on Oct 12, 2020

    @roji
    MemberAuthor

    #3229 definitely can't be backported as-is, but specifically the part that sets the timeout might be. But I'm curious - this is supposed to be a rather rare issue - with read hanging specifically during connection/authentication. I'm curious - is this recurring on your case? If so, any idea why?

  10. russellfoster commented on Oct 12, 2020

    @russellfoster

    No, I really hope it is rare. It's a customer issue. Not sure how they got into this situation, could be some bad VM settings on the database server, but will need to figure that out as well. There is a fallback mechanism for the database going offline, but this was never reached. Probably need to add a watchdog for this as well.

  11. russellfoster commented on Oct 12, 2020

    @russellfoster

    You may get a kick out of this. I did one time consider this helper to class to hack around this issue:

        public static class ConnectionHelper
        {
            private static Lazy<PropertyInfo> connectorProperty = new Lazy<PropertyInfo>(() => typeof(NpgsqlConnection).GetProperty("Connector", BindingFlags.Instance | BindingFlags.NonPublic));
    
            public static async Task OpenWithTimeoutAsync(DbConnection connection, TimeSpan timeout)
            {
                using (CancellationTokenSource cancellationTokenSource = new CancellationTokenSource())
                {
                    Task killTask = KillConnectionAsync(connection, timeout, cancellationTokenSource.Token);
    
                    try
                    {
                        await connection.OpenAsync();
                    }
                    finally
                    {
                        cancellationTokenSource.Cancel();
    
                        await killTask;
                    }
                }
            }
    
            public static async Task KillConnectionAsync(DbConnection connection, TimeSpan timeout, CancellationToken cancelToken)
            {
                NpgsqlConnection npgsqlConnection = connection as NpgsqlConnection;     // do this to maintain CLS compliance
    
                if (npgsqlConnection != null)
                {
                    await Task.Delay(timeout, cancelToken);
    
                    if (!cancelToken.IsCancellationRequested)
                    {
                        await Task.Run(() => BreakConnection(npgsqlConnection));
                    }
                }
            }
    
            private static void BreakConnection(NpgsqlConnection connection)
            {
                object connector = connectorProperty.Value?.GetValue(connection);
                MethodInfo breakMethod = connector?.GetType().GetMethod("Break", BindingFlags.Instance | BindingFlags.NonPublic);
    
                breakMethod?.Invoke(connector, null);
            }
        }
    

    Still needed work to cache more of the reflection stuff. Anyways, not easy to use with Entity Framework and very ugly ha!

  12. roji commented on Oct 13, 2020

    @roji
    MemberAuthor

    Ha, your helper resembles this hack we use to fake a timeout in older .NET TFMs where sockets don't support cancellations or timeouts on async operations...

    Regardless, your helper does suffer from a concurrency issue - depending on when Break gets called, it may cause undefined behavior as we don't exactly support concurrent operations on the same connector this way...

  13. added theissue type on Jun 18, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

No labels
No labels

Type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions