Skip to content

Reset IMAP state to AUTH after unselect_folder() (fixes #639)#644

Open
apoorvdarshan wants to merge 1 commit into
mjs:masterfrom
apoorvdarshan:fix-unselect-state-639
Open

Reset IMAP state to AUTH after unselect_folder() (fixes #639)#644
apoorvdarshan wants to merge 1 commit into
mjs:masterfrom
apoorvdarshan:fix-unselect-state-639

Conversation

@apoorvdarshan

Copy link
Copy Markdown

Summary

unselect_folder() leaves the connection in the SELECTED state, so a subsequent enable() raises IllegalStateError (issue #639):

imap.select_folder("INBOX")
imap.unselect_folder()
imap.enable("UTF8=ACCEPT")   # -> IllegalStateError: ENABLE command illegal in state SELECTED

The root cause is that unselect_folder() calls self._imap._simple_command("UNSELECT") directly and never resets self._imap.state back to "AUTH". In contrast, imaplib.IMAP4.unselect() sets self.state = "AUTH" when the command succeeds, and close_folder() (which goes through imaplib's close) works correctly for the same reason.

Fix

After a successful UNSELECT (typ == "OK"), set self._imap.state = "AUTH", mirroring imaplib.IMAP4.unselect(). This returns the connection to the authenticated state so enable() and other AUTH-state commands are legal again. The change is localized to unselect_folder() in imapclient/imapclient.py.

Tests

Added test_unselect_resets_state_to_auth in tests/test_imapclient.py (alongside the existing test_unselect). It sets the mock state to "SELECTED", calls unselect_folder(), and asserts the state is reset to "AUTH". The test fails on the pristine code and passes with the fix. Full suite: 268 tests pass. black, isort, and flake8 are clean on the changed files.

Disclosure: prepared with AI assistance; reviewed and verified locally.

unselect_folder() called _simple_command('UNSELECT') directly but never
reset self._imap.state back to 'AUTH', unlike imaplib.IMAP4.unselect().
The client therefore stayed in the SELECTED state, so a subsequent
enable() raised IllegalStateError.

Set state to 'AUTH' after a successful UNSELECT, mirroring imaplib.

Fixes mjs#639.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant