Ministack: Free, open-source local AWS emulator - 60+ services, Terraform compatible, real databases. Free forever. MIT licensed.
Python awsaws-emulatoraws-localaws-sdkdevtoolsdockerdynamodbec2emulatorlambdalocalstacklocalstack-alternativeministackmock-awsopen-sourcepythons3sqsterraform
fix(SMTP): relay has no timeout (#2029)
Nahuel Nuceraf8a9e33b7dbf
diff --git a/ministack/services/ses.py b/ministack/services/ses.pyindex 0aee7d98..daa1fcdd 100644--- a/ministack/services/ses.py+++ b/ministack/services/ses.py@@ -36,6 +36,7 @@ from email.policy import default as default_policyfrom email.utils import parseaddrfrom urllib.parse import parse_qs+from ministack.core.concurrency import spawn_backgroundfrom ministack.core.responses import (AccountRegionScopedDict,AccountScopedDict,@@ -846,14 +847,21 @@ def _build_mime_message(source, to_addrs, cc_addrs, bcc_addrs,return msg.as_string()+_SMTP_TIMEOUT_SECONDS = 10++def _smtp_relay(source, to_addrs, message_str):- """Relay email via external SMTP if SMTP_HOST is set. Best-effort."""+ """Relay email via external SMTP if SMTP_HOST is set. Best-effort, off the event loop."""endpoint = _parse_smtp_host()if not endpoint:return- host, port = endpoint+ spawn_background(_smtp_send, source, to_addrs, message_str, *endpoint,+ thread_name="ministack-ses-smtp")+++def _smtp_send(source, to_addrs, message_str, host, port):try:- with smtplib.SMTP(host, port) as conn:+ with smtplib.SMTP(host, port, timeout=_SMTP_TIMEOUT_SECONDS) as conn:conn.sendmail(source, to_addrs, message_str)logger.info('SMTP relay: %s -> %s via %s:%d', source, to_addrs, host, port)except Exception:diff --git a/tests/test_ses.py b/tests/test_ses.pyindex 0c9455b6..69594c9f 100644--- a/tests/test_ses.py+++ b/tests/test_ses.py@@ -2,6 +2,7 @@import jsonimport os+import timeimport uuid as _uuid_modfrom unittest.mock import MagicMock, patch@@ -310,6 +311,13 @@ def test_ses_v2_send_email_with_v1_template(ses, sesv2):assert sent[0]["Subject"] == "Hello Alice"assert sent[0]["BodyText"] == "Hi Alice, order #42"[email protected](autouse=True)+def _inline_smtp_relay(monkeypatch):+ """Run the background SMTP relay inline so tests can assert on it."""+ monkeypatch.setattr('ministack.services.ses.spawn_background',+ lambda fn, *args, **_kw: fn(*args))++@pytest.fixture(autouse=True)def _clear_smtp_host():"""Ensure SMTP_HOST is clean before/after each test."""@@ -389,12 +397,28 @@ def test_ses_smtp_relay_sends_when_host_set():mock_smtp.__enter__ = MagicMock(return_value=mock_smtp)mock_smtp.__exit__ = MagicMock(return_value=False)_smtp_relay('[email protected]', ['[email protected]'], 'message body')- mock_cls.assert_called_once_with('127.0.0.1', 1025)+ mock_cls.assert_called_once_with('127.0.0.1', 1025, timeout=10)mock_smtp.sendmail.assert_called_once_with('[email protected]', ['[email protected]'], 'message body',)+def test_ses_smtp_relay_does_not_block_the_caller(monkeypatch):+ """An unreachable SMTP_HOST must not stall the request that sent the email."""+ import threading++ from ministack.core.concurrency import spawn_background+ from ministack.services.ses import _smtp_relay+ monkeypatch.setattr('ministack.services.ses.spawn_background', spawn_background)+ os.environ['SMTP_HOST'] = '127.0.0.1:1025'+ release = threading.Event()+ with patch('ministack.services.ses.smtplib.SMTP', side_effect=lambda *a, **k: release.wait(5)):+ start = time.monotonic()+ _smtp_relay('[email protected]', ['[email protected]'], 'message')+ assert time.monotonic() - start < 1+ release.set()++def test_ses_smtp_relay_error_is_logged_not_raised():os.environ['SMTP_HOST'] = '127.0.0.1:1025'from ministack.services.ses import _smtp_relay