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

[smhi] Fix exception in aggregation function when daily forecast is empty #10851

Merged
merged 2 commits into from
Jun 15, 2021

Conversation

pacive
Copy link
Member

@pacive pacive commented Jun 11, 2021

Also catch any Exception to prevent thread from crashing

Fixes #10850

Signed-off-by: Anders Alfredsson [email protected]

@pacive pacive changed the title Fix exception in aggregation function when daily forecast is empty [smhi] Fix exception in aggregation function when daily forecast is empty Jun 11, 2021
@fwolter
Copy link
Member

fwolter commented Jun 12, 2021

Catching a division by zero exception, raised in your own code isn't a pretty good solution. Better check if the divisor is zero beforehand.

@pacive
Copy link
Member Author

pacive commented Jun 12, 2021

Better check if the divisor is zero beforehand.

That's what I did, the method returns Optional.empty() if the expression used as divisor returns zero.

The catch-all is just so the binding won't crash if there are other exceptions thrown that I might have missed.

@fwolter
Copy link
Member

fwolter commented Jun 12, 2021

Ok, I missed that. Catching unchecked exceptions in binding's scheduler threads is not necessary as the framework will catch and log them.

@pacive
Copy link
Member Author

pacive commented Jun 12, 2021

Yes, but if the exception is caught by the scheduler it stops scheduling new tasks, so the binding stops updating.

@pacive
Copy link
Member Author

pacive commented Jun 12, 2021

I know that it's probably bad practice to catch Exception, but is there a better way to prevent overlooked bugs from causing the binding to stop working?

@fwolter
Copy link
Member

fwolter commented Jun 12, 2021

Are you sure the periodic task is cancelled completely? Actually, we came to the conclusion in openhab/openhab-core#2392 that this is covered for bindings.

@pacive
Copy link
Member Author

pacive commented Jun 12, 2021

The user who reported the issue had the binding stop updating, and I had a different issue earlier (which is already patched) that also caused the same symptom. The discussion you linked only seems to suggest that the exceptions are logged, not that the execution of the scheduled tasks continue?

Should this be filed as an issue in oh core? I can see cases where you don't want the task to continue be scheduled, e.g. to avoid tasks that consistently throws exception to continue to run, possibly hogging resources (which I admit would also be the case if everyone catches Exception in all tasks)

Also catch any Exception to prevent thread from crashing

Signed-off-by: Anders Alfredsson <[email protected]>
@fwolter
Copy link
Member

fwolter commented Jun 13, 2021

You're right. The exception is logged and the periodic task is terminated. I think is is okay behavior. It's ok to let the binding crash (loudly) if it encounters a bug that needs to be fixed.

I'd remove the generic try-catch if it was my code as I think bugs should be fixed and not handled. I leave it up to you if you keep it or not. But if you keep it, you should make the log level error.

@pacive
Copy link
Member Author

pacive commented Jun 14, 2021

I'd remove the generic try-catch if it was my code as I think bugs should be fixed and not handled. I leave it up to you if you keep it or not. But if you keep it, you should make the log level error.

I totally agree, but I also want to spare users from problems caused by my mistakes. I narrowed down the try-catch to RuntimeException and changed the logging to warn, with a suggestion to report the issue so it can be fixed properly. While working on fixing this one better I also made some changes which I believe should make it a bit more robust.

Improve tests to cover different forecast scenarios

Signed-off-by: Anders Alfredsson <[email protected]>
@pacive pacive added the bug An unexpected problem or unintended behavior of an add-on label Jun 15, 2021
Copy link
Member

@fwolter fwolter left a comment

Choose a reason for hiding this comment

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

LGTM

@fwolter fwolter merged commit c993345 into openhab:main Jun 15, 2021
@fwolter fwolter added this to the 3.1 milestone Jun 15, 2021
computergeek1507 pushed a commit to computergeek1507/openhab-addons that referenced this pull request Jul 13, 2021
…mpty (openhab#10851)

* Fix exception in aggregation function when daily forecast is empty

Also catch any Exception to prevent thread from crashing

Signed-off-by: Anders Alfredsson <[email protected]>

* Refactor to improve robustness of calculations

Improve tests to cover different forecast scenarios

Signed-off-by: Anders Alfredsson <[email protected]>
lucacalcaterra pushed a commit to lucacalcaterra/openhab-addons that referenced this pull request Jul 26, 2021
…mpty (openhab#10851)

* Fix exception in aggregation function when daily forecast is empty

Also catch any Exception to prevent thread from crashing

Signed-off-by: Anders Alfredsson <[email protected]>

* Refactor to improve robustness of calculations

Improve tests to cover different forecast scenarios

Signed-off-by: Anders Alfredsson <[email protected]>
Signed-off-by: Luca Calcaterra <[email protected]>
lucacalcaterra pushed a commit to lucacalcaterra/openhab-addons that referenced this pull request Jul 26, 2021
…mpty (openhab#10851)

* Fix exception in aggregation function when daily forecast is empty

Also catch any Exception to prevent thread from crashing

Signed-off-by: Anders Alfredsson <[email protected]>

* Refactor to improve robustness of calculations

Improve tests to cover different forecast scenarios

Signed-off-by: Anders Alfredsson <[email protected]>
Signed-off-by: Luca Calcaterra <[email protected]>
lucacalcaterra pushed a commit to lucacalcaterra/openhab-addons that referenced this pull request Aug 3, 2021
…mpty (openhab#10851)

* Fix exception in aggregation function when daily forecast is empty

Also catch any Exception to prevent thread from crashing

Signed-off-by: Anders Alfredsson <[email protected]>

* Refactor to improve robustness of calculations

Improve tests to cover different forecast scenarios

Signed-off-by: Anders Alfredsson <[email protected]>
Signed-off-by: Luca Calcaterra <[email protected]>
@pacive pacive deleted the smhi-fix branch August 10, 2021 07:10
frederictobiasc pushed a commit to frederictobiasc/openhab-addons that referenced this pull request Oct 26, 2021
…mpty (openhab#10851)

* Fix exception in aggregation function when daily forecast is empty

Also catch any Exception to prevent thread from crashing

Signed-off-by: Anders Alfredsson <[email protected]>

* Refactor to improve robustness of calculations

Improve tests to cover different forecast scenarios

Signed-off-by: Anders Alfredsson <[email protected]>
thinkingstone pushed a commit to thinkingstone/openhab-addons that referenced this pull request Nov 7, 2021
…mpty (openhab#10851)

* Fix exception in aggregation function when daily forecast is empty

Also catch any Exception to prevent thread from crashing

Signed-off-by: Anders Alfredsson <[email protected]>

* Refactor to improve robustness of calculations

Improve tests to cover different forecast scenarios

Signed-off-by: Anders Alfredsson <[email protected]>
marcfischerboschio pushed a commit to bosch-io/openhab-addons that referenced this pull request May 5, 2022
…mpty (openhab#10851)

* Fix exception in aggregation function when daily forecast is empty

Also catch any Exception to prevent thread from crashing

Signed-off-by: Anders Alfredsson <[email protected]>

* Refactor to improve robustness of calculations

Improve tests to cover different forecast scenarios

Signed-off-by: Anders Alfredsson <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug An unexpected problem or unintended behavior of an add-on
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[smhi] Division by zero error in ForecastAggregator
2 participants