fix(core): prevent listener crashes on late CDP transaction responses#242
Closed
Avejack wants to merge 1 commit intocdpdriver:mainfrom
Closed
fix(core): prevent listener crashes on late CDP transaction responses#242Avejack wants to merge 1 commit intocdpdriver:mainfrom
Avejack wants to merge 1 commit intocdpdriver:mainfrom
Conversation
Harden transaction completion and listener handling so stale/late CDP responses cannot crash the websocket listener task with InvalidStateError. - Guard Transaction.__call__ against already-completed futures. - Add safe completion helpers that swallow/log InvalidStateError: _set_result_safely and _set_exception_safely. - Treat malformed CDP responses (missing "result") as transaction errors. - Stop raising parse failures out of Transaction.__call__; set transaction exceptions instead. - Clean mapper entries when Connection.send() is cancelled. - Mirror cancellation/stale-mapper cleanup in Connection._send_oneshot(). - Add Listener._complete_transaction() to isolate transaction completion errors and keep listener_loop alive. - Add regression tests for late response after cancel, parse mismatch path, send-cancellation mapper cleanup, and listener completion safety. - Document fix in CHANGELOG under Unreleased. This addresses long-running instability where listener_loop could terminate after a late response arrived for a cancelled/completed transaction.
Member
|
Thanks for the contribution @Avejack, the changes make sense to me. Looks like the lint is failing, would you mind taking a look? I'd suggest using the included scripts & |
Codecov Report❌ Patch coverage is
📢 Thoughts on this report? Let us know! |
Contributor
|
This pull request has been marked stale because it has been open for 30 days with no activity. If there is no activity within 7 days, it will be automatically closed. |
Contributor
|
This pull request was automatically closed because it has been inactive for 7 days since being marked as stale. |
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.
Summary
This PR fixes a race condition where late CDP responses for cancelled/completed transactions could raise
InvalidStateErrorand terminateListener.listener_loop(), causing automation sessions to become unhealthy over long runtimes.Problem
In
Transaction.__call__, response completion usedset_result()/set_exception()directly without guarding future state.When a response arrived after cancellation/completion,
asyncio.InvalidStateErrorcould be raised.That exception propagated from
tx(**message)inlistener_loop, killing the listener task.Root Cause
Transaction.__call__had no done-state guard.InvalidStateErrorduring future completion.listener_loopinvoked transaction completion without isolation.Connection.send()and_send_oneshot()could leave stale mapper entries on cancellation.Changes
zendriver/core/connection.py.Transaction.__call__._set_result_safely_set_exception_safely"result"not present).Connection.send()mapper lifecycle.Connection._send_oneshot().Listener._complete_transaction()wrapper to prevent listener crash on completion errors._complete_transaction.CHANGELOG.mdunderUnreleased -> Fixed.tests/core/test_connection.py:test_transaction_late_response_after_cancel_does_not_raisetest_transaction_parse_mismatch_sets_exception_not_raisetest_send_cancellation_removes_tx_from_mappertest_listener_complete_transaction_handles_exceptionsBehavior Impact
Validation
Executed:
poetry run pytest -q tests/core/test_connection.pyResult:
4 passedNotes
A pre-existing warning remains unrelated to this fix:
zendriver/core/connection.pywarns aboutcontinuein afinallyblock.Risk Assessment
Low.
Changes are scoped to internal transaction/listener robustness and covered by new regression tests.
Pre-merge Checklist
./scripts/format.shand./scripts/lint.shscripts. My code is properly formatted and has no linting errors.uv run pytestand ensured all tests pass.[Unreleased]section.