Skip to content

Fixed "AttributeError: 'NoneType' object has no attribute 'close'" for SmtpHook#62409

Merged
guan404ming merged 6 commits into
apache:mainfrom
TopCoder2K:fix_smtp_connection_retry_limit
Jun 18, 2026
Merged

Fixed "AttributeError: 'NoneType' object has no attribute 'close'" for SmtpHook#62409
guan404ming merged 6 commits into
apache:mainfrom
TopCoder2K:fix_smtp_connection_retry_limit

Conversation

@TopCoder2K

@TopCoder2K TopCoder2K commented Feb 24, 2026

Copy link
Copy Markdown
Contributor

This loop inside the SmtpHook.get_conn() method isn't entered when self.smtp_retry_limit == 0. It seems enough just to set the range() to start at 0 (since self.smtp_retry_limit is a number of retries = repeated tries after the first try).

P.S.
I wasn't able to run the checks due to #62408.


Was generative AI tooling used to co-author this PR?
  • Yes

@boring-cyborg

boring-cyborg Bot commented Feb 24, 2026

Copy link
Copy Markdown

Congratulations on your first Pull Request and welcome to the Apache Airflow community! If you have any issues or are unsure about any anything please check our Contributors' Guide (https://github.com/apache/airflow/blob/main/contributing-docs/README.rst)
Here are some useful points:

  • Pay attention to the quality of your code (ruff, mypy and type annotations). Our prek-hooks will help you with that.
  • In case of a new feature add useful documentation (in docstrings or in docs/ directory). Adding a new operator? Check this short guide Consider adding an example DAG that shows how users should use it.
  • Consider using Breeze environment for testing locally, it's a heavy docker but it ships with a working Airflow and a lot of integrations.
  • Be patient and persistent. It might take some time to get a review or get the final approval from Committers.
  • Please follow ASF Code of Conduct for all communication including (but not limited to) comments on Pull Requests, Mailing list and Slack.
  • Be sure to read the Airflow Coding style.
  • Always keep your Pull Requests rebased, otherwise your build might fail due to changes not related to your commits.
    Apache Airflow is a community-driven project and together we are making it better 🚀.
    In case of doubts contact the developers at:
    Mailing List: dev@airflow.apache.org
    Slack: https://s.apache.org/airflow-slack

@SameerMesiah97 SameerMesiah97 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks fine. But let's wait for CI to run.

@guan404ming guan404ming left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your contribution.

The core fix (range(0, ...)) seems correct, but all four retry loops in the file need the same treatment. Fixing only get_conn() while leaving aget_conn(), send_email_smtp(), and asend_email_smtp() unfixed is incomplete.

@TopCoder2K

Copy link
Copy Markdown
Contributor Author

@guan404ming, oops, didn't think about them. OK, I'll try to look at them today or tomorrow. Thank you for pointing that out!

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This pull request fixes a critical bug where SmtpHook.get_conn() would not establish any SMTP connection when smtp_retry_limit is set to 0, causing an AttributeError: 'NoneType' object has no attribute 'close' when the context manager exits.

Changes:

  • Changed retry loop ranges from range(1, retry_limit + 1) to range(0, retry_limit + 1) to ensure at least one connection attempt
  • Updated test assertions to reflect the correct number of attempts (retry_limit + 1)
  • Added comprehensive test coverage for retry_limit=0 configuration

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
providers/smtp/src/airflow/providers/smtp/hooks/smtp.py Fixed retry loop logic in 4 locations (get_conn, aget_conn, send_email_smtp, asend_email_smtp) to start at attempt 0 instead of 1
providers/smtp/tests/unit/smtp/hooks/test_smtp.py Added CONN_ID_0_RETRIES test connection, added test cases for 0-retries configuration, and corrected test assertions to expect retry_limit+1 attempts

Comment thread providers/smtp/src/airflow/providers/smtp/hooks/smtp.py
@TopCoder2K

Copy link
Copy Markdown
Contributor Author

@guan404ming, could you please re-review?

@eladkal
eladkal requested a review from dabla March 9, 2026 15:34
@TopCoder2K

Copy link
Copy Markdown
Contributor Author

@eladkal, why did the 4 checks fail? All of them seem not connected to my changes.

@potiuk

