New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
imaplib.IMAP4_stream subprocess is opened unbuffered but ignores short reads #61645
Comments
imaplib.IMAP4_stream subprocess is opened unbuffered but ignores short reads when reading the message body. Depending on timing, message body size and kernel pipe buffer size and phase of the moon and whether you're debugging the thing or not... It can fail to read the entire message body before wrongly assuming it has and attempting to read the terminating b')\r\n' of the IMAP protocol. Bug discovered during a debugging session at the PyCon 2013 Python 3 Porting Clinic BOF. |
The error does not happen when running the same code under 2.7, despite the same default bufsize=0 subprocess behavior. This is likely due to differences in the Python 2.x old style io library when os.fdopen(fd, 'rb', bufsize) is used vs 3.x when io.open(fd, 'rb', bufsize) is used for Popen.stdout. One workaround is to add a non-zero bufsize to the subprocess.Popen call in imaplib.IMAP4_stream. I'm not sure if subprocess should be updated or if subprocess's docs on what it means for a pipe to be unbuffered (read(n) is a single syscall rather than a loop until n bytes or EOF) should be updated. |
os.fdopen() in 2.x would always create a FILE*, and therefore inherit fread()'s semantics even in "unbuffered" mode. In 3.x, unbuffered I/O instead calls read() directly, and happily returns partial reads; this is by design. So, I guess imaplib should be fixed :-) |
I don't think there's any reason to open the subprocess in unbuffered mode (you aren't sharing the stdio streams with anyone else). Just be careful to call flush() on stdin before attempting to read any response from stdout. |
Yes imaplib can be fixed pretty easily and should use buffered IO regardless. I'm pondering if the default behavior of subprocess needs fixing as Thankfully my subprocess32 backport on 2.x doesn't suffer from the |
So as a first stab at fixing this. I modified imaplib to wrap the process.stdin / process.stdout from with io.BufferedWriter / io.BufferedReader. I didn't use the TextIOWrapper as the imaplib wanted to work with the raw \r\n. The change seems to have fixed the problem I was having, I also checked out 82724:ef8ea052bcc4 and tried running "./python -m test -j3 " before and after the buffer wrapping and it didn't seem to trigger any test case failures. |
After bumping into r.david.murray in the elevator I got the impression setting the bufsize argument to the Popen call would be a better idea. I found that BufferedReader/Writer were using a DEFAULT_BUFFER_SIZE set somewhere in the c part of io. To cut down on magic numbers, this imaplib patch imports that constant and uses it on the Popen call. It doesn't seem to introduce test failures and still fixes the imap desynchronization problem seen at the porting clinic. |
that patch looks good for imaplib. i'll follow up on the subprocess side of things to see if the default |
New changeset c5aacf9d1cdc by R David Murray in branch '3.2': New changeset 0baa65b3ef76 by R David Murray in branch '3.3': New changeset 4c6463b96a2c by R David Murray in branch 'default': |
Thanks, Diane, and expecially thanks for finding this and helping is track down the cause. We need better test infrastructure for imap...because this occurs only during string litteral reads, I decided that making a test for this with our current imap test infrastructure just wasn't worth it. |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: