Respect timeout when cancellation token is provided #2207
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.
Context
Our code runs the MQTT Client in a Background Service.
Problem
Providing any CancellationToken to any of those methods -
ConnectAsync,PingAsync,DisconnectAsync,SubscribeAsync, andUnsubscribeAsyncdisregards theTimeoutoption.In Background Services, we always provide the cancellation token, so if we are not aware of this behaviour, those methods will never timeout, disregarding the
WithTimeoutorTimeoutcompletely.I do acknowledge this is partially mentioned in the Sample for timeouts, though, and it is recommended to use a linked token source.
I found it confusing, so I'm suggesting a change.
Solution
Instead of disregarding the Timeout, those operations should always consider it and link the cancellation sources.
I'm also aware this is a breaking change, as it'd cause Timeouts for existing code that depends on overwriting this with a linked token with an expiry.
Thank you for considering this contribution. I appreciate any feedback or suggestions for change. Alternatively, we could consider improving the sample and/or documentation.