Fix a race condition preventing embedded compiler to shutdown after a protocol error #2106
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.
There is an extreme rare race condition during compiler shutdown after a protocol error is send from host callable here:
dart-sass/lib/src/embedded/host_callable.dart
Line 50 in 16b8512
A reproduction looks like this:
The fundamental problem here is that
StreamSink#close()
is async operation which can take time if the system is slow. This gives us a chance to send a second message before compiler has been shutdown.The sequence of events looks like:
_requestError = true
in this case._requestError
is not set, the thrown ProtocolError bubbles up as a CompileFailure._checkedOut = false
on the isolate.Unhandled exception: Bad state: Shouldn't receive a message before being checked out.
The compiler would get stuck after the unhandled exception. The fix here is to make sure
_requestError = true
is set to that it does not send an extra CompilerFailure later which would reset isolate state at incorrect timing.Slow sink close can be emulated with the following patch to make it easy to reproduce: