Skip to content
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

Introduce a context manager that closes opened connections #279

Merged
merged 1 commit into from
Aug 31, 2017

Conversation

NicolasLM
Copy link
Collaborator

To prevent users of the library from inadvertently leaking
sockets.

Fixes #276

@NicolasLM NicolasLM requested a review from mjs August 28, 2017 11:30
@mjs mjs added this to the 2.0.0 milestone Aug 29, 2017
Copy link
Owner

@mjs mjs left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks great. Thank you for your attention to detail as always.

However if an error is raised when selecting the folder, the connection may be
left open.

IMAPClient provides a context manager that automatically closes
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"IMAPClient may be used as a context manager... " might be slightly better?

def __exit__(self, exc_type, exc_val, exc_tb):
"""Logout and closes the connection when exiting the context manager.

All exceptions are caught because an error in `logout` or `shutdown`
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"All exceptions during logout and connection shutdown are caught because an error here usually means the connection was already closed."

mock_logger.info.assert_called_once_with(
'Could not close the connection cleanly: %s',
self.client._imap.shutdown.side_effect
)
Copy link
Owner

@mjs mjs Aug 29, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please consider one more test where an exception occurs inside the with block. We want to be sure it isn't suppressed by the context manager.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

@@ -3,6 +3,33 @@ Advanced Usage
This document covers some more advanced features and tips for handling
specific usages.

Prevent leaving unclosed connections
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"Cleaning Up Connections" might be better

To prevent users of the library from inadvertently leaking
sockets.

Fixes mjs#276
@mjs mjs merged commit 6330970 into mjs:master Aug 31, 2017
@mjs
Copy link
Owner

mjs commented Aug 31, 2017

Awesome stuff. Thank you!

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.

2 participants