Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Keep task references while running #87970

Merged
merged 15 commits into from
Feb 14, 2023
Merged

Keep task references while running #87970

merged 15 commits into from
Feb 14, 2023

Conversation

balloob
Copy link
Member

@balloob balloob commented Feb 13, 2023

Breaking change

Proposed change

Attempt 2 to make sure we track all tasks created in HA. This time we only track it for tasks created via hass.async_create_task and not for all tasks created on our event loop (which was attempt 1 at #87959)

Will always track tasks but make sure tasks clean up themselves.

This feels a bit like heart surgery…

Also added a new hass.background_task.async_create_task for tasks you know are going to be in the background. They will never block async_block_till_done but still get canceled when HA shuts down.

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

  • This PR fixes or closes issue: fixes #
  • This PR is related to issue:
  • Link to documentation pull request:

Checklist

  • 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
  • The code has been formatted using Black (black --fast homeassistant tests)
  • Tests have been added to verify that the new code works.

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 link to the changelog, or at minimum a diff between library versions is added to the PR description.
  • Untested files have been added to .coveragerc.

To help with the load of incoming pull requests:

homeassistant/core.py Outdated Show resolved Hide resolved
Comment on lines -642 to -644
while self._pending_tasks:
pending = [task for task in self._pending_tasks if not task.done()]
self._pending_tasks.clear()
Copy link
Member Author

@balloob balloob Feb 13, 2023

Choose a reason for hiding this comment

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

The old approach would clear all the tasks so it would only track new tasks. The new approach is to just track all tasks. Old + new. It doesn't matter because if the old one blocks, it's also blocking. It was also necessary because each task will try to remove itself from this set once it's done.

@balloob
Copy link
Member Author

balloob commented Feb 13, 2023

Tried re-running the failed tests locally and they all pass 🙄

pytest tests/components/{pilight,history_stats,graphite} tests/helpers/test_event.py

@balloob balloob mentioned this pull request Feb 13, 2023
19 tasks
homeassistant/core.py Outdated Show resolved Hide resolved
homeassistant/core.py Outdated Show resolved Hide resolved
@balloob balloob force-pushed the keep-task-references branch from de389a4 to e642fb4 Compare February 13, 2023 13:54
@balloob balloob marked this pull request as ready for review February 13, 2023 18:47
@balloob balloob requested review from bdraco and a team as code owners February 13, 2023 18:47
homeassistant/core.py Outdated Show resolved Hide resolved
@@ -69,7 +69,7 @@ def setup(hass: HomeAssistant, config: ConfigType) -> bool:
else:
_LOGGER.debug("No connection check for UDP possible")

GraphiteFeeder(hass, host, port, protocol, prefix)
hass.data[DOMAIN] = GraphiteFeeder(hass, host, port, protocol, prefix)
Copy link
Contributor

Choose a reason for hiding this comment

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

Why is this changed?

Copy link
Member Author

Choose a reason for hiding this comment

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

Previously the test relied on race conditions about when the thread was done with work. Now the test just wait for the thread to be done.

homeassistant/core.py Outdated Show resolved Hide resolved
tests/helpers/test_script.py Outdated Show resolved Hide resolved
@balloob balloob force-pushed the keep-task-references branch from fb704b0 to 6fe9569 Compare February 14, 2023 02:01
@bdraco
Copy link
Member

bdraco commented Feb 14, 2023

Testing

  • dev
  • hou prod
  • jhm prod
  • performance - no measurable change

Copy link
Member

@bdraco bdraco left a comment

Choose a reason for hiding this comment

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

Testing looks good 👍

@balloob balloob merged commit d54f594 into dev Feb 14, 2023
@balloob balloob deleted the keep-task-references branch February 14, 2023 04:17
@github-actions github-actions bot locked and limited conversation to collaborators Feb 15, 2023
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