Skip to content

Conversation

@invokermain
Copy link
Contributor

Issue link

This Pull Request is linked to issue (URL): #4653

Description

The Python async_client does not handle cancellation correctly. This issue would affect both async backends (asyncio/trio) in different ways.

When a request is cancelled by the client, the server will still send a response which will trigger the _process_response logic. However, when using asyncio, the future will be marked as done due to the cancellation. Note, trio behaves differently here due to _CompatFuture, which will not be in the done state when cancelled.

The current code would not check whether the future is done, and will try to call set_result on the future. In asyncio this raises an InvalidStateError which would then cause the client to be closed.

Fix

The fix is relatively simple, we explicitly check whether the future is done, and if so ignore it (as we know no one is going to read the response anyway).

The current code would also close the client if an orphaned/unknown response was received, this seems needlessly defensive, so this PR changes that behaviour to just log and ignore it.

@invokermain invokermain requested a review from a team as a code owner December 18, 2025 11:34
@xShinnRyuu xShinnRyuu linked an issue Dec 19, 2025 that may be closed by this pull request
@xShinnRyuu
Copy link
Collaborator

Hi @invokermain! Thanks for contributing.

We will try and get to it asap.

As a side note though, It looks like the DCO for our CICD has failed. That is because we require all commits to have both DCO sign-off, as well as signing.

@invokermain invokermain force-pushed the python-async-invalid-state branch from 6824aca to 26239f4 Compare December 19, 2025 16:30
@invokermain
Copy link
Contributor Author

no problem I have amended & signed the commit

@xShinnRyuu
Copy link
Collaborator

xShinnRyuu commented Dec 19, 2025

no problem I have amended & signed the commit

@invokermain It looks like you have DCO approval, but your previous commit is not signed/verified with a signature
image

@xShinnRyuu
Copy link
Collaborator

xShinnRyuu commented Dec 19, 2025

@invokermain Thanks for the fix, it generally looks good! However, please resolve the Python CICD issues

@invokermain invokermain force-pushed the python-async-invalid-state branch from d2b69c0 to ae05f1b Compare December 20, 2025 12:37
@invokermain
Copy link
Contributor Author

I think my local keys are incorrect (wrong email), I will take a look again later today

@invokermain invokermain force-pushed the python-async-invalid-state branch from ae05f1b to 1a2b9da Compare December 21, 2025 11:09
Signed-off-by: invokermain <timothyj725@googlemail.com>
@invokermain invokermain force-pushed the python-async-invalid-state branch from 1a2b9da to dda9d46 Compare December 21, 2025 11:10
@invokermain
Copy link
Contributor Author

cool, I just need (re) approval on the workflow so I can see what the issues are. Then I can fix them.

Signed-off-by: invokermain <timothyj725@googlemail.com>
@invokermain invokermain force-pushed the python-async-invalid-state branch from 9435a52 to cbdc448 Compare December 22, 2025 11:20
Signed-off-by: invokermain <timothyj725@googlemail.com>
@invokermain
Copy link
Contributor Author

I've simplified the fix to be a more minimal change, that fixes the failing test cases locally.

@yipin-chen yipin-chen merged commit a4957c9 into valkey-io:main Jan 5, 2026
26 checks passed
tdschwarz pushed a commit to tdschwarz/valkey-glide that referenced this pull request Jan 9, 2026
* fix (python): handle cancelled requests gracefully

Signed-off-by: invokermain <timothyj725@googlemail.com>
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.

Invalid State error + Closed Client

4 participants