[GH-ISSUE #213] smtpd gives up after a couple of emails #156

Closed
opened 2026-02-25 23:41:23 +03:00 by kerem · 4 comments
Owner

Originally created by @danielfaulknor on GitHub (Jan 22, 2019).
Original GitHub issue: https://github.com/healthchecks/healthchecks/issues/213

I'm running smtpd in a docker container:

set -e -x

setcap 'cap_net_bind_service=+ep' /usr/bin/python3.6

su -c "python3 /src/manage.py smtpd --port=25" hc

It connects up fine, and takes a few email pings, then these start hitting the logs. My CONN_MAX_AGE is 0.

error: uncaptured python exception, closing channel <smtpd.SMTPChannel connected 50.31.156.116:35051 at 0x7feda0e9f4e0> (<class 'django.db.utils.OperationalError'>:(2006, 'MySQL server has gone away') [/usr/lib/python3.6/asyncore.py|read|83] [/usr/lib/python3.6/asyncore.py|handle_read_event|423] [/usr/lib/python3.6/asynchat.py|handle_read|171] [/usr/lib/python3.6/smtpd.py|found_terminator|386] [/src/hc/api/management/commands/smtpd.py|process_message|25] [/usr/local/lib/python3.6/dist-packages/django/db/models/manager.py|manager_method|82] [/usr/local/lib/python3.6/dist-packages/django/db/models/query.py|get|393] [/usr/local/lib/python3.6/dist-packages/django/db/models/query.py|__len__|250] [/usr/local/lib/python3.6/dist-packages/django/db/models/query.py|_fetch_all|1183] [/usr/local/lib/python3.6/dist-packages/django/db/models/query.py|__iter__|54] [/usr/local/lib/python3.6/dist-packages/django/db/models/sql/compiler.py|execute_sql|1061] [/usr/local/lib/python3.6/dist-packages/django/db/backends/utils.py|execute|68] [/usr/local/lib/python3.6/dist-packages/django/db/backends/utils.py|_execute_with_wrappers|77] [/usr/local/lib/python3.6/dist-packages/django/db/backends/utils.py|_execute|85] [/usr/local/lib/python3.6/dist-packages/django/db/utils.py|__exit__|89] [/usr/local/lib/python3.6/dist-packages/django/db/backends/utils.py|_execute|85] [/usr/local/lib/python3.6/dist-packages/django/db/backends/mysql/base.py|execute|71] [/usr/lib/python3/dist-packages/MySQLdb/cursors.py|execute|250] [/usr/lib/python3/dist-packages/MySQLdb/connections.py|defaulterrorhandler|50] [/usr/lib/python3/dist-packages/MySQLdb/cursors.py|execute|247] [/usr/lib/python3/dist-packages/MySQLdb/cursors.py|_query|411] [/usr/lib/python3/dist-packages/MySQLdb/cursors.py|_do_query|374] [/usr/lib/python3/dist-packages/MySQLdb/connections.py|query|292])

I guess it's holding the connection open, and mysqld goes away. This results in emails being rejected.

Originally created by @danielfaulknor on GitHub (Jan 22, 2019). Original GitHub issue: https://github.com/healthchecks/healthchecks/issues/213 I'm running smtpd in a docker container: ```#!/bin/sh set -e -x setcap 'cap_net_bind_service=+ep' /usr/bin/python3.6 su -c "python3 /src/manage.py smtpd --port=25" hc ``` It connects up fine, and takes a few email pings, then these start hitting the logs. My `CONN_MAX_AGE` is 0. ``` error: uncaptured python exception, closing channel <smtpd.SMTPChannel connected 50.31.156.116:35051 at 0x7feda0e9f4e0> (<class 'django.db.utils.OperationalError'>:(2006, 'MySQL server has gone away') [/usr/lib/python3.6/asyncore.py|read|83] [/usr/lib/python3.6/asyncore.py|handle_read_event|423] [/usr/lib/python3.6/asynchat.py|handle_read|171] [/usr/lib/python3.6/smtpd.py|found_terminator|386] [/src/hc/api/management/commands/smtpd.py|process_message|25] [/usr/local/lib/python3.6/dist-packages/django/db/models/manager.py|manager_method|82] [/usr/local/lib/python3.6/dist-packages/django/db/models/query.py|get|393] [/usr/local/lib/python3.6/dist-packages/django/db/models/query.py|__len__|250] [/usr/local/lib/python3.6/dist-packages/django/db/models/query.py|_fetch_all|1183] [/usr/local/lib/python3.6/dist-packages/django/db/models/query.py|__iter__|54] [/usr/local/lib/python3.6/dist-packages/django/db/models/sql/compiler.py|execute_sql|1061] [/usr/local/lib/python3.6/dist-packages/django/db/backends/utils.py|execute|68] [/usr/local/lib/python3.6/dist-packages/django/db/backends/utils.py|_execute_with_wrappers|77] [/usr/local/lib/python3.6/dist-packages/django/db/backends/utils.py|_execute|85] [/usr/local/lib/python3.6/dist-packages/django/db/utils.py|__exit__|89] [/usr/local/lib/python3.6/dist-packages/django/db/backends/utils.py|_execute|85] [/usr/local/lib/python3.6/dist-packages/django/db/backends/mysql/base.py|execute|71] [/usr/lib/python3/dist-packages/MySQLdb/cursors.py|execute|250] [/usr/lib/python3/dist-packages/MySQLdb/connections.py|defaulterrorhandler|50] [/usr/lib/python3/dist-packages/MySQLdb/cursors.py|execute|247] [/usr/lib/python3/dist-packages/MySQLdb/cursors.py|_query|411] [/usr/lib/python3/dist-packages/MySQLdb/cursors.py|_do_query|374] [/usr/lib/python3/dist-packages/MySQLdb/connections.py|query|292])``` I guess it's holding the connection open, and mysqld goes away. This results in emails being rejected.
kerem closed this issue 2026-02-25 23:41:23 +03:00
Author
Owner

@danielfaulknor commented on GitHub (Jan 23, 2019):

If anyone finds this... There's a Lambda function in https://github.com/healthchecks/healthchecks/issues/139

I've switched to using that.

<!-- gh-comment-id:456975054 --> @danielfaulknor commented on GitHub (Jan 23, 2019): If anyone finds this... There's a Lambda function in https://github.com/healthchecks/healthchecks/issues/139 I've switched to using that.
Author
Owner

@cuu508 commented on GitHub (Feb 21, 2019):

I'm wondering if "MySQL server has gone away" happens because of a connection being idle for too long.

Did you also see similar errors when running manage.py sendalerts? sendalerts is similar in that it is also a management command and it also doesn't do anything special with connection handling. (but, it runs queries every few seconds)

<!-- gh-comment-id:466125572 --> @cuu508 commented on GitHub (Feb 21, 2019): I'm wondering if "MySQL server has gone away" happens because of a connection being idle for too long. Did you also see similar errors when running `manage.py sendalerts`? sendalerts is similar in that it is also a management command and it also doesn't do anything special with connection handling. (but, it runs queries every few seconds)
Author
Owner

@danielfaulknor commented on GitHub (Feb 26, 2019):

sendalerts is fine yeah

<!-- gh-comment-id:467617692 --> @danielfaulknor commented on GitHub (Feb 26, 2019): sendalerts is fine yeah
Author
Owner

@cuu508 commented on GitHub (Feb 27, 2019):

So my theory is that the manage.py smtpd is opening a single database connection and expecting to use it indefinitely. If the emails are infrequent then the connection is idle for long periods and gets closed – either by kernel or by mysql, not sure.

I added a workaround to get a new db connection on every email. I'm closing this issue for now, and we'll see if the problem resurfaces.

<!-- gh-comment-id:467891751 --> @cuu508 commented on GitHub (Feb 27, 2019): So my theory is that the `manage.py smtpd` is opening a single database connection and expecting to use it indefinitely. If the emails are infrequent then the connection is idle for long periods and gets closed – either by kernel or by mysql, not sure. I added a workaround to get a new db connection on every email. I'm closing this issue for now, and we'll see if the problem resurfaces.
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
starred/healthchecks#156
No description provided.