none
ADO.NET hangs RRS feed

  • Question

  • I wrote program on .net 2.0 C#. It opens connection to MS SQL Server 2005 and makes some queries (using SqlCommand.ExecuteNonQuery, SqlCommand.ExecuteReader) and then disconnect.
    If network fails in the middle of job (I disconnect it manually) the program hangs for infinite time. The Program doesn’t generate any exceptions. It loops in system.data.dll and does not return control to managed code.
    I can only kill process in task manager. Thread.Abort hangs calling thread.
    This problem occurs only on Windows 2003 server.

    If I run this program on Windows XP/2000 when I disconnect network it generates normal exception: A transport-level error has occurred when receiving results from the server. (provider: TCP Provider, error: 0…). So on Windows XP/2000 it works fine.
    Why it hangs on Win2003 server?

    I analyze this and notice that function in system.data.dll waits for some event using WaitForSingleObject in loop with 1 second timeout.

    Here is stack trace for case when it hangs:

    7ffe0304()
    ntdll.dll!77f4c994()
    [Frames below may be incorrect and/or missing, no symbols loaded for ntdll.dll]
    kernel32.dll!77e7eaee()
    kernel32.dll!77e5d4a3()
    System.Data.dll!6500d22a()
    System.Data.dll!65000520()
    System.Data.dll!SNINativeMethodWrapper.SNIClose(System.IntPtr pConn) + 0x5c bytes
    System.Data.dll!System.Data.SqlClient.SNIHandle.ReleaseHandle() + 0x35 bytes
    mscorlib.dll!System.Runtime.InteropServices.SafeHandle.Dispose(bool disposing) + 0xc bytes
    mscorlib.dll!System.Runtime.InteropServices.SafeHandle.Dispose() + 0xa bytes
    System.Data.dll!System.Data.SqlClient.TdsParserStateObject.Dispose() + 0x8e bytes
    System.Data.dll!System.Data.SqlClient.TdsParser.Disconnect() + 0x86 bytes
    System.Data.dll!System.Data.SqlClient.SqlInternalConnectionTds.Dispose() + 0x67 bytes
    System.Data.dll!System.Data.ProviderBase.DbConnectionPool.DestroyObject(System.Data.ProviderBase.DbConnectionInternal obj = {System.Data.SqlClient.SqlInternalConnectionTds}) + 0x106 bytes
    System.Data.dll!System.Data.ProviderBase.DbConnectionPool.DeactivateObject(System.Data.ProviderBase.DbConnectionInternal obj) + 0xff bytes
    System.Data.dll!System.Data.ProviderBase.DbConnectionPool.PutObject(System.Data.ProviderBase.DbConnectionInternal obj, object owningObject) + 0x10a bytes
    System.Data.dll!System.Data.ProviderBase.DbConnectionInternal.CloseConnection(System.Data.Common.DbConnection owningObject = {System.Data.SqlClient.SqlConnection}, System.Data.ProviderBase.DbConnectionFactory connectionFactory = {System.Data.SqlClient.SqlConnectionFactory}) + 0x62 bytes
    System.Data.dll!System.Data.SqlClient.SqlConnection.Close() + 0x76 bytes
    System.Data.dll!System.Data.SqlClient.SqlConnection.OnError(System.Data.SqlClient.SqlException exception = {Cannot evaluate expression because a native frame is on top of the call stack.}, bool breakConnection) + 0x37 bytes
    System.Data.dll!System.Data.SqlClient.SqlInternalConnection.OnError(System.Data.SqlClient.SqlException exception, bool breakConnection) + 0xbc bytes
    System.Data.dll!System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(System.Data.SqlClient.TdsParserStateObject stateObj) + 0xbd bytes
    System.Data.dll!System.Data.SqlClient.TdsParserStateObject.SendAttention() + 0x15f bytes
    System.Data.dll!System.Data.SqlClient.TdsParserStateObject.ReadSniError(System.Data.SqlClient.TdsParserStateObject stateObj = {System.Data.SqlClient.TdsParserStateObject}, uint error) + 0x10e bytes
    System.Data.dll!System.Data.SqlClient.TdsParserStateObject.ReadSni(System.Data.Common.DbAsyncResult asyncResult, System.Data.SqlClient.TdsParserStateObject stateObj) + 0xa5 bytes
    System.Data.dll!System.Data.SqlClient.TdsParserStateObject.ReadPacket(int bytesExpected) + 0x23 bytes
    System.Data.dll!System.Data.SqlClient.TdsParserStateObject.ReadBuffer() + 0x1f bytes
    System.Data.dll!System.Data.SqlClient.TdsParserStateObject.ReadByte() + 0x12 bytes
    System.Data.dll!System.Data.SqlClient.TdsParser.Run(System.Data.SqlClient.RunBehavior runBehavior, System.Data.SqlClient.SqlCommand cmdHandler = {System.Data.SqlClient.SqlCommand}, System.Data.SqlClient.SqlDataReader dataStream = {System.Data.SqlClient.SqlDataReader}, System.Data.SqlClient.BulkCopySimpleResultSet bulkCopyHandler = null, System.Data.SqlClient.TdsParserStateObject stateObj = {System.Data.SqlClient.TdsParserStateObject}) + 0x3c bytes
    System.Data.dll!System.Data.SqlClient.SqlDataReader.ConsumeMetaData() + 0x20 bytes
    System.Data.dll!System.Data.SqlClient.SqlDataReader.MetaData.get() + 0x3f bytes
    System.Data.dll!System.Data.SqlClient.SqlCommand.FinishExecuteReader(System.Data.SqlClient.SqlDataReader ds = {System.Data.SqlClient.SqlDataReader}, System.Data.SqlClient.RunBehavior runBehavior, string resetOptionsString) + 0xd4 bytes
    System.Data.dll!System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(System.Data.CommandBehavior cmdBehavior, System.Data.SqlClient.RunBehavior runBehavior, bool returnStream, bool async = false) + 0x236 bytes
    System.Data.dll!System.Data.SqlClient.SqlCommand.RunExecuteReader(System.Data.CommandBehavior cmdBehavior, System.Data.SqlClient.RunBehavior runBehavior, bool returnStream, string method, System.Data.Common.DbAsyncResult result) + 0x85 bytes
    System.Data.dll!System.Data.SqlClient.SqlCommand.RunExecuteReader(System.Data.CommandBehavior cmdBehavior, System.Data.SqlClient.RunBehavior runBehavior, bool returnStream, string method) + 0x21 bytes
    System.Data.dll!System.Data.SqlClient.SqlCommand.ExecuteReader(System.Data.CommandBehavior behavior, string method) + 0x7b bytes
    System.Data.dll!System.Data.SqlClient.SqlCommand.ExecuteReader() + 0x55 bytes
    > HungTest.exe!HungTest.HungTest.DoSomeWork() Line 39 + 0xa bytes C#
    HungTest.exe!HungTest.Program.Main() Line 19 + 0xa bytes C#
    ntdll.dll!77f492fb()
    ntdll.dll!77f84d57()
    ntdll.dll!77f48e7a()
    ntdll.dll!77f492fb()
    ntdll.dll!77f492fb()
    ntdll.dll!77f84d57()
    ntdll.dll!77f48e7a()
    ntdll.dll!77f492fb()
    ntdll.dll!77f492fb()
    ntdll.dll!77f492fb()
    ntdll.dll!77f48c7a()
    mscoree.dll!79011b5f()
    kernel32.dll!77e7d977()

    Here is Source code:

    namespace HungTest
    {
    class HungTest
    {
    string ConnectionString = "Data Source=...;Initial Catalog=...;Persist Security Info=True;User ID=...;password=...";
    string queryUpdate = "update ...";

    public void DoSomeWork()
    {
    Exception occuredEx = null;
    Console.WriteLine("DoSomeWork started");
    try
    {
    using (SqlConnection conn = new SqlConnection(this.ConnectionString))
    {
    conn.Open();
    SqlTransaction trans = conn.BeginTransaction(IsolationLevel.Serializable);
    try
    {
    SqlCommand cmd = conn.CreateCommand();
    cmd.CommandTimeout = 30;
    cmd.Transaction = trans;
    cmd.CommandText = queryUpdate;

    Console.WriteLine("Run...");
    using (SqlDataReader dr = cmd.ExecuteReader())
    {
    int row_count = 0;
    while (dr.Read())
    {
    row_count++;
    }
    Console.WriteLine("Rows processed: " + row_count.ToString());
    }
    trans.Commit();
    }
    catch
    {
    try
    {
    trans.Rollback();
    }
    catch (Exception ex)
    {
    Console.WriteLine(ex.Message);
    }
    throw;
    }
    conn.Close();
    }
    }
    catch (Exception ex)
    {
    Console.WriteLine(ex.Message);
    occuredEx = ex;
    }
    finally
    {
    //stop point
    Console.ReadLine();
    }
    }

    }
    }




    Thanks for your assistance!



    Friday, October 5, 2007 8:00 AM

