fix: Http2Adapter does not fail with StateError when connection is closed#2469
fix: Http2Adapter does not fail with StateError when connection is closed#2469vanelizarov wants to merge 3 commits intocfug:mainfrom
Conversation
There was a problem hiding this comment.
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 |
| } on StateError { | ||
| // Ignore StateError, which may occur if the stream is already closed. |
There was a problem hiding this comment.
I'm still not confident about shipping this implementation, given that we didn't find the root cause here. Could you share a reproducible case in production?
| } catch (_) { | ||
| rethrow; | ||
| } |
| # Local history | ||
| .history No newline at end of file |
There was a problem hiding this comment.
It would be better if you could submit a separate PR for this with more context.
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