Skip to content

Reconnect SMTP hooks after leaving a context manager - #74416

Merged
eladkal merged 2 commits into
apache:mainfrom
shivaam:codex/smtp-hook-reconnect
Oct 8, 2026
Merged

eladkal merged 2 commits into
apache:mainfrom
shivaam:codex/smtp-hook-reconnect

Conversation

@shivaam

@shivaam shivaam commented Oct 7, 2026

Copy link
Copy Markdown
Contributor

Reusing an SmtpHook across two context-manager blocks retains the closed SMTP client and skips connection setup on the second use. This also affects repeated calls to an SmtpNotifier, which caches its hook.

Clear the client reference when synchronous or asynchronous context cleanup finishes, including when close() or quit() raises, so the next use reconnects. Cleanup errors still propagate to the caller.

Regression tests cover repeated sync/async sends, cleanup failures, and repeated notifications using the notifier's real cached hook. All five new cases fail without the fix.

Validation: full SMTP unit suite (59 passed), selected core and provider DB/non-DB regressions (including 299 core and 1,081 provider non-DB passes), hook and all-provider type checks, regular/manual repository hooks, and SMTP/common.compat documentation builds and spell checks. Related setup-failure work is in #74185; this change covers cleanup after context exit.


Was generative AI tooling used to co-author this PR?
  • Yes — Codex (GPT-6)

Generated-by: Codex (GPT-6) following the guidelines

Repeated notifications can reuse a cached hook whose SMTP session was
closed by its previous context. Retaining that client makes the next
context skip connection setup and attempt another send on a closed session.

The hook must discard the client on context exit, including cleanup
failures, so subsequent use can establish a fresh session. Keep cleanup
exceptions visible rather than suppressing failures to make reuse work.

Regression tests cover synchronous and asynchronous context reuse,
close/quit failures, and repeated notification through the cached hook.
Use the existing async SMTP test double so lifecycle assertions do not enforce a newer third-party constructor signature. Set notification connection data as portable JSON instead of requiring SDK Connection.as_json on Airflow 3.0.6.

Both failed CI environments are covered locally. All five corrected regressions still fail when context-exit client invalidation is removed, so the compatibility correction preserves their ability to catch the original bug. No production behavior or dependency floors are changed.
@shivaam
shivaam marked this pull request as ready for review October 8, 2026 04:42
@shivaam
shivaam requested a review from hussein-awala as a code owner October 8, 2026 04:42
@eladkal
eladkal merged commit 9c9346c into apache:main Oct 8, 2026
154 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants