ReadFile has return ERROR_WORKING_SET_QUOTA
GetLastError return 1453 ERROR_WORKING_SET_QUOTA after ReadFile. Does it means that there are memory leak or something? This code is used to communicate with usb device. This procedure works for a 10 hours (read every 40 ms) of testing and then error comes.
uint ReadBytesCount = 0;
byte[] bytes = new byte[0x8000];
fixed (byte* p = bytes)
{
if (UnsafeMethods.ReadFile(handle, p, 0x8000, out ReadBytesCount, intOverlapped) == 0)
{
int hr = UnsafeMethods.GetLastError(handle);
if (hr == UnsafeMethods.ERROR_ACCESS_DENIED)
{
doCleanup = true;
break;
}
if (hr == NativeMethods.ERROR_IO_PENDING)
{
int error = 0;
// if we get IO pending, MSDN says we should wait
// on the WaitHandle, then call GetOverlappedResult
// use timeout to ensure that first time read
bool success = waitReadEventWaitHandle.WaitOne(1000);
if (success)
{
uint ReadInProgressCount = 0;
success = Unsaf开发者_JAVA百科eMethods.GetOverlappedResult(
handle,
intOverlapped,
ref ReadInProgressCount,
false);
if (!success)
error = UnsafeMethods.GetLastError(handle);
ReadBytesCount += ReadInProgressCount;
if (!success && error != 0)
{
// Ignore ERROR_IO_INCOMPLETE, because there's a chance
// one of those while shutting down
if (!(
(error == UnsafeMethods.ERROR_IO_INCOMPLETE)
&& ShutdownLoop)
)
Debug.Assert(false,
"GetOverlappedResult,might leak intOverlapped memory"
+ error.ToString());
}
}
}
else if (hr != UnsafeMethods.ERROR_INVALID_PARAMETER)
{
// ignore ERROR_INVALID_PARAMETER errors.
Debug.Assert(false, "ReadUsbLoop returned error " + hr);
}
}
}
The only meaningful info i have found was in mysql bugs list unconditional exit(1) on ERROR_WORKING_SET_QUOTA 1453 (0x5AD) for InnoDB backend.
When the error occurs, the SQL Server worker yields for 100ms and tries the read operation again. This loop continues until the I/O is successfully issued. A simplified example demonstrates this behavior.
WHILE( FALSE == ReadFile()
&& (1450 == GetLastError() || 1453 == GetLastError())
)
{
Yield(100);
}
That lead me to conclusion that I should try sleep thread for 100 ms and retry read.
uint ReadBytesCount = 0;
byte[] bytes = new byte[0x8000];
fixed (byte* p = bytes)
{
if (UnsafeMethods.ReadFile(handle, p, 0x8000, out ReadBytesCount, intOverlapped) == 0)
{
int hr = UnsafeMethods.GetLastError(handle);
if (hr == UnsafeMethods.ERROR_ACCESS_DENIED)
{
doCleanup = true;
break;
}
if (hr == NativeMethods.ERROR_IO_PENDING)
{
// do something
}
else if (hr == UnsafeMethods.ERROR_WORKING_SET_QUOTA ||
hr == UnsafeMethods.ERROR_NO_SYSTEM_RESOURCES)
{
Thread.Sleep(100);
}
else if (hr != UnsafeMethods.ERROR_INVALID_PARAMETER)
{
// ignore ERROR_INVALID_PARAMETER errors.
Debug.Assert(false, "ReadUsbLoop returned error " + hr);
}
}
}
Update: No, it did not help. Now I am considering throwing timeout error and reopening a device
精彩评论