Skip to content

Possible Memory Leak after upgrading from Python 3.12 to 3.14.2 #1122

@franciscopaglia

Description

@franciscopaglia

Version: '6.12.5'

After upgrading to the latest Python version, we observed increasing memory usage when sending emails in a tight loop using the SendGrid Python client. With ~3,000 sends in a simple function, process memory grows steadily and does not appear to stabilize.

Image
...
from django.conf import settings
from monitoring.instrumentation import Instrumentation
from sendgrid import Attachment as SendGridAttachment
from sendgrid import Category, Mail, SendGridAPIClient
...

class SendgridMailClient(MailClient):
    def __init__(self) -> None:
        self._templates: dict = settings.EMAIL_TEMPLATES
        self._instrumentation = SendgridMailClientInstrumentation(Instrumentation())
        self._client = SendGridAPIClient(settings.SENDGRID_API_KEY)

    def send_new(self, email_info: MailInfo) -> None:
        email_settings = settings.EMAIL_TEMPLATES[email_info.template_name]

        email_message = Mail(
            from_email=email_settings["from_email"],
            to_emails=email_info.to_email,
        )

        email_message.template_id = email_settings["template_id"][email_info.language_code]

        email_message.category = [Category("transactional"), Category(email_info.template_name)]

        self._add_context(email_info=email_info, email_message=email_message)

        self._add_attachments(email_message=email_message, attachments=email_info.attachments)

        try:
            self._client.send(email_message)
            Instrumentation().logger.info(
                "[SendgridMailClient] Email sent",
                language_code=email_info.language_code,
                template_name=email_info.template_name,
                context=json.dumps(email_info.context),
            )
        except Exception as exception:
            self._instrumentation.email_send_error_new(exception, email_info)
            raise exception

    def _add_attachments(self, email_message: Mail, attachments: list[Attachment]) -> None:
        for attachment in attachments:
            sendgrid_attachment: SendGridAttachment = self._map_sendgrid_attachment(attachment)

            email_message.add_attachment(sendgrid_attachment)

    def _add_context(self, email_info: MailInfo, email_message: Mail) -> None:
        context: dict[str, Any] = email_info.context

        email_message.dynamic_template_data = context

    def _map_sendgrid_attachment(self, attachment: Attachment) -> SendGridAttachment:
        encoded_file_content = base64.b64encode(attachment.content).decode()

        return SendGridAttachment(
            file_content=encoded_file_content,
            file_type=attachment.mimetype,
            file_name=attachment.name,
        )

After removing the _client.send method call for sanity, also confirmed leak disappears.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions