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

Refactor timeout handling #523

Merged
merged 6 commits into from Dec 9, 2020
Merged

Refactor timeout handling #523

merged 6 commits into from Dec 9, 2020

Conversation

farmio
Copy link
Member

@farmio farmio commented Dec 7, 2020

Description

  • Refactor timeout handling of GatewayScanner, RequestResponse and ValueReader to
    • handle Task cancellation without leaving ghost callbacks
    • be shorter and better readable with fewer methods
  • add type-checking for GatewayScanner and ValueReader
    • (RequestResponse seems to be a bigger task - thats for another weekend)

Fixes # (issue)

Type of change

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • This change requires a documentation update

Checklist:

  • I have performed a self-review of my own code
  • The documentation has been adjusted accordingly
  • The changes generate no new warnings
  • Tests have been added that prove the fix is effective or that the feature works
  • The changes are documented in the changelog
  • The Homeassistant plugin has been adjusted in case of new config options

@farmio farmio requested a review from marvin-w December 7, 2020 22:46
Comment on lines 32 to 60
__all__ = [
"KNXIPBody",
"CEMIFrame",
"ConnectRequest",
"ConnectResponse",
"ConnectionStateRequest",
"ConnectionStateResponse",
"DIB",
"DIBDeviceInformation",
"DIBGeneric",
"DIBSuppSVCFamilies",
"DisconnectRequest",
"DisconnectResponse",
"ErrorCode",
"KNXIPHeader",
"HPAI",
"KNXIPFrame",
"APCICommand",
"CEMIFlags",
"CEMIMessageCode",
"ConnectRequestType",
"DIBServiceFamily",
"DIBTypeCode",
"KNXIPServiceType",
"KNXMedium",
"RoutingIndication",
"SearchRequest",
"SearchResponse",
"TunnellingAck",
"TunnellingRequest",
]
Copy link
Member Author

Choose a reason for hiding this comment

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

I don't like this, but it seems

xknx/setup.cfg

Line 51 in 0defb26

implicit_reexport = true

isn't doing what I think it should do.

Comment on lines +43 to +57
try:
await asyncio.wait_for(
self.response_received_or_timeout.wait(),
timeout=self.timeout_in_seconds,
)
except asyncio.TimeoutError:
logger.warning(
"Error: KNX bus did not respond in time (%s secs) to GroupValueRead request for: %s",
self.timeout_in_seconds,
self.group_address,
)
finally:
# cleanup to not leave callbacks (for asyncio.CancelledError)
self.xknx.telegram_queue.unregister_telegram_received_cb(cb_obj)
Copy link
Member Author

Choose a reason for hiding this comment

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

The core of this PR is always this pattern.
Instead of using loop.call_later(self.timeout_in_seconds, self.timeout) in self.start_timeout() with self.stop_timeout() and self.timeout()
we use asyncio.wait_for() with a timeout argument and move the contents of self.timeout() into the exception clause so asyncio takes care of handling the timeout for us. This way we can use finally to always remove our callbacks.

@coveralls
Copy link

coveralls commented Dec 8, 2020

Coverage Status

Coverage decreased (-0.003%) to 94.665% when pulling a978650 on farmio:timeout-refactor into 8990e86 on XKNX:master.

@farmio farmio merged commit 5957eb0 into XKNX:master Dec 9, 2020
@farmio farmio deleted the timeout-refactor branch December 9, 2020 20:36
@farmio farmio mentioned this pull request Dec 16, 2020
10 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants