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

Fix some handle leaks in rainforest_raven #113035

Merged
merged 1 commit into from Mar 12, 2024

Conversation

cottsay
Copy link
Contributor

@cottsay cottsay commented Mar 11, 2024

Proposed change

There were file handle leaks when:

  • The component was shutdown or reloaded
  • There was a timeout during the initial device opening

Additionally, the device was not closed/reopened when there was a timeout reading regular data.

A test was added to exercise the timeout code. Note that this test actually does wait for the timeout to trigger normally, and therefore takes 5 seconds to complete. If this is not acceptable, I can update the test to explicitly raise a TimeoutError immediately.

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:

There were leaks when
* The component was shutdown
* There was a timeout during the initial device opening

Additionally, the device was not closed/reopened when there was a
timeout reading regular data.
@cottsay
Copy link
Contributor Author

cottsay commented Mar 11, 2024

I'd like this bugfix to be considered for release as part of the next 2024.3 patch.

@frenck frenck added this to the 2024.3.1 milestone Mar 11, 2024
@balloob balloob merged commit eb81599 into home-assistant:dev Mar 12, 2024
21 checks passed
Copy link
Member

@MartinHjelmare MartinHjelmare left a comment

Choose a reason for hiding this comment

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

Please address the comments in a new PR. Thanks!

async def test_coordinator_device_timeout_update(hass: HomeAssistant, mock_device):
"""Test handling of a device timeout during an update."""
entry = create_mock_entry()
coordinator = RAVEnDataCoordinator(hass, entry)
Copy link
Member

Choose a reason for hiding this comment

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

Set up the integration instead. We shouldn't interact with the coordinator directly as that's an integration detail.

https://developers.home-assistant.io/docs/development_testing#writing-tests-for-integrations

await coordinator.async_config_entry_first_refresh()
assert coordinator.last_update_success is True

mock_device.get_network_info.side_effect = functools.partial(asyncio.sleep, 10)
Copy link
Member

Choose a reason for hiding this comment

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

Besides this, I'd make the timeout time a constant so we can easily patch it to 0. We don't want slow tests.

assert coordinator.last_update_success is True

mock_device.get_network_info.side_effect = functools.partial(asyncio.sleep, 10)
await coordinator.async_refresh()
Copy link
Member

Choose a reason for hiding this comment

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

Move time forward instead to trigger a refresh. See other integrations that use a coordinator with tests for examples.


mock_device.get_network_info.side_effect = functools.partial(asyncio.sleep, 10)
await coordinator.async_refresh()
assert coordinator.last_update_success is False
Copy link
Member

Choose a reason for hiding this comment

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

We should assert that the mock device is closed. We can also assert that a coordinator entity have its state set to unavailable.

frenck pushed a commit that referenced this pull request Mar 13, 2024
There were leaks when
* The component was shutdown
* There was a timeout during the initial device opening

Additionally, the device was not closed/reopened when there was a
timeout reading regular data.
@frenck frenck mentioned this pull request Mar 13, 2024
xeniter pushed a commit to xeniter/core that referenced this pull request Mar 13, 2024
There were leaks when
* The component was shutdown
* There was a timeout during the initial device opening

Additionally, the device was not closed/reopened when there was a
timeout reading regular data.
@cottsay cottsay deleted the raven-handle-leak branch March 14, 2024 00:17
@github-actions github-actions bot locked and limited conversation to collaborators Mar 15, 2024
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.

ValueError: filedescriptor out of range in select()
4 participants