[pywin32-bugs] [ pywin32-Bugs-2800605 ] Potential problem with win32file.ReadFile and async I/O
OLD project page for the Python extensions for Windows
Brought to you by:
mhammond
From: SourceForge.net <no...@so...> - 2009-06-03 16:53:25
|
Bugs item #2800605, was opened at 2009-06-03 12:53 Message generated for change (Tracker Item Submitted) made by vokinloksar You can respond by visiting: https://sourceforge.net/tracker/?func=detail&atid=551954&aid=2800605&group_id=78018 Please note that this message will contain a full copy of the comment thread, including the initial issue submission, for this request, not just the latest update. Category: win32 Group: None Status: Open Resolution: None Priority: 5 Private: No Submitted By: VokinLoksar (vokinloksar) Assigned to: Nobody/Anonymous (nobody) Summary: Potential problem with win32file.ReadFile and async I/O Initial Comment: Hello, I'm using ReadFile on a serial port handle opened in overlapped mode. A buffer is pre-allocated with AllocateReadBuffer and passed to ReadFile. In this scenario one of two things will happen under normal circumstances: 1. The call will return immediately with ERROR_IO_PENDING status and I would then use GetOverlappedResult to wait for the operation to finish and determine how much of the buffer contains valid data. 2. The call may, in some cases, finish synchronously. The returned status is 0 in this case. The problem is that, according to Microsoft's documentation, GetOverlappedResult should NOT be used in the second case. Here's the exact quote: "If, on the other hand, an operation is completed immediately, then &NumberOfBytesRead passed into ReadFile is valid for the number of bytes read. In this case, ignore the OVERLAPPED structure passed into ReadFile; do not use it with GetOverlappedResult or WaitForSingleObject." Source: http://support.microsoft.com/kb/156932 Unfortunately, the way ReadFile is implemented in win32file.i, there is no way for python code to get access to the numRead variable, as it is called there. That variable is used correctly when I pass in the number of bytes to read for the second argument, but not when using a pre-allocated buffer. A simple solution would be to return numRead instead of the original buffer object when ReadFile finishes with a status of 0. I should note that using GetOverlappedResult does seem to work in the second case, but I'm worried if there are some situations where using it would be a bad idea. With serial ports, a simple way of getting ReadFile to behave synchronously, even when an overlapped structure is passed in, is to set a read timeout of 0 and wait for some incoming data to be buffered. In this case, the driver will usually complete the request without going into asynchronous mode. ---------------------------------------------------------------------- You can respond by visiting: https://sourceforge.net/tracker/?func=detail&atid=551954&aid=2800605&group_id=78018 |