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

error responses from server are masked in smtplib when server closes connection #61700

Closed
bitdancer opened this issue Mar 20, 2013 · 6 comments
Closed
Labels
type-bug An unexpected behavior, bug, or error

Comments

@bitdancer
Copy link
Member

BPO 17498
Nosy @bitdancer, @kushaldas
Files
  • issue17498.patch
  • issue17498_v2.patch: New version of the patch which can be successfully applied to tip.
  • 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:

    assignee = None
    closed_at = <Date 2014-04-14.22:24:41.257>
    created_at = <Date 2013-03-20.20:48:30.070>
    labels = ['type-bug']
    title = 'error responses from server are masked in smtplib when server closes connection'
    updated_at = <Date 2014-04-14.22:24:41.255>
    user = 'https://github.com/bitdancer'

    bugs.python.org fields:

    activity = <Date 2014-04-14.22:24:41.255>
    actor = 'r.david.murray'
    assignee = 'none'
    closed = True
    closed_date = <Date 2014-04-14.22:24:41.257>
    closer = 'r.david.murray'
    components = []
    creation = <Date 2013-03-20.20:48:30.070>
    creator = 'r.david.murray'
    dependencies = []
    files = ['29521', '34817']
    hgrepos = []
    issue_num = 17498
    keywords = ['patch']
    message_count = 6.0
    messages = ['184793', '184810', '184829', '216089', '216235', '216237']
    nosy_count = 3.0
    nosy_names = ['r.david.murray', 'python-dev', 'kushal.das']
    pr_nums = []
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue17498'
    versions = ['Python 3.4', 'Python 3.5']

    @bitdancer
    Copy link
    Member Author

    If an SMTP server returns an error code and then closes the connection, smtplib will attempt to send an RSET command, which will then raise a socket error, which smtplib turns into a ServerDisconnectedError, thereby loosing the information about what error code the server actually returned. While (except for code 421, as noted in bpo-5713) a server doing this is technically out of spec with the RFC, it is a reality of the internet that it happens, and smtplib should handle it correctly.

    Since the RSET is just to get the protocol back to a known starting state after an error, I believe the fix is to have an internal _rset method that wraps the RSET call in a try/except and just ignores any ServerDiscoonected error. The library would then call this internally instead of the public API method. If the library client continues on after the error code it gets back from calling an smtplib method that results in the server closing the connection, it will get the ServerDisconnected error the next time it tries to send a command to the server. Thus the behavior change introduced here is just to allow commands that end with server disconnects to return an error code instead of an (otherwise mysterious and quite possibly currently-mishandled) ServerDisconnected error. Since any existing code will already need to handle errors from commands sent to servers that *don't* disconnect, I believe that this is a safe fix from a backward compatibility standpoint.

    @kushaldas
    Copy link
    Member

    Working on a patch for this.

    @kushaldas
    Copy link
    Member

    The following patch adds a _rset method which catches any SMTPServerDisconnected exception, also includes the corresponding test and test_421_from_mail_cmd assert statement typo fix.

    @kushaldas
    Copy link
    Member

    New version of the patch which can be successfully applied to tip.

    @python-dev
    Copy link
    Mannequin

    python-dev mannequin commented Apr 14, 2014

    New changeset 3c441e9ccf87 by R David Murray in branch '3.4':
    bpo-17498: Defer SMTPServerDisconnected errors until the next command.
    http://hg.python.org/cpython/rev/3c441e9ccf87

    New changeset 842014ab1c06 by R David Murray in branch 'default':
    Merge bpo-17498: Defer SMTPServerDisconnected errors until the next command.
    http://hg.python.org/cpython/rev/842014ab1c06

    @bitdancer
    Copy link
    Member Author

    Thanks, Kushal. Sorry it took so long to get this committed :)

    @bitdancer bitdancer added the type-bug An unexpected behavior, bug, or error label Apr 14, 2014
    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    type-bug An unexpected behavior, bug, or error
    Projects
    None yet
    Development

    No branches or pull requests

    2 participants