Exception on System.Net.OSSOCK.closesocket()

  • Thread starter Thread starter pedrito
  • Start date Start date
P

pedrito

I've got an app that downloads from several concurrent threads.

Occasionally, I get an expcetion trying to read the HttpWebResponse
stream...

Sorry, the code isn't short, but I figured best to provide the whole method.
This is run within its own thread. "Job" is a class that receives the
information about the downloaded pages. The exception happens on the read
where it says "EXCEPTION HERE"

private static void DownloadPage(Job job)
{
HttpWebRequest req = null;
try
{
Debug.WriteLine("Beginning HTTP request of: " + job.URL);
req = (HttpWebRequest) HttpWebRequest.Create(job.URL);
req.UserAgent = _browserID;
if (job.Referrer != null)
{
req.Referer = job.Referrer;
}
req.CookieContainer = job.Cookies;
req.Timeout = _timeout;
}
catch(UriFormatException ex)
{
job.Status = JobStatus.Error;
job.ReportError(-1, ex.Message);
_threadMgr.RemoveJob(job);
return;
}
try
{
job.BeginDownload();
using (HttpWebResponse resp = req.GetResponse() as HttpWebResponse)
{
using (Stream pageStream = resp.GetResponseStream())
{

byte[] readBuffer = new byte[BUFFERSIZE];

byte[] retBuffer = null;
int readCount = -1;
int lastReadCount = -1;
// EXCEPTION HERE
while ((readCount = pageStream.Read(readBuffer, 0,
BUFFERSIZE)) > 0)
{
if (retBuffer == null)
{
retBuffer = new byte[readCount];
for(int index = 0; index < retBuffer.Length;
index++)
{
retBuffer[index] = readBuffer[index];
}
lastReadCount = readCount;
}
else
{
byte[] tempBuffer = new byte[readCount +
retBuffer.Length];
int index = 0;
for(index = 0; index < retBuffer.Length; index++)
{
tempBuffer[index] = retBuffer[index];
}
for(index = 0; index < readCount; index++)
{
tempBuffer[index + retBuffer.Length] =
readBuffer[index];
}
retBuffer = tempBuffer;
lastReadCount = readCount;
}
if (retBuffer != null)
{
job.UpdateProgress(retBuffer);
}
}
Thread.Sleep(100);
job.SetData(retBuffer);
job.Status = JobStatus.CompletedSuccessful;
job.Enabled = false;
job.DownloadComplete();
_threadMgr.RemoveJob(job);
}
}
}
catch(WebException ex)
{
job.Status = JobStatus.Error;
job.ReportError((int) ((HttpStatusCode) ex.Status), ex.Message);
job.Enabled = false;
job.SetData(null);
_threadMgr.RemoveJob(job);
}
catch(Exception ex)
{
Debug.WriteLine("Unexpected Exception: " + ex.Message);
job.Status = JobStatus.Error;
job.ReportError(-1, ex.Message);
job.Enabled = false;
_threadMgr.RemoveJob(job);
}
lock(_oldThreads.SyncRoot)
{
_oldThreads.Add(Thread.CurrentThread);
}
job.ExecutingThread = null;
}


The outer exception is an "Unable to read data from the transport
connection". The inner is a null referenceexception that has the following
stack:

StackTrace "
at System.Net.OSSOCK.closesocket(IntPtr socketHandle)\r\n
at System.Net.Sockets.Socket.Dispose(Boolean disposing)\r\n
at System.Net.Sockets.Socket.System.IDisposable.Dispose()\r\n
at System.Net.Sockets.Socket.Close()\r\n
at System.Net.Sockets.NetworkStream.Dispose(Boolean disposing)\r\n
at System.Net.Sockets.NetworkStream.System.IDisposable.Dispose()\r\n
at System.Net.Sockets.NetworkStream.Close()\r\n
at System.Net.Connection.CloseConnectionSocket(WebExceptionStatus status,
ConnectionReturnResult& returnResult)\r\n
at System.Net.Connection.HandleError(WebExceptionStatus webExceptionStatus,
ConnectionFailureGroup failureGroup, ConnectionReturnResult&
returnResult)\r\n

This ends up eventually causing a Win32 exception and I can't seem to catch
it and bring my whole app crashing down.

I'm assuming closesocket is dying on this line:

int num = UnsafeNclNativeMethods.OSSOCK.closesocket(this.m_Handle);

and I suspect m_Handle is what's null and that must be causing the unhandled
Win32 exception

Is there any way around this? Am I just doing something wrong???

As for all the byte at a time copying of the arrays, I send the array back
to the GUI thread via the job.UpdateProgress(retBuffer). To keep the GUI
from stepping on the arrays used during the download, I'm simply creating a
new copy of the array for the gui... There's probably a faster way to copy
them, but this doesn't seem too slow...

