2.0.6 proposal: Fixed HA Deprecation, Error Handling, Resources Management #113
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Hi @mudape 👋
Added few fixes to ensure proper resource cleanup and prevent resource leaks, ensuring the stability and reliability of the code:
Replaced
SOURCE_TYPE_ROUTER
withSourceType.ROUTER
(There is a warning in HA 2024.1b #101).Wrapped the code that might raise exceptions in try-except blocks.
Use the
closing
context manager from thecontextlib
module to ensure that theIPRoute
instance is properly closed after use, even if an exception occurs within the block. Theclosing
context manager ensures that the__exit__
method of theIPRoute
instance is always called, allowing it to release any associated resources.The
IPRoute
class from thepyroute2
library interacts with system resources, and not closing instances of it could lead to resource leaks. Each instance ofIPRoute
may allocate file descriptors for communication with the kernel'snetlink
subsystem, and if these descriptors are not released, they can accumulate over time, leading to the observed behavior in 2.0.5: Too many open files #99 .If an exception occurs while querying the network state using
IPRoute
, it's caught and handled gracefully, and a default state value is used instead.