GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Possible Memory Leak after upgrading from Python 3.12 to 3.14.2 · Issue #1122 · sendgrid/sendgrid-python

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

New issue
Copy link
New issue
Copy link
Open
Open
Possible Memory Leak after upgrading from Python 3.12 to 3.14.2#1122
Copy link

Description

@franciscopaglia
franciscopaglia
opened on Dec 17, 2025
Issue body actions

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 SendGridAPIClient.send() method call for sanity, we confirmed the leak disappears.

Reacted by Vlad Strelnikov

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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

      You are visitor 01402503 — GeezerVison does not store the pages you look up.
      Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.