Anyway, please, if you have any ideas, I'd like to hear them. This is
driving me nuts.
 
Some additional information, the exception is happening on a callback
thread. Sorry I forgot to mention that. The ENTIRE call stack is

system.dll!System.Net.Connection.ReadCallback(System.IAsyncResult
asyncResult = {System.Net.Sockets.OverlappedAsyncResult}) + 0x3ab bytes
system.dll!System.Net.LazyAsyncResult.InvokeCallback() + 0x27 bytes
system.dll!System.Net.LazyAsyncResult.InvokeCallback(bool
completedSynchronously = false, System.Object result = {0}) + 0x23 bytes
system.dll!System.Net.Sockets.OverlappedAsyncResult.CompletionPortCallback(uint
errorCode = 10054, uint numBytes = 0, System.Threading.NativeOverlapped*
nativeOverlapped = 1985184) + 0x16d bytes
 
Still haven't figured this out. Would really appreciate it if anyone has any
idea. I've resorted to using ADplus and kernel debugger, not that I really
know what I'm doing. Here are som stacks and exceptions where it appears to
be happening. First is here:

Mon Aug 20 18:06:09.375 2007 (GMT-5): (4450.4638): Access violation - code
c0000005 (first chance)
---
--- 1st chance AccessViolation exception ----
---------------------------------------------------------------

Occurrence happened at:
Debug session time: Mon Aug 20 18:06:09.375 2007 (GMT-5)
System Uptime: 1 days 19:38:25.118
Process Uptime: 0 days 0:00:43.921
Kernel time: 0 days 0:00:00.796
User time: 0 days 0:00:01.265

Faulting stack below ---
# ChildEBP RetAddr Args to Child
WARNING: Stack unwind information not available. Following frames may be
wrong.
00 050aea3c 71a54229 04ac3088 00000000 04ac2ff0
ntdll!RtlDeleteCriticalSection+0x46
01 050aea58 71a541c6 04ac2ff0 050aeec0 03f175c0 mswsock+0x4229
02 050aeac4 71ab9680 00000814 050aeadc 050aeec0 mswsock+0x41c6
03 050aeb0c 7b28ae24 00000000 00c9b220 00c9b19c WS2_32!closesocket+0x47
04 050aeb38 7b28ae99 00000000 00c9b220 00c9b19c system_7b1d0000+0xbae24
05 050aeb48 7b286956 00000001 00c9b220 7b28698c system_7b1d0000+0xbae99
06 050aeb54 7b28698c 00000000 00000001 00cd1528 system_7b1d0000+0xb6956
07 050aeb64 7b2868ff 7b274b9f 050aebb8 00000000 system_7b1d0000+0xb698c
08 050aeb68 7b274b9f 050aebb8 00000000 00c18ad0 system_7b1d0000+0xb68ff
09 050aeb8c 7b273336 050aebb4 00e3a090 00d1462c system_7b1d0000+0xa4b9f
0a 050aeba0 7b277b30 050aebb4 00d1462c 00e31e44 system_7b1d0000+0xa3336
0b 050aebb8 7b27a6c0 00d1462c ffffffff 00c16ce0 system_7b1d0000+0xa7b30
0c 050aebc8 7b27a608 7b279594 050aec84 00e31e1c system_7b1d0000+0xaa6c0
0d 050aebcc 7b279594 050aec84 00e31e1c 00d1462c system_7b1d0000+0xaa608
0e 050aebe8 7b2793a4 00d1462c 00e31d18 7b278b76 system_7b1d0000+0xa9594
0f 050aebf4 7b278b76 00000000 00000118 00c0c590 system_7b1d0000+0xa93a4
10 050aec5c 791b1d1d 00c3f208 030fd40b 00000000 system_7b1d0000+0xa8b76
11 050aec70 791b1a28 79998dc4 00000000 00bfa660 mscorwks!Ordinal76+0x1d1d
12 050aec74 79998dc4 00000000 00bfa660 050aecb4 mscorwks!Ordinal76+0x1a28
13 050aeca0 791daf8e 00000000 00000000 00000000 mscorlib_79990000+0x8dc4
14 050aecb4 791dc474 050aedcc 791dce6f 050aed08
mscorwks!CoInitializeEE+0xa0cb
15 050aecbc 791dce6f 050aed08 00000000 050aece0
mscorwks!GetCompileInfo+0xf7a
16 050aedcc 791ddb14 00bca8cb 79b7c000 050aedf8
mscorwks!GetCompileInfo+0x1975
17 050aee88 791ddb82 79bca8cb 79b7c000 79b93e78
mscorwks!GetCompileInfo+0x261a
18 050aeeb0 792ea4af 050aeef4 0021ee10 791b33f8
mscorwks!GetCompileInfo+0x2688
19 050aeefc 792ea57e 050aef14 00212c70 792ea4bc
mscorwks!ReleaseFusionInterfaces+0x4104c
1a 050aefa0 791c94b4 00212c70 00000000 00000000
mscorwks!ReleaseFusionInterfaces+0x4111b
1b 050affb4 7c80b683 00213c40 00000000 0020df10 mscorwks!Ordinal17+0x10c4
1c 050affec 00000000 791c9473 00213c40 00000000
KERNEL32!GetModuleFileNameA+0x1b4



