Description
Bug report
Bug description:
Pull request #122233 introduced a new class HeaderWriteError in commit 0976339 and imports that from email.generator.
This breaks running applications that have imported other parts of email before the update, and then try to import the generator past the update.
Now this is a bit silly, but it is what email.message.Message.as_string() does, it imports email.generator inside the function - which may happen at any point of the program run-time rather than at startup.
For example, the following pseudo-code will fail, assuming it has not generated another email earlier or manually imported the email.generator module.
import email.message
<do something for a long time, such as wait for a web form, Python is being upgraded here>
msg = <prepare a message>
msg.as_string()A particular instance of the issue is the unattended-upgrades package in Ubuntu and Debian, which will install the security update and then may send an email and fail there due to the ImportError, see https://bugs.launchpad.net/ubuntu/+source/python3.8/+bug/2080940.
I'm wondering if it's feasible to add a workaround to the stable branches:
Cchange the email.generator module import:
from email.errors import HeaderWriteErrorto graciously support the previous version email.errors:
try:
from email.errors import HeaderWriteError
except ImportError:
from email.errors import MessageError as HeaderWriteErrorThis is a safe change, existing applications, where the import fails can't be having except HeaderWriteError statements anyway.
Thanks.
CPython versions tested on:
3.12
Operating systems tested on:
No response
Activity
julian-klode commentedon Sep 17, 2024
I think the real fix is to not lazily import modules like this in the standard library, but I assume there's a reason email.generators is lazily imported?
jimkoeleman commentedon Sep 18, 2024
Since yesterday I have the same error on my servers, using Python 3.8. It happens to crash my unicorn workers resulting in bad gateway errors for end users. Until I manually restart my servers. Happened yesterday and also today in the morning.
muzomer commentedon Sep 18, 2024
It happened to one of our servers as well. Running:
We upgraded recently to Django 4.2.16, commit django/django@bf4888d might be related.
Error
jimkoeleman commentedon Sep 18, 2024
We run on Django 3.2.9 (..i know), got the following error log. Still no solution found for the problem.
JelleZijlstra commentedon Sep 18, 2024
It sounds like those of you who were getting this error were upgrading Python on the file system without restarting your processes. If so, you should be able to get around the issue by restarting all Python processes to have the new code.
jimkoeleman commentedon Sep 18, 2024
Thanks for your reply. Hmm this could mean that the server restart solved the problem. In our setup with 4 web servers it could hypothetically be that yesterday server-1 and server-2 were automatically updated, and server-3 and server-4 today so that it should not happen anymore after today. Let's wait until tomorrow morning to see if this assumption is correct.
DanielRuf commentedon Sep 23, 2024
The question is, is a manual restart needed because unattended-upgrades did not do some restart and this error state will persist until the restart is done?
What are the possible options for current servers? So far I understand:
/usr/bin/python3 /usr/bin/networkd-dispatcher --run-startup-triggersand/usr/bin/python3 /usr/share/unattended-upgrades/unattended-upgrade-shutdown --wait-for-signalare correctly started again?)Does the error happen again on systewms which are still running but everything was upgraded by
unattended-upgrades?frewper commentedon Oct 22, 2024
how can one disable this automatic update ?
u-u: reload the "email.errors" package to workaround (LP: 2080940)
u-u: reload the "email.errors" package to workaround (LP: 2080940)
u-u: reload the "email.errors" package to workaround (LP: 2080940)
u-u: import "email" package late to workaround (LP: 2080940)
u-u: import "email" package late to workaround (LP: 2080940)
u-u: import "email.genators" package early to workaround
u-u: import "email.genators" package early as workaround
u-u: import "email.genators" package early as workaround