Answers

  • I found that this problem occurs only in windows 2003 Server Standart edition.
    The problem was found on win sockets library. Socket doesn’t feel any disconnect events.
    WSARecv ends read operation by timeout and then .Net library handle this situation incorrectly.

    On WinXP Professional WSARecv ends with error code 10054 without timeout and .Net library handle this situation correctly.
    Monday, October 15, 2007 10:53 AM
  •  

    I'm glad you were able to find the problem. This is a problem that I believe was fixed with Visual Studio 2008, so if you can install either the fix for the sockets library or VS 2008, you should be good to go.

     

    Thanks,

    Erick

    Monday, October 15, 2007 8:30 PM

All replies

  • Hello,

     

    Does the problem occur with a number of different commands/databases, or just this particular command/database combination?

     

    Also, please install Visual Studio 2008 Beta 2, as this contains a number of bug fixes in the area that you are seeing the problem in. You don't need to use VS 2008, you can continue to use 2005, as 2008 installs the fixes to 2.0 assemblies. Let me know if doing this fixes the problem.

     

    Thanks,

    Erick

    Monday, October 8, 2007 6:32 PM
  • I found that this problem occurs only in windows 2003 Server Standart edition.
    The problem was found on win sockets library. Socket doesn’t feel any disconnect events.
    WSARecv ends read operation by timeout and then .Net library handle this situation incorrectly.

    On WinXP Professional WSARecv ends with error code 10054 without timeout and .Net library handle this situation correctly.
    Monday, October 15, 2007 10:53 AM
  •  

    I'm glad you were able to find the problem. This is a problem that I believe was fixed with Visual Studio 2008, so if you can install either the fix for the sockets library or VS 2008, you should be good to go.

     

    Thanks,

    Erick

    Monday, October 15, 2007 8:30 PM
  • Could this also be the cause of an access database hanging when located on Windows Server 2003?

     

    Monday, October 29, 2007 4:36 PM