title: SqlClient calls causing "Thread was being aborted at SNINativeMethodWrapper.SNIPacketGetConnection(IntPtr packet)"
body: <p>I would really appreciate any suggestions, no matter how simple or complex, to help me get this issue isolated and resolved. </p>
  
  <p>I have a bit of code that generates small report files. For each file in the collection, a stored proc is executed to get the data via XML reader (its a pretty big result set). When I created all this, and stepped through it, all is well. Files are generated, no errors. </p>
  
  <p>This library is called via remoting, and is hosted via IIS. When I deploy the compiled library and call it, its able to generate some of the reports, but then throws a Thread Abort Exception. If I attach the debugger to the asp worker process, and step through the code, I have no problems.</p>
  
  <p>Seeing as this failure is pretty consistent, I looked for similarities and found that the failure happens on different reports, but seems to happen at about the same chronological point.</p>
  
  <p>This lead me to think that it was a timeout setting that the debugger is overriding, I did some rough timings of the overall process (not the single piece of failing code) and it seems to fail at just after about 200 seconds. The web.config executionTimeout is set for 600 minutes (plenty high enough). There are other parts to this server application that require COM+ transactions (2 minute timeout), but this is not one of them. I am at a loss as to what timeout it could be hitting (at about the 200 second mark).</p>
  
  <p>The SQL Connection timeout is left at default (the connection opens successfully), the command timeout is 300 seconds (it takes only 12-15 to exec the command). </p>
  
  <ul>
  <li>Is there any other timeout that I could be missing?</li>
  </ul>
  
  <p>I ran SQL profiler, and it shows that the result is returned correctly (all statements and RPC completed - no errors). Executing the code via SSMS provides perfect results.</p>
  
  <p>Using reflector, I drilled into the SNINativeMethodWrapper, and its a wrapper for unmanaged code and I cant see what its trying to actually do. I can only assume (perhaps wrongly) that the code has received the TDS from the SQL server and the wrapper is trying to get the connection associated with the packet and it cannot.</p>
  
  <ul>
  <li>Does anyone know what this wrapper is supposed to do?</li>
  <li>Is there any way to trace / debug this code to find out what is causing the failure?</li>
  </ul>
  
  <p>I tried using different methods (ExecScalar, DataAdapter), but they all use ExecuteReader internally. </p>
  
  <p>I tried disabling connection pooling and forcing the client to use the same packet size as the server.</p>
  
  <ul>
  <li>Does anybody have any ideas as to what causes this, or what I can do to isolate and try to correct the problem?</li>
  </ul>
  
  <h2>This is the calling code where the exception is generated.</h2>
  
  <pre><code>Private Function GetDataAsXmlDoc(ByVal cmd As SqlClient.SqlCommand) As XmlDocument
  
      Dim _xmlDoc As XmlDocument
  
      Using _connection As New SqlClient.SqlConnection(GetConnectionString())
  
          Logging.DebugEvent.Raise(Me.GetType.Namespace, Reflection.MethodBase.GetCurrentMethod().Name, _
                                   "No cached data found or used. Getting data for report from the database using SQL connection.")
  
          Dim _xmlReader As XmlReader
          'DataAdapter,ExecuteScalar, ExecuteXmlReader all use ExecuteReader internally and suffer the same problem.'
          'If you dont believe me, reflect it or look at one of the blowed up stack traces. '
  
          '_connection.ConnectionString += ";Pooling=false;"' 'This has no effect on the ThreadAbort.'
          cmd.Connection = _connection
          cmd.CommandTimeout = 300
          _connection.Open()
  
          Logging.DebugEvent.Log(String.Format("Connection opened, using packet size of {0}.", _connection.PacketSize))
  
          _xmlReader = cmd.ExecuteXmlReader() 'Thread aborts in here'
  
          Logging.DebugEvent.Raise(Me.GetType.Namespace, Reflection.MethodBase.GetCurrentMethod().Name, _
                                   "Report data recieved from database")
  
          _xmlDoc = New XmlDocument()
          _xmlDoc.Load(_xmlReader)
  
          _xmlReader.Close()
  
      End Using
  
    Return _xmlDoc
  
  End Function
  </code></pre>
  
  <h2>Stack</h2>
  
  <pre><code>Exception String - System.Threading.ThreadAbortException: Thread was being aborted.
     at SNINativeMethodWrapper.SNIPacketGetConnection(IntPtr packet)
     at System.Data.SqlClient.TdsParserStateObject.ProcessSniPacket(IntPtr packet, UInt32 error)
     at System.Data.SqlClient.TdsParserStateObject.ReadSni(DbAsyncResult asyncResult, TdsParserStateObject stateObj)
     at System.Data.SqlClient.TdsParserStateObject.ReadNetworkPacket()
     at System.Data.SqlClient.TdsParserStateObject.ReadBuffer()
     at System.Data.SqlClient.TdsParserStateObject.ReadByte()
     at System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj)
     at System.Data.SqlClient.SqlDataReader.ConsumeMetaData()
     at System.Data.SqlClient.SqlDataReader.get_MetaData()
     at System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString)
     at System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async)
     at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, DbAsyncResult result)
     at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method)
     at System.Data.SqlClient.SqlCommand.ExecuteXmlReader()...
  </code></pre>