-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
fix: Http2Adapter does not fail with StateError when connection is closed #2469
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull request overview
This PR fixes a race condition in the Http2Adapter that caused unhandled StateError: Bad state: Cannot add event after closing exceptions when the server closes the connection before the client finishes sending the request body.
Key Changes:
- Replaced the simple stream listener with a more robust approach using
StreamSubscription,Completer, and explicit error handling to gracefully handle StateError - Added try-catch block around the final
stream.outgoingMessages.close()call to prevent StateError propagation - Improved cancellation handling by properly cancelling subscriptions before closing streams
Reviewed changes
Copilot reviewed 3 out of 4 changed files in this pull request and generated 4 comments.
| File | Description |
|---|---|
| plugins/http2_adapter/lib/src/http2_adapter.dart | Refactored request stream handling to catch StateError during data transmission and gracefully complete the request without propagating the error |
| plugins/http2_adapter/test/http2_test.dart | Added test case that simulates server closing connection early to verify the StateError fix |
| plugins/http2_adapter/CHANGELOG.md | Documented the bug fix in the unreleased section |
| .gitignore | Added .history directory to ignore list |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
|
@AlexV525 should I take into account comments from AI or will you do the review manually? |
@vanelizarov AI might give helpful suggestions. If you have opposite opinions, you can leave comments saying that you disagree with the suggestion. A manual review will be sent eventually, before the PR is merged. Additionally, the suggestions in the above review seem reasonable. |
Code Coverage Report: Only Changed Files listed
Minimum allowed coverage is |
Periodically I receive unhandled
StateError: Bad state: Cannot add event after closingin Crashlytics:Stack Trace
This problem was mentioned earlier by me in this PR.
After investigation, it appears that the error was caused by a race condition, for example, when the server closed the connection before receiving the request body.
This PR introduces a more reliable way to handle such situations. It also includes a test that can be run with the old implementation to reproduce the described issue.
New Pull Request Checklist
mainbranch to avoid conflicts (via merge from master or rebase)CHANGELOG.mdin the corresponding package