Second is here:

Mon Aug 20 18:06:12.218 2007 (GMT-5): (4450.4630): Access violation - code
c0000005 (first chance)
---
--- 1st chance AccessViolation exception ----
---------------------------------------------------------------

Occurrence happened at:
Debug session time: Mon Aug 20 18:06:12.218 2007 (GMT-5)
System Uptime: 1 days 19:38:27.958
Process Uptime: 0 days 0:00:46.765
Kernel time: 0 days 0:00:00.843
User time: 0 days 0:00:01.265

Faulting stack below ---
# ChildEBP RetAddr Args to Child
WARNING: Stack unwind information not available. Following frames may be
wrong.
00 0576eabc 71a54229 04ac29c8 00000000 04ac2930
ntdll!RtlDeleteCriticalSection+0x46
01 0576ead8 71a541c6 04ac2930 0576ef40 03f17df8 mswsock+0x4229
02 0576eb44 71ab9680 000006d0 0576eb5c 0576ef40 mswsock+0x41c6
03 0576eb8c 7b28ae24 00000000 00c7a4b0 00c7a42c WS2_32!closesocket+0x47
04 0576ebb8 7b28ae99 00000000 00c7a4b0 00c7a42c system_7b1d0000+0xbae24
05 0576ebc8 7b286956 00000001 00c7a4b0 7b28698c system_7b1d0000+0xbae99
06 0576ebd4 7b28698c 00000000 00000001 00c7ab50 system_7b1d0000+0xb6956
07 0576ebe4 7b2868ff 7b274b9f 0576ec38 00000000 system_7b1d0000+0xb698c
08 0576ebe8 7b274b9f 0576ec38 00000000 00c1e090 system_7b1d0000+0xb68ff
09 0576ec0c 7b273336 0576ec34 00e4e6cc 00c7ba10 system_7b1d0000+0xa4b9f
0a 0576ec20 7b277b30 0576ec34 00c7ba10 01072e44 system_7b1d0000+0xa3336
0b 0576ec38 7b27a6c0 00c7ba10 ffffffff 00c16ce0 system_7b1d0000+0xa7b30
0c 0576ec48 7b27a608 7b279594 0576ed04 01072e1c system_7b1d0000+0xaa6c0
0d 0576ec4c 7b279594 0576ed04 01072e1c 00c7ba10 system_7b1d0000+0xaa608
0e 0576ec68 7b2793a4 00c7ba10 01072d18 7b278b76 system_7b1d0000+0xa9594
0f 0576ec74 7b278b76 00000000 00000118 00c0c4e0 system_7b1d0000+0xa93a4
10 0576ecdc 791b1d1d 00c3b364 030fd40b 00000000 system_7b1d0000+0xa8b76
11 0576ecf0 791b1a28 79998dc4 00000000 00bfa660 mscorwks!Ordinal76+0x1d1d
12 0576ecf4 79998dc4 00000000 00bfa660 0576ed34 mscorwks!Ordinal76+0x1a28
13 0576ed20 791daf8e 00000000 00000000 00000000 mscorlib_79990000+0x8dc4
14 0576ed34 791dc474 0576ee4c 791dce6f 0576ed88
mscorwks!CoInitializeEE+0xa0cb
15 0576ed3c 791dce6f 0576ed88 00000000 0576ed60
mscorwks!GetCompileInfo+0xf7a
16 0576ee4c 791ddb14 00bca8cb 79b7c000 0576ee78
mscorwks!GetCompileInfo+0x1975
17 0576ef08 791ddb82 79bca8cb 79b7c000 79b93e78
mscorwks!GetCompileInfo+0x261a
18 0576ef30 792ea4af 0576ef74 0021efc8 791b33f8
mscorwks!GetCompileInfo+0x2688
19 0576ef7c 792ea57e 0576ef94 00212c70 792ea4bc
mscorwks!ReleaseFusionInterfaces+0x4104c
1a 0576f020 791c94b4 00212c70 00000000 00000000
mscorwks!ReleaseFusionInterfaces+0x4111b
1b 0576ffb4 7c80b683 00213c40 00000000 00213ea8 mscorwks!Ordinal17+0x10c4
1c 0576ffec 00000000 791c9473 00213c40 00000000
KERNEL32!GetModuleFileNameA+0x1b4
 
Back
Top