Condividi tramite


WinHTTP Questions - async close on a sync request

Hello, my name is Deepak and I'm a SDET in serviceability. We handle a bunch of questions from developers using WinHTTP, and thought we might share then in a new posting series, "WinHTTP Questions".

Can I cancel a synchronous WinHttpSendRequest call by closing the request handle from a different thread? Or, are there any requirements that I need to use the asynchronous WinHttpSendRequest if I need the ability to cancel it?

The short answer is don't do that.

Let's think about the implications of this proposed code. Thread 1 starts a synchronous request and Thread 2 comes along an issues a close. We can't synchronize between Thread 1 and Thread 2 by definition because Thread 1's call is synchronous, we either can deterministically call the close before the request call starts or after the call is completed. So to close the handle during the call results in a race condition. If thread 2 attempts to close the connection during the call, the handle passed in to WinHttpSendRequest may become invalid before WinHttp has a chance to work with it. While you might get lucky, you might also non-deterministically get a crash or memory corruption.

  -- Deepak and Ari

Comments

  • Anonymous
    February 26, 2008
    While you're on the topic of WinHTTP questions, could you address whether the WinHTTP performance issue I describe at http://kobyk.wordpress.com/2007/08/31/winhttprequest-performance-woes/ will be mitigated in the future? It appears you understand the limitations and behavior of the api (although you probably want ResponseBody, not ResponseText, since ResponseText does additional codepage related manipulation). First, I would urge that you file a suggestion on our connect site at http://connect.microsoft.com/winhttp/feedback (you need a live id and to "apply" for the program to submit feedback at http://connect.microsoft.com/programdetails.aspx?ProgramDetailsId=90 ). Although I will note that the development priorities this time around will make it difficult to get to COM related winhttp use. Second, I would suggest using the system.net apis via powershell for this usage. -- Ari
  • Anonymous
    June 19, 2008
    The comment has been removed