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

Prevent time pattern reschedule if cancelled during job execution #117879

Merged
merged 3 commits into from
May 22, 2024

Conversation

swoga
Copy link
Contributor

@swoga swoga commented May 21, 2024

Proposed change

An automation that is triggered via a time entity creates a scheduled job for triggering on the next due date.
However, after triggering, a new job is always scheduled for the next execution.
This becomes a problem if the value of the entity is changed during the execution of the automation.

  1. the automation is triggered, by the time set in input_datetime.test
    hass.async_run_hass_job(self.job, localized_now, background=True)
  2. automations calls service input_datetime.set_datetime for input_datetime.test, which leads to cancellation
    def async_cancel(self) -> None:
  3. automation ends and the time trigger is rescheduled with the old entity status, even though it was canceled
    self._cancel_callback = async_track_point_in_utc_time(

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

  • 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.

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:

@swoga swoga requested a review from a team as a code owner May 21, 2024 20:13
Copy link

@home-assistant home-assistant bot left a comment

Choose a reason for hiding this comment

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

Hi @swoga

It seems you haven't yet signed a CLA. Please do so here.

Once you do that we will be able to review and accept this pull request.

Thanks!

@home-assistant home-assistant bot added bugfix cla-needed core small-pr PRs with less than 30 lines. labels May 21, 2024
@home-assistant home-assistant bot marked this pull request as draft May 21, 2024 20:13
@home-assistant
Copy link

Please take a look at the requested changes, and use the Ready for review button when you are done, thanks 👍

Learn more about our pull request process.

@swoga swoga marked this pull request as ready for review May 21, 2024 20:15
@home-assistant home-assistant bot dismissed their stale review May 21, 2024 20:15

Stale

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.

Good find. 👍 Please add a test case for this change.

@home-assistant home-assistant bot marked this pull request as draft May 22, 2024 02:54
@bdraco
Copy link
Member

bdraco commented May 22, 2024

An alternate fix that wouldn't require a new flag is to move hass.async_run_hass_job(self.job, localized_now, background=True) to the end of the function so the timer is already scheduled by the time it runs which allows cancellation to work as expected from job.

That's probably more efficient as well for the common case as it's going to be very rare that the timer handler is cancelled from the job.

@bdraco
Copy link
Member

bdraco commented May 22, 2024

Thank you for detailing out the execution flow, it made reviewing this and understanding the problem nearly immediate.

@swoga
Copy link
Contributor Author

swoga commented May 22, 2024

An alternate fix that wouldn't require a new flag is to move hass.async_run_hass_job(self.job, localized_now, background=True) to the end of the function so the timer is already scheduled by the time it runs which allows cancellation to work as expected from job.

I've given it some thought, but I think it could cause problems.
If jobs have an execution time longer than one second, this would be a potential trouble spot.
e.g. if a time pattern is choosen which is executed every second, the next job is scheduled before the execution is completed

@swoga
Copy link
Contributor Author

swoga commented May 22, 2024

Does async_run_hass_job always wait for the end of a job, or is it allowed for callbacks to be async?
I don't know the internals well enough, but this could probably also be a problem area.

@swoga swoga marked this pull request as ready for review May 22, 2024 14:50
@home-assistant home-assistant bot requested a review from bdraco May 22, 2024 14:50
@bdraco
Copy link
Member

bdraco commented May 22, 2024

An alternate fix that wouldn't require a new flag is to move hass.async_run_hass_job(self.job, localized_now, background=True) to the end of the function so the timer is already scheduled by the time it runs which allows cancellation to work as expected from job.

I've given it some thought, but I think it could cause problems.

If jobs have an execution time longer than one second, this would be a potential trouble spot.

e.g. if a time pattern is choosen which is executed every second, the next job is scheduled before the execution is completed

I don't think it could be an issue because we already fetch utcnow to calculate the next time before the job is run. If this was a problem it would already be firing the next one right away.

The job can't block and will finish synchronously as otherwise it would block the event loop.

This is also how _TrackTimeInterval works (which doesn't have this problem)

hass.async_run_hass_job(self._run_job, now, background=True)

@bdraco bdraco added this to the 2024.5.5 milestone May 22, 2024
@bdraco
Copy link
Member

bdraco commented May 22, 2024

Since we are likely to ship 2024.5.5 soon, we can push this to 2024.6.x if its not ready by than. Its not a blocker as its been a problem for a while

@swoga
Copy link
Contributor Author

swoga commented May 22, 2024

I will remove the flag and move async_run_hass_job under the rescheduling.
Commit coming in a moment.

@swoga swoga force-pushed the fix-prevent-reschedule-after-cancel branch from 0291ed7 to 58bafa5 Compare May 22, 2024 19:59
@swoga
Copy link
Contributor Author

swoga commented May 22, 2024

I don't think it could be an issue because we already fetch utcnow to calculate the next time before the job is run. If this was a problem it would already be firing the next one right away.

The job can't block and will finish synchronously as otherwise it would block the event loop.

This is also how _TrackTimeInterval works (which doesn't have this problem)

Thanks for the insight and the quick review!

@bdraco
Copy link
Member

bdraco commented May 22, 2024

Thanks @swoga

@bdraco bdraco changed the title Prevent reschedule if cancelled during job execution Prevent time pattern reschedule if cancelled during job execution May 22, 2024
@bdraco bdraco merged commit eb76386 into home-assistant:dev May 22, 2024
38 checks passed
@swoga swoga deleted the fix-prevent-reschedule-after-cancel branch May 22, 2024 20:39
@github-actions github-actions bot locked and limited conversation to collaborators May 23, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
3 participants