Set a reasonable default for EMAIL_TIMEOUT

123 views
Skip to first unread message

Federico Capoano

unread,
Nov 17, 2022, 10:33:29 AM11/17/22
to Django developers (Contributions to Django itself)
Hi all,

Why is there no default for EMAIL_TIMEOUT?

Applications built in Django can potentially stall indefinitely if email sending starts to hang (eg: if the SMTP server is overloaded), when this happens, since there's no timeout, there's also no error being logged, so it's very hard and time consuming to debug.

Wouldn't it be better to set a timeout? Some high value like 2 minutes which wouldn't really make sense to wait any longer, so at least if and when this happens, developers will find error traces in the logs and quickly understand where the problem is coming from, instead of spending hours to debug it like I did in the past week.

Alternatively, we could set a default timeout only if "socket.getdefaulttimeout()" returns None, just to avoid applications stalling indefinitely, which is something that has been happening to me lately.

Best regards
Federico Capoano

Carlton Gibson

unread,
Nov 17, 2022, 11:16:45 AM11/17/22
to django-d...@googlegroups.com
Hi Federico. 

Maybe if we were starting from scratch having a default would be the right course — "maybe", I didn't think it through yet, or look at why the original decision went the way it did — but I don't think changing it is worth the disruption, or the difficulty of going through the deprecation process. 

(Why is "Just give it a value in your settings" not OK? 🤔)

Around core.mail, there's a cluster of tickets that I'm hoping to pull into a project idea for this year's GSoC — see...  

- [Add Settings EMAIL_FQDN by Jrief · Pull Request #13728 · Django/Django](https://github.com/django/django/pull/13728)

- [Ui/Django-post_office: A Django App That Allows You to Send Email Asynchronously in Django. Supports HTML Email, Database Backed Templates and Logging.](https://github.com/ui/django-post_office)

- [Proposal on How Add Configuration Options to Email Backends](https://groups.google.com/g/django-developers/c/R8ebGynQjK0)


... and linked threads. 


... and linked threads. 

One result of that (if all comes to pass) should hopefully be better customizability of your email backends, so you'd be free to set a default in your own backend, without the BC concerns for others.

So that's not a, yes, let's change it, but hopefully it doesn't leave you without optimism 🙂

Kind Regards,

Carlton

--
You received this message because you are subscribed to the Google Groups "Django developers (Contributions to Django itself)" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-develop...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-developers/CAERYH6Vs6%2BWnJMsw95Uc_b006PZ2N_Pr%3DNgx4ztPDkgjVbkivA%40mail.gmail.com.

Arthur Pemberton

unread,
Nov 17, 2022, 11:28:56 AM11/17/22
to django-d...@googlegroups.com
> (Why is "Just give it a value in your settings" not OK? 🤔)

I'm guessing the issue here is that it's currently an unknown-unknown to most/all newbies.

At the very least, it should probably be addressed in the documentation on deployment.

Regards,
Arthur Pemberton

Mariusz Felisiak

unread,
Nov 17, 2022, 12:18:28 PM11/17/22
to Django developers (Contributions to Django itself)
"Why is there no default for EMAIL_TIMEOUT?"

The Django's SMTP backend is a wrapper around smtplib.SMTP/SMTP_SSL and we only pass the "timeout" parameter to the smtplib API, so I'd first ask why there is no default value for a timeout parameter in smtplib.

Best,
Mariusz

Michiel Beijen

unread,
Nov 18, 2022, 9:28:14 AM11/18/22
to django-d...@googlegroups.com
Hi Mariusz,

> Op 17 nov. 2022, om 18:18 heeft Mariusz Felisiak <felisiak...@gmail.com> het volgende geschreven:
>
> "Why is there no default for EMAIL_TIMEOUT?"
> The Django's SMTP backend is a wrapper around smtplib.SMTP/SMTP_SSL and we only pass the "timeout" parameter to the smtplib API, so I'd first ask why there is no default value for a timeout parameter in smtplib.

Well, Django’s docs say:

> If unspecified, the default timeout will be the one provided by socket.getdefaulttimeout(), which defaults to None(no timeout).
(ref https://docs.djangoproject.com/en/4.1/topics/email/#smtp-backend)

So if you say: why is there no default value in smtplib, the smtplib maintainer can so go one level up and ask why there is no default timeout for `socket` :D

Nevertheless, I would argue that having a timeout in django, set to a rather conservative value such as 60 seconds, is much better than having no timeout at all; especially because what the OP posted:

> so at least if and when this happens, developers will find error traces in the logs and quickly understand where the problem is coming from, instead of spending hours to debug it like I did in the past week.

In the typical scenario, people are much better off with specified timeouts than with a timeout of None


Michiel

Federico Capoano

unread,
Nov 21, 2022, 3:31:27 PM11/21/22
to Django developers (Contributions to Django itself)
Hi there,

yes I can add EMAIL_TIMEOUT in my applications and I am already doing that.

The reason I wanted to bring this up here is that I care about Django.
This behavior goes against a well known concept of the Unix Philosophy which is to  "Write programs which fail in a way that is easy to diagnose".

I believe we can all agree that any potentially long running operation should have a default timeout after which the operation fails and a trace is left on a log somewhere for the system administrators to find
I can't see the benefit in not failing, not leaving log traces and keeping an app blocked indefinitely.

What are the benefits vs the downsides of such a change?

Benefit: any issue in production systems will be quickly understood and dealt with.
Downsides? Backward compatibility? Systems relying on the default timeout being set elsewhere? Anything else?

Even if we allow configuring different email backends, I am still wonderfing: why do we think it's good to have an email sending operation which can potentially stall indefinitely?

Best regards
Federico

Federico Capoano

unread,
Nov 29, 2022, 12:54:12 PM11/29/22
to Django developers (Contributions to Django itself)
Hi everyone,

do you think you are against on opening an issue for this?
I had already opened an issue which was marked as wontfix and I was asked to open this discussion here.

Best regards
Federico

Adam Johnson

unread,
Nov 29, 2022, 3:26:48 PM11/29/22
to django-d...@googlegroups.com
Federico - I think we’d like to see a few more “pro”s in the thread.

Personally, I am pro adding a default timeout. The lack of a default timeout in requests is a constant source of outages, which lead me to write this hack to patch it. httpx has learned from this by setting a default timeout.

It would be good to see some research whether a default in smtplib was ever explicitly discussed.

--
You received this message because you are subscribed to the Google Groups "Django developers (Contributions to Django itself)" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-develop...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages