This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: imaplib behaviour when deleting selected folder
Type: behavior Stage: resolved
Components: email, Library (Lib) Versions: Python 3.3, Python 3.4, Python 2.7
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: barry, dveeden, python-dev, r.david.murray
Priority: normal Keywords: patch

Created on 2013-12-18 08:34 by dveeden, last changed 2022-04-11 14:57 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
test_san.py dveeden, 2013-12-18 13:21 Test script
imaplib_issue20013_san.log dveeden, 2013-12-18 13:22 Test log
check_bye.patch r.david.murray, 2013-12-18 14:37 review
check_bye.patch r.david.murray, 2013-12-18 15:33 review
Messages (10)
msg206501 - (view) Author: Daniël van Eeden (dveeden) Date: 2013-12-18 08:34
When executing DELETE against a SELECTED imap folder the server responds:
---------------------------
a8 SELECT SentMail
* FLAGS (\Answered \Flagged \Deleted \Seen \Draft)
* OK [PERMANENTFLAGS (\Answered \Flagged \Deleted \Seen \Draft \*)] Flags permitted.
* 0 EXISTS
* 0 RECENT
* OK [UIDVALIDITY 1386063387] UIDs valid
* OK [UIDNEXT 1] Predicted next UID
* OK [NOMODSEQ] No permanent modsequences
a8 OK [READ-WRITE] Select completed (0.001 secs).
a9 delete SentMail
a9 OK Delete completed.
* BYE Selected mailbox was deleted, have to disconnect.
Connection closed by foreign host.
---------------------------

If the same is done with imaplib the exception does not clearly
indicate what happened.

  File "/usr/lib/python2.7/imaplib.py", line 649, in select
    typ, dat = self._simple_command(name, mailbox)
  File "/usr/lib/python2.7/imaplib.py", line 1070, in _simple_command
    return self._command_complete(name, self._command(name, *args))
  File "/usr/lib/python2.7/imaplib.py", line 899, in _command_complete
    raise self.abort('command: %s => %s' % (name, val))
imaplib.abort: command: SELECT => socket error: EOF
msg206513 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2013-12-18 12:54
Looks like a bug in the RFC, not including DELETE of current mailbox in the things that cause a transition back to 'authenticated' state.

But yes, a clearer error message would be good.  The code should be handling this correctly, so something odd is going on.  Can you provide an imaplib debug level 5 trace of the session?  That should show the network conversation from imaplib's point of view, as well as a bit about its internal state.

In your imaplib case, I presume the error occurs when you try to issue a new select command?
msg206518 - (view) Author: Daniël van Eeden (dveeden) Date: 2013-12-18 13:22
I've replace the user and password in both the test script and the log.
msg206520 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2013-12-18 14:37
Please try the attached patch and let me know if it improves things.

If it works, figuring out how to write a test for this is going to be the hard part.  The imaplib tests don't have very good coverage, even in python3 where there are at least more of them.
msg206521 - (view) Author: Daniël van Eeden (dveeden) Date: 2013-12-18 14:44
The patch doesn't seem to change the behaviour.
msg206524 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2013-12-18 15:33
Ah, I see.  The BYE isn't issued until the next command is successfully transmitted.  I'd call that a broken server, but obviously it's what you have to deal with, so we should handle it.

Try this version.
msg206584 - (view) Author: Daniël van Eeden (dveeden) Date: 2013-12-19 07:06
Without the patch:
Traceback (most recent call last):
  File "./test.py", line 10, in <module>
    s.select('INBOX')
  File "/usr/lib/python2.7/imaplib.py", line 649, in select
    typ, dat = self._simple_command(name, mailbox)
  File "/usr/lib/python2.7/imaplib.py", line 1070, in _simple_command
    return self._command_complete(name, self._command(name, *args))
  File "/usr/lib/python2.7/imaplib.py", line 899, in _command_complete
    raise self.abort('command: %s => %s' % (name, val))
imaplib.abort: command: SELECT => socket error: EOF

With the patch:
Traceback (most recent call last):
  File "./test.py", line 10, in <module>
    s.select('INBOX')
  File "/usr/lib/python2.7/imaplib.py", line 649, in select
    typ, dat = self._simple_command(name, mailbox)
  File "/usr/lib/python2.7/imaplib.py", line 1075, in _simple_command
    return self._command_complete(name, self._command(name, *args))
  File "/usr/lib/python2.7/imaplib.py", line 899, in _command_complete
    raise self.abort('command: %s => %s' % (name, val))
imaplib.abort: command: SELECT => Selected mailbox was deleted, have to disconnect.

I'm really happy with this new behaviour.
msg210518 - (view) Author: Roundup Robot (python-dev) (Python triager) Date: 2014-02-07 18:45
New changeset a24f7d195b8f by R David Murray in branch 'default':
#20013: don't raise socket error when selected mailbox deleted.
http://hg.python.org/cpython/rev/a24f7d195b8f
msg210521 - (view) Author: Roundup Robot (python-dev) (Python triager) Date: 2014-02-07 18:51
New changeset aecc0a4be052 by R David Murray in branch '3.3':
#20013: don't raise socket error when selected mailbox deleted.
http://hg.python.org/cpython/rev/aecc0a4be052

New changeset 16af3de2240f by R David Murray in branch '2.7':
#20013: don't raise socket error when selected mailbox deleted.
http://hg.python.org/cpython/rev/16af3de2240f
msg210522 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2014-02-07 18:53
Fix applied without tests, as I haven't found time to write them, given the poor existing test coverage of this module.
History
Date User Action Args
2022-04-11 14:57:55adminsetgithub: 64212
2014-02-07 18:53:43r.david.murraysetstatus: open -> closed
versions: + Python 3.3, Python 3.4
messages: + msg210522

resolution: fixed
stage: resolved
2014-02-07 18:51:54python-devsetmessages: + msg210521
2014-02-07 18:45:44python-devsetnosy: + python-dev
messages: + msg210518
2013-12-19 07:06:15dveedensetmessages: + msg206584
2013-12-18 15:33:33r.david.murraysetfiles: + check_bye.patch

messages: + msg206524
2013-12-18 14:44:50dveedensetmessages: + msg206521
2013-12-18 14:37:43r.david.murraysetfiles: + check_bye.patch
keywords: + patch
messages: + msg206520
2013-12-18 13:22:26dveedensetfiles: + imaplib_issue20013_san.log

messages: + msg206518
2013-12-18 13:21:55dveedensetfiles: + test_san.py
2013-12-18 12:54:44r.david.murraysetnosy: + barry, r.david.murray
messages: + msg206513

components: + email
type: behavior
2013-12-18 08:34:20dveedencreate