Repository navigation
Handle replies and disconnects for cancelled commands - #67
Merged
Merged
Conversation
Cancelling a coroutine awaiting a command reply (for example asyncio.wait_for(client.rpc(...), timeout)) cancels the future registered for that command. _future_success and _future_error resolved that future unconditionally, which raised InvalidStateError: - a late reply killed the task processing incoming messages, so no further replies, pushes or pings were handled until the no-ping timer forced a reconnect; - a disconnect with such a command still in flight raised from _clear_outgoing_futures, aborting disconnect half-way. Skip resolving futures that are already done, still dropping the in-flight entry and cancelling its timeout timer.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
What
Client._future_successandClient._future_errornow skip resolving a command future that is already done. They still drop the in-flight entry and cancel its timeout timer.Why
Cancelling a coroutine that is awaiting a command reply also cancels the future registered for that command. A common way to do that is to wrap an SDK call in
asyncio.wait_for(client.rpc(...), timeout)orasyncio.timeout(). Both helpers then set the result on that cancelled future anyway, which raisesInvalidStateError:_process_replyand kills the task that processes incoming messages. The client still reportsCONNECTED, but it stops handling replies, pushes and pings. Every later call times out, until the no-ping timer finally forces a reconnect._clear_outgoing_futuresraises out of_do_disconnect.disconnect()fails half-way: the state is never set toDISCONNECTEDandon_disconnectedis never called.The same applies to connect and subscribe commands (
_register_future_with_done), for example whenclient.connect()itself is cancelled.Verification
Added
tests/test_cancelled_command.py, which uses the in-process fake server:test_late_reply_to_cancelled_command: the fake server holds the reply to an RPC. The test cancels the call withasyncio.wait_for, has the server send the held reply, then asserts that a second RPC still gets its reply and the client stays connected. Before the fix the second RPC failed withOperationTimeoutError, because the message processing task had died withInvalidStateError("unhandled exception in background task" in the log).test_disconnect_with_cancelled_command_in_flight: cancels an RPC that never gets a reply, then asserts thatdisconnect()completes, the state isDISCONNECTEDand no in-flight commands are left. Before the fixdisconnect()raisedInvalidStateErrorfrom_future_error.I ran both tests against the unfixed
client.pyand confirmed they fail as described, then confirmed they pass with the fix. I kept the tests because cancelling SDK calls with a timeout is a normal usage pattern, and nothing else in the suite covers it.I also ran the full suite (
python -m unittest discover -s tests, 125 tests, with the docker-compose Centrifugo running) andruff check .. Both pass.