Skip to content

Conversation

@vanelizarov
Copy link
Contributor

@vanelizarov vanelizarov commented Dec 12, 2025

Periodically I receive unhandled StateError: Bad state: Cannot add event after closing in Crashlytics:

Stack Trace
StateError: Bad state: Cannot add event after closing
  *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** (***)
  pid: 19891, tid: 483148275864, name 1.ui (unparsed)
  android arch: arm64 comp: yes sim: no (os:)
  build_id: '4f1bdaeddbb49260128a5ff3c1e2e0c4' (unparsed)
  isolate_dso_base: 6ca549c000, vm_dso_base: 6ca549c000 (unparsed)
  isolate_instructions: 6ca5622940, vm_instructions: 6ca560c000 (unparsed)
#0      _StreamController.add (third_party/dart/sdk/lib/async/stream_controller.dart:617:24)
#1      _StreamSinkWrapper.add (third_party/dart/sdk/lib/async/stream_controller.dart:898:13)
#2      Http2Adapter._fetch.<anonymous closure> (/Users/vanelizarov/.pub-cache/git/dio-5259d4ae4fb665b8212c343c015e53fcbfe7d0b7/plugins/http2_adapter/lib/src/http2_adapter.dart:144:33)
#3      _RootZone.runUnaryGuarded (third_party/dart/sdk/lib/async/zone.dart:1778:10)
#4      _BufferingStreamSubscription._sendData (third_party/dart/sdk/lib/async/stream_impl.dart:381:11)
#5      _BufferingStreamSubscription._add (third_party/dart/sdk/lib/async/stream_impl.dart:312:7)
#6      _SinkTransformerStreamSubscription._add (third_party/dart/sdk/lib/async/stream_transformers.dart:67:11)
#7      _EventSinkWrapper.add (third_party/dart/sdk/lib/async/stream_transformers.dart:13:11)
#8      _transform.<anonymous closure> (/Users/vanelizarov/.pub-cache/hosted/pub.dev/dio-5.9.0/lib/src/progress_stream/io_progress_stream.dart:33:16)
#9      _HandlerEventSink.add (third_party/dart/sdk/lib/async/stream_transformers.dart:230:17)
#10     _SinkTransformerStreamSubscription._handleData (third_party/dart/sdk/lib/async/stream_transformers.dart:115:24)
#11     _SinkTransformerStreamSubscription._handleData (third_party/dart/sdk/lib/async/stream_transformers.dart:113:3)
#12     _RootZone.runUnaryGuarded (third_party/dart/sdk/lib/async/zone.dart:1778:10)
#13     _BufferingStreamSubscription._sendData (third_party/dart/sdk/lib/async/stream_impl.dart:381:11)
#14     _BufferingStreamSubscription._add (third_party/dart/sdk/lib/async/stream_impl.dart:312:7)
#15     _MultiStreamController.addSync (third_party/dart/sdk/lib/async/stream_impl.dart:1195:36)
#16     new Stream.fromIterable.<anonymous closure>.next (third_party/dart/sdk/lib/async/stream.dart:385:26)
#17     _microtaskLoop (third_party/dart/sdk/lib/async/schedule_microtask.dart:40:35)
#18     _startMicrotaskLoop (third_party/dart/sdk/lib/async/schedule_microtask.dart:49:5)
#19     _startMicrotaskLoop (third_party/dart/sdk/lib/async/schedule_microtask.dart:44:1)

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

  • I have read the Documentation
  • I have searched for a similar pull request in the project and found none
  • I have updated this branch with the latest main branch to avoid conflicts (via merge from master or rebase)
  • I have added the required tests to prove the fix/feature I'm adding
  • I have updated the documentation (if necessary)
  • I have run the tests without failures
  • I have updated the CHANGELOG.md in the corresponding package

@vanelizarov vanelizarov requested a review from a team as a code owner December 12, 2025 14:03
Copy link
Contributor

Copilot AI left a 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.

@vanelizarov
Copy link
Contributor Author

vanelizarov commented Dec 14, 2025

@AlexV525 should I take into account comments from AI or will you do the review manually?

@AlexV525
Copy link
Member

@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.

@github-actions
Copy link
Contributor

Code Coverage Report: Only Changed Files listed

Package Base Coverage New Coverage Difference
plugins/http2_adapter/lib/src/connection_manager_imp.dart 🟢 78.83% 🟢 83.94% 🟢 5.11%
plugins/http2_adapter/lib/src/http2_adapter.dart 🟠 74.13% 🟢 78.48% 🟢 4.35%
Overall Coverage 🟢 87.95% 🟢 88.58% 🟢 0.63%

Minimum allowed coverage is 0%, this run produced 88.58%

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.

2 participants