potiuk commented Mar 10, 2026

Copy link
Copy Markdown
Member

You need to rebase.

@potiuk
potiuk marked this pull request as draft March 12, 2026 01:25
@potiuk

potiuk commented Mar 12, 2026

Copy link
Copy Markdown
Member

@TopCoder2K Converting to draft — this PR doesn't yet meet our Pull Request quality criteria.

  • Pre-commit / static checks: Failing: CI image checks / Static checks. Run prek run --from-ref main locally to find and fix issues. See Pre-commit / static checks docs.
  • mypy (type checking): Failing: CI image checks / MyPy checks (mypy-providers). Run prek --stage manual mypy-providers --all-files locally to reproduce. You need breeze ci-image build --python 3.10 for Docker-based mypy. See mypy (type checking) docs.
  • Build docs: Failing: CI image checks / Build documentation (--docs-only). Run breeze build-docs locally to reproduce. See Build docs docs.
  • Other failing CI checks: Failing: CI image checks / Build documentation (--spellcheck-only). Run prek run --from-ref main locally to reproduce. See static checks docs.

Note: Your branch is 539 commits behind main. Please rebase and push again to get up-to-date CI results.

See the linked criteria for how to fix each item, then mark the PR "Ready for review". This is not a rejection — just an invitation to bring the PR up to standard. No rush.


Note: This comment was drafted by an AI-assisted triage tool and may contain mistakes. Once you have addressed the points above, an Apache Airflow maintainer — a real person — will take the next look at your PR. We use this two-stage triage process so that our maintainers' limited time is spent where it matters most: the conversation with you.

@TopCoder2K
TopCoder2K force-pushed the fix_smtp_connection_retry_limit branch from 242c77b to 4e7efe9 Compare March 18, 2026 17:37
@TopCoder2K

Copy link
Copy Markdown
Contributor Author

