title: sp_send_dbmail blocks and causes PREEMPTIVE_OS_GETPROCADDRESS
body: <p>I have a strange issue with <code>msdb.dbo.sp_send_dbmail</code>within a stored procedure (code simplified):</p>
  
  <pre><code>SET NOCOUNT ON;
  SET XACT_ABORT ON;
  
  -- ...
  
  SET @msg = N'...';
  SET @filename = N'...';
  
  EXEC msdb.dbo.sp_send_dbmail
          @recipients = 'mailaddr@example.com'
          , @blind_copy_recipients = 'another_mailaddr@example.com'
          , @from_address = 'senders_mail@example.com'
          , @subject = N'...'
          , @body = @msg
          , @query = N'SET NOCOUNT ON; SELECT &lt;something&gt; FROM &lt;a_view&gt;;'
          , @execute_query_database = N'&lt;same database the proc resides in&gt;'
          , @query_result_width = 8000
          , @attach_query_result_as_file = 1
          , @query_attachment_filename = @filename
          , @query_result_header = 1
          , @query_result_separator = ';'
          , @query_result_no_padding = 1
          , @exclude_query_output = 1;
  </code></pre>
  
  <p>I have a database user which is member of <code>db_datareader</code>and <code>EXECUTE</code>permissions for the stored procedure. Furthermore the assigned server login is mapped to <code>msdb</code>and a member of the <code>DatabaseMailUserRole</code>. There is only 1 single mail profile which is <code>public</code> and flagged as <code>default</code>.</p>
  
  <p>Everything works fine from SSMS: connect with the user's credentials and execute the stored procedure. Great!</p>
  
  <p>The first oddity: if I'm logged in as <code>sysadmin</code> and try to <code>EXECUTE AS</code> it doesn't work. Okay, I found something that points out that there are issues with this.</p>
  
  <p>But the <strong>main issue</strong> is this: I call the procedure from a 3rd party Java application which connects using the jTDS driver (don't know if this is important). The applicaton executes the procedure ... and nothing else happened (no log entries, the task freezes).</p>
  
  <p>In the activity monitor I see the following:</p>
  
  <ul>
  <li>Process &lt;...></li>
  <li>Database <code>master</code> (??? I've never connected to this
  db!)</li>
  <li>Task State <em>Running</em></li>
  <li>Wait Type <code>PREEMPTIVE_OS_GETPROCADDRESS</code></li>
  <li><strong>Head Blocker <code>1</code></strong></li>
  </ul>
  
  <p>To make things worse I cannot kill this process. If I try this the <em>Command</em> column in the activity monitor shows only <code>KILLED/ROLLBACK</code>.</p>
  
  <p><code>KILL &lt;PROCESS-ID&gt;</code>shows</p>
  
  <blockquote>
    <p>spid &lt;...>: Transaction rollback in progress. Estimated rollback completion: 0% Estimated time left: 0 seconds.</p>
  </blockquote>
  
  <p>I have to restart the whole instance to get rid of the process.</p>
  
  <p>What is happening here?</p>