Request.Abort() causing sockets to stay in CLOSE_WAIT state

A

Allen L.

Hi,
I am trying to write a small program to do an async fetch of a web
page. Currently everything works fine if my timer doesn't expire.
But if it does and I call Request.Abort(), the request does seem to
abort but then the socket looks like it is in a CLOSE_WAIT state and
doesn't close until I exit the program. I'm using CF SP2.

Here is a snippet of code to illustrate what I'm talking about. It's
modeled after the MSDN example:


private void RespCallback(IAsyncResult ar)
{
// Get the RequestState object from the async result.
RequestState rs = (RequestState) ar.AsyncState;

// Get the WebRequest from RequestState.
WebRequest req = rs.Request;
WebResponse resp = null;

//Call EndGetResponse, which produces the WebResponse object
//that came from the request issued above.
try
{
resp = req.EndGetResponse(ar);
rs.contentType = resp.Headers["Content-Type"];
// Start reading data from the response stream.
Stream ResponseStream = resp.GetResponseStream();

// Store the response stream in RequestState to read
// the stream asynchronously.
rs.ResponseStream = ResponseStream;

// Pass rs.BufferRead to BeginRead. Read data into
rs.BufferRead
IAsyncResult iarRead =
ResponseStream.BeginRead(rs.BufferRead, 0,
BufferSize, new AsyncCallback(ReadCallBack), rs);
}
catch (WebException we)
{
_log.Warn("Can't get response from server to cache
file: " + we.Message + " Status: " + we.Status);
_log.Warn("URI:" + req.RequestUri.ToString());

rs.Success=false;
if (rs.ResponseStream != null)
{
rs.ResponseStream.Close();
}
rs.allDone.Set();
}
}

I have a timer in the method that calls RespCallBack which will issue
a request.Abort(). I can this message in my log file when the abort
happens:
The request was aborted: RequestCanceled. Status: RequestCanceled

So I close the Response Stream if the abort happens. I also close the
response stream inside of the ReadCallBack method if I finish reading
so I'm pretty sure I'm closing the response.

Has anyone seen this before? Is this a known issue?

Thanks,
Allen
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top