Damn, Git hooks don't want to work in GitHub Codespaces, so I'm forced to run them locally on my ancient laptop :(
I'll try this week.

@dabla

dabla commented Mar 23, 2026

Copy link
Copy Markdown
Contributor

Damn, Git hooks don't want to work in GitHub Codespaces, so I'm forced to run them locally on my ancient laptop :( I'll try this week.

I think you just need to fix this and we're fine, no need for local run ;-)

https://github.com/apache/airflow/pull/62409/changes#diff-f84423e31e0641b5ad1e6757ebf08c5dd1188292e6dc80f3c63bc028539460b2R2287

@TopCoder2K

Copy link
Copy Markdown
Contributor Author

@dabla, oh, I didn't know there was highlighting of failed checks in the "Files changed" section, thank you! Let's try.

@TopCoder2K
TopCoder2K force-pushed the fix_smtp_connection_retry_limit branch from 4e7efe9 to 7f53fec Compare March 24, 2026 06:44
@TopCoder2K

Copy link
Copy Markdown
Contributor Author

@eladkal, could you please start the waiting workflows?

@TopCoder2K
TopCoder2K force-pushed the fix_smtp_connection_retry_limit branch 2 times, most recently from e426ce8 to 8b458b2 Compare March 28, 2026 07:00
@TopCoder2K

Copy link
Copy Markdown
Contributor Author

Ok, let's try to break the stalemate with this:

@TopCoder2K
TopCoder2K marked this pull request as ready for review March 28, 2026 07:02
@TopCoder2K

Copy link
Copy Markdown
Contributor Author

I took a look at airflow/airflow-e2e-tests/tests/airflow_e2e_tests/basic_tests/test_example_dags.py and didn't see any smtp or email usage. Logs for pnpm exec playwright test chromium also don't show any smtp or email usage, so I have no clue why

[chromium] › tests/e2e/specs/backfill.spec.ts:89:7 › Backfill › Backfill creation and validation › verify backfill with 'All Runs' behavior 
[chromium] › tests/e2e/specs/dag-audit-log.spec.ts:78:3 › DAG Audit Log › verify audit log entries display valid data

failed. I'll try to investigate this tomorrow or next week, but any help would be appreciated.

@potiuk potiuk removed the ready for maintainer review Set after triaging when all criteria pass. label May 18, 2026
@potiuk
potiuk marked this pull request as draft May 18, 2026 10:48
@potiuk

potiuk commented May 18, 2026

Copy link
Copy Markdown
Member

@TopCoder2K — Removing the ready for maintainer review label and converting back to draft. CI has failed since the label was added (4 failing checks).

The label's contract is that the PR is ready for maintainer review — a regression like this means the PR temporarily isn't. Check the failing checks, fix the regression, push, then mark "Ready for review" again to re-enter the queue.

See the Pull Request quality criteria.

No rush.


Note: This comment was drafted by an AI-assisted triage tool and may contain mistakes. Once you have addressed the points above, an Apache Airflow maintainer — a real person — will take the next look at your PR. We use this two-stage triage process so that our maintainers' limited time is spent where it matters most: the conversation with you.

@TopCoder2K

Copy link
Copy Markdown
Contributor Author

It seems that this is again due to the branch obsolescence. Let's try to rebase it.

@TopCoder2K
TopCoder2K force-pushed the fix_smtp_connection_retry_limit branch from 46b3e9b to 1be298e Compare May 18, 2026 12:05
@TopCoder2K
TopCoder2K marked this pull request as ready for review May 18, 2026 12:06
@potiuk potiuk added the ready for maintainer review Set after triaging when all criteria pass. label May 24, 2026
@potiuk potiuk removed the ready for maintainer review Set after triaging when all criteria pass. label Jun 9, 2026
@potiuk

potiuk commented Jun 9, 2026

Copy link
Copy Markdown
Member

@TopCoder2K — A reviewer (@guan404ming) has requested changes on this PR, so I've removed the ready for maintainer review label — the next step is on your side. Could you address the review comments (push a fix, or reply in-thread explaining why the feedback doesn't apply)? Once addressed, re-request review from @guan404ming or re-mark the PR ready and it returns to the maintainer queue. Thank you.

Note: This comment was drafted by an AI-assisted triage tool and may contain mistakes. Once you have addressed the points above, an Apache Airflow maintainer — a real person — will take the next look at your PR. We use this two-stage triage process so that our maintainers' limited time is spent where it matters most: the conversation with you.

@TopCoder2K

Copy link
Copy Markdown
Contributor Author

Oh no, why? @potiuk, the comment above is wrong. I'll be forced to re-enter the queue due to this mistake.(

By the way, Guan-Ming has been ignoring this PR for at least 1.5 months. Can anyone else review it? I've stuck in the rebase --> ready for review --> converted to draft due to obsolescence --> rebase --> ... loop.

@guan404ming
guan404ming force-pushed the fix_smtp_connection_retry_limit branch from 1be298e to d8a9003 Compare June 11, 2026 00:18

@guan404ming guan404ming left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for addressing the comments, and sorry for the late re-review. LGTM.

@TopCoder2K

Copy link
Copy Markdown
Contributor Author

Both failed tests seem to be unrelated to my changes. Should I wait until the problems are fixed and then rebase?

@ashb
ashb force-pushed the fix_smtp_connection_retry_limit branch from d8a9003 to 915977d Compare June 11, 2026 11:13
@TopCoder2K

Copy link
Copy Markdown
Contributor Author

Thank you for rebasing, Ash. Now I see two other errors, and they seem unrelated to my changes.

@TopCoder2K

Copy link
Copy Markdown
Contributor Author

Does anyone know if accessibility to https://registry-1.docker.io/v2/ has been restored? It seems that both tests failed due to connection problems. Can this be related to #66507?

@guan404ming
guan404ming force-pushed the fix_smtp_connection_retry_limit branch from 915977d to dfce44b Compare June 16, 2026 13:16
@TopCoder2K

TopCoder2K commented Jun 18, 2026

Copy link
Copy Markdown
Contributor Author

Hooray, all tests have been passed! 🥳
Let's merge?

@guan404ming
guan404ming merged commit 5027101 into apache:main Jun 18, 2026
258 of 261 checks passed
@boring-cyborg

boring-cyborg Bot commented Jun 18, 2026

Copy link
Copy Markdown

Awesome work, congrats on your first merged pull request! You are invited to check our Issue Tracker for additional contributions.

@guan404ming

Copy link
Copy Markdown
Member

Thanks @TopCoder2K!

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.

airflow.exceptions.AirflowException: The smtp connection should be loaded before!

8 participants