Skip to content

Cancel and await idle_start future if the task was canceled after an IMAP connection was lost#168662

Merged
jbouwh merged 7 commits into
devfrom
imap-cancel-idle-wait-future
Apr 22, 2026
Merged

Cancel and await idle_start future if the task was canceled after an IMAP connection was lost#168662
jbouwh merged 7 commits into
devfrom
imap-cancel-idle-wait-future

Conversation

@jbouwh
Copy link
Copy Markdown
Contributor

@jbouwh jbouwh commented Apr 20, 2026

Proposed change

If an IMAP server is awaited for push messages and the connection is lost, the IDLE command fails.
The future that was awaited still needs to be canceled.

This fix may solve issue #122809

Type of change

  • Dependency upgrade
  • Bugfix (non-breaking change which fixes an issue)
  • New integration (thank you!)
  • New feature (which adds functionality to an existing integration)
  • Deprecation (breaking change to happen in the future)
  • Breaking change (fix/feature causing existing functionality to break)
  • Code quality improvements to existing code or addition of tests

Additional information

Checklist

  • I understand the code I am submitting and can explain how it works.
  • The code change is tested and works locally.
  • Local tests pass. Your PR cannot be merged unless tests pass
  • There is no commented out code in this PR.
  • I have followed the development checklist
  • I have followed the perfect PR recommendations
  • The code has been formatted using Ruff (ruff format homeassistant tests)
  • Tests have been added to verify that the new code works.
  • Any generated code has been carefully reviewed for correctness and compliance with project standards.

If user exposed functionality or configuration variables are added/changed:

If the code communicates with devices, web services, or third-party tools:

  • The manifest file has all fields filled out correctly.
    Updated and included derived files by running: python3 -m script.hassfest.
  • New or updated dependencies have been added to requirements_all.txt.
    Updated by running python3 -m script.gen_requirements_all.
  • For the updated dependencies a diff between library versions and ideally a link to the changelog/release notes is added to the PR description.

To help with the load of incoming pull requests:

Copilot AI review requested due to automatic review settings April 20, 2026 22:01
@jbouwh jbouwh changed the title Cancel and await idle_start future if the task is canceled after an IMAP connection was lost Cancel and await idle_start future if the task was canceled after an IMAP connection was lost Apr 20, 2026
@jbouwh jbouwh added this to the 2026.4.4 milestone Apr 20, 2026
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR addresses a cleanup issue in the IMAP push (IDLE) loop where the idle_start() future could remain pending after a lost connection, leading to “Task was destroyed but it is pending!” warnings.

Changes:

  • Track the idle_start() future in the IMAP push loop and attempt to cancel/await it in a finally block to prevent leaked pending futures.
  • Add/adjust a regression test to assert the IDLE future is canceled after a simulated connection loss.

Reviewed changes

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

File Description
homeassistant/components/imap/coordinator.py Adds finally cleanup to cancel/await the pending IDLE future after errors.
tests/components/imap/test_init.py Extends the lost-connection push test to validate the IDLE cancellation path via logs.

Comment thread homeassistant/components/imap/coordinator.py Outdated
Copilot AI review requested due to automatic review settings April 20, 2026 22:11
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

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

Comment thread tests/components/imap/test_init.py Outdated
Comment thread homeassistant/components/imap/coordinator.py Outdated
Comment thread homeassistant/components/imap/coordinator.py Outdated
Comment thread homeassistant/components/imap/coordinator.py Outdated
Comment thread homeassistant/components/imap/coordinator.py Outdated
Co-authored-by: J. Nick Koston <nick@koston.org>
Copilot AI review requested due to automatic review settings April 21, 2026 08:15
Comment thread homeassistant/components/imap/coordinator.py Outdated
Co-authored-by: J. Nick Koston <nick@koston.org>
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated no new comments.

Comments suppressed due to low confidence (1)

homeassistant/components/imap/coordinator.py:7

  • Remove the unused suppress import (or use it) to avoid failing lint checks for an unused import.
from datetime import datetime, timedelta

@jbouwh
Copy link
Copy Markdown
Contributor Author

jbouwh commented Apr 22, 2026

I still need some help to fix the code coverage for the new code. Optionally this is fixed later to allow the patch to be released.

@jbouwh
Copy link
Copy Markdown
Contributor Author

jbouwh commented Apr 22, 2026

Discussed with @bdraco , desided to merge

@jbouwh jbouwh merged commit 018e8e0 into dev Apr 22, 2026
32 of 33 checks passed
@jbouwh jbouwh deleted the imap-cancel-idle-wait-future branch April 22, 2026 07:43
@github-actions github-actions Bot locked and limited conversation to collaborators Apr 23, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants