Raise RuntimeError if watcher.cancel() is called after client.close() #17
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.
Currently client.watch() will return a watcher object that can be used separately from the client.
Current behavior when watcher.close() is called after client.close() -
AttributeError: 'NoneType' object has no attribute 'cancel'
- is confusing and unclear.I think two ways to handle this better are:
Make watcher.cancel() a no-op after client is closed.
This mimics how
async for kv in watcher:
iterator currently works.Have watcher.cancel() raise a clear "must not be called when client is already closed" exception.
This PR does the latter, assuming that it is a programming error to call watcher.cancel() for closed client.
In my use-case closing the client should cancel all watcher iterators already, so such call indeed indicates some code bug.
Picked RuntimeError because it's used for same types of bugs in asyncio itself and also in client.py already, and there should never be a need to handle it in any way (i.e. handling RuntimeError is almost certainly a bug in the code).
Alternative viewpoint on the API can be that
w = client.watch(); try: ... finally: w.cancel()
pattern should be supported, in which case .cancel() should be a no-op (to avoid raising exception in "finally").If this is the case, rtypes.Watch should also support aenter/aexit to work in a more conventional
with client.watch() as w: ...
pattern. Assuming that this is not implemented on purpose, RuntimeError variant above seem to be a more fitting option.