Skip to content

Consider handling error code 40613 with stretched timeout with default retry logic provider #1957

Open

Description

@cheenamalhotra

Describe the bug

When connecting to a serverless database, it's reasonable to receive error code 40613 as the database resumes. But resuming can take as long as ~45 seconds, in which case driver generally times out even with the default Retry logic provider.

Exception message:  SqlError.ctor | ERR | Info Number 40613, Error State 1, Error Class 20, 
Error Message 'Database '<>' on server '<>.database.windows.net' is not currently available.  
Please retry the connection later. 

To reproduce

Connect to a serverless database when it's paused.

Proposal

Since the driver receives the error code with the default retry logic provider, the driver could provide a log (Waiting for database to resume) and retry connection after some time (additional 30 seconds delay) - so it can resume and successfully connect in the next connection attempt.

Most users would like to not have to worry about this use case, even if it takes a bit longer to wait for database to resume.

Activity

  1. lcheunglci commented on Mar 17, 2023

    @lcheunglci
    Contributor

    Thanks @cheenamalhotra for identifying this issue and making a suggestion to provide a better user experience. We'll discuss it with the team regarding this enhancement.

  2. David-Engel commented on Mar 21, 2023

    @David-Engel
    Contributor

    How would we differentiate 40613 between a database that is paused and one that truly doesn't exist? (They are still the same error, right?) I wouldn't want to implement an extra long pause for scenarios where the database doesn't exist...

  3. cheenamalhotra commented on Mar 21, 2023

    @cheenamalhotra
    MemberAuthor

    For a paused database (not resuming), we get below error:
    (tested with paused SQL instance of Azure Synapse dedicated pool)

    Microsoft.Data.SqlClient.SqlException (0x80131904): Cannot connect to database when it is paused.
       at Microsoft.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)
    ...
    ClientConnectionId:554ec90f-cf32-40f9-92c2-bae917b1962e
    Error Number:40892,State:1,Class:20
    

    Whereas for a non-existing database we get:

    Microsoft.Data.SqlClient.SqlException (0x80131904): Login failed for user '<token-identified principal>'.
       at Microsoft.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)
    ...
    ClientConnectionId:80148e7d-9e4b-46c0-9ae5-62692e2f7fc8
    Error Number:18456,State:1,Class:14
    
  4. cheenamalhotra commented on Mar 21, 2023

    @cheenamalhotra
    MemberAuthor

    Reading more about it, the other condition 40613 may occur would be when "there is already an existing dedicated administrator connection (DAC) established to the database." explained here.

    It would be a valid error in that case, and if increasing timeout, it will fail in timeout +30 seconds (with default retry logic provider). But I think serverless connections are more prominently used than DAC connections, which leads to error been thrown with default timeout.

  5. cheenamalhotra commented on Jul 27, 2023

    @cheenamalhotra
    MemberAuthor

    Reading more about it, the other condition 40613 may occur would be when "there is already an existing dedicated administrator connection (DAC) established to the database." explained here.

    Tested again today and noticed that second DAC connection attempt fails with below error now:

    Unhandled exception. Microsoft.Data.SqlClient.SqlException (0x80131904): A connection was successfully established with the server, but then an error occurred during the pre-login handshake. 
    (provider: TCP Provider, error: 0 - An existing connection was forcibly closed by the remote host.)
     ---> System.ComponentModel.Win32Exception (10054): An existing connection was forcibly closed by the remote host.
    
  6. cheenamalhotra commented on Nov 22, 2023

    @cheenamalhotra
    MemberAuthor

    Tested again today and noticed that second DAC connection attempt fails with below error now:

    Unhandled exception. Microsoft.Data.SqlClient.SqlException (0x80131904): A connection was successfully established with the server, but then an error occurred during the pre-login handshake. 
    (provider: TCP Provider, error: 0 - An existing connection was forcibly closed by the remote host.)
     ---> System.ComponentModel.Win32Exception (10054): An existing connection was forcibly closed by the remote host.
    

    Just an update, this seems to be resolved now, and doesn't happen anymore - the docs likely need updating. 40613 is now solely for sleeping serverless connection error.

  7. moved this from To triage to Backlog in SqlClient Boardon Jan 6, 2026
  8. priyankatiwari08 commented on Sep 17, 2026

    @priyankatiwari08
    Contributor

    /triage

  9. github-actions commented on Sep 17, 2026

    @github-actions

    test simple comment

    Generated by SqlClient Issue Auto-Triage for #1957 · copilot · auto · 46.7 AIC · ⌖ 13 AIC · ⊞ 11.9K · ◷

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

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions