Skip to content
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

Bad state: Future already completed #45047

Closed
bsutton opened this issue Feb 18, 2021 · 11 comments
Closed

Bad state: Future already completed #45047

bsutton opened this issue Feb 18, 2021 · 11 comments
Labels
area-vm Use area-vm for VM related issues, including code coverage, and the AOT and JIT backends. library-io type-bug Incorrect behavior (everything from a crash to more subtle misbehavior)

Comments

@bsutton
Copy link

bsutton commented Feb 18, 2021

dart --version
Dart SDK version: 2.12.0-259.12.beta (beta) (Fri Feb 12 11:00:34 2021 +0100) on "linux_x64"

I should note that I'm using the project https://pub.dev/packages/dswitch to switch between beta/stable version of dart.

I've seen the below error a couple of times now but haven't been paying much attention ( think it may have happened during unit testing?)

dart pub upgrade --null-safety
Because every version of node_mgmt_lib from path depends on path 1.7.0 and nj_build_all depends on path >=1.8.0-nullsafety.3, node_mgmt_lib from path is forbidden.
So, because nj_build_all depends on node_mgmt_lib from path, version solving failed.
Unhandled exception:
Bad state: Future already completed
#0      _AsyncCompleter.complete (dart:async/future_impl.dart:45:31)
#1      _NativeSocket.tryConnectToResolvedAddresses.connectNext.<anonymous closure> (dart:io-patch/socket_patch.dart:847:16)
#2      _NativeSocket.issueWriteEvent.issue (dart:io-patch/socket_patch.dart:1214:14)
#3      _NativeSocket.issueWriteEvent (dart:io-patch/socket_patch.dart:1221:12)
#4      _NativeSocket.multiplex (dart:io-patch/socket_patch.dart:1242:11)
#5      _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:184:12).
Attempt:11 waiting for isolate vm-isolate to check in
Attempt:11 waiting for isolate dartdev to check in
Attempt:12 waiting for isolate vm-isolate to check in
Attempt:12 waiting for isolate dartdev to check in
Attempt:13 waiting for isolate vm-isolate to check in
Attempt:13 waiting for isolate dartdev to check in
Attempt:14 waiting for isolate vm-isolate to check in
Attempt:14 waiting for isolate dartdev to check in
Attempt:15 waiting for isolate vm-isolate to check in
Attempt:15 waiting for isolate dartdev to check in
Attempt:16 waiting for isolate vm-isolate to check in
Attempt:16 waiting for isolate dartdev to check in
Attempt:17 waiting for isolate vm-isolate to check in
Attempt:17 waiting for isolate dartdev to check in
Attempt:18 waiting for isolate vm-isolate to check in
Attempt:18 waiting for isolate dartdev to check in

The problem doesn't occur if I just run dart pub upgrade

@mkustermann mkustermann added area-vm Use area-vm for VM related issues, including code coverage, and the AOT and JIT backends. library-io type-bug Incorrect behavior (everything from a crash to more subtle misbehavior) labels Feb 19, 2021
@mkustermann
Copy link
Member

/cc @mraleph @a-siva Do we have someone to look at dart:io bugs?

@mraleph
Copy link
Member

mraleph commented Feb 19, 2021

This might be related to DNS resolution changes

/cc @aam

@aam
Copy link
Contributor

aam commented Feb 19, 2021

DNS resolution change(looking up ipv4 and ipv6 in parallel) targeted osx, should not have affected other os.

@bsutton how easy is to reproduce the failure? Are you issuing dart pub upgrade --null-safety from the shell or it is executed by dswitch? Has it started from a particular revision (of dart or flutter)?

@bsutton
Copy link
Author

bsutton commented Feb 19, 2021 via email

@bsutton
Copy link
Author

bsutton commented Feb 19, 2021 via email

@mraleph
Copy link
Member

mraleph commented Feb 22, 2021

DNS resolution change(looking up ipv4 and ipv6 in parallel) targeted osx, should not have affected other os.

We did go from Future<List<int>> to Stream<List<int>> on other OSes which might have changed something in ordering of async events in the event queue. I would not be surprised if the bug was already present before we touched the code (the whole function was always painful to look at - so convoluted it was).

I'd suspect cancellation as the source of this issue due to suspicious non-symmetry in the code: notice how many places where result.complete is called we check if result.isCompleted first, one of them has the following comment // Might be already completed via onCancel.

Indeed the following test case seems to reproduce the problem, or at least something that looks like the same exception:

$ cat /tmp/repro.dart 
import 'dart:io';

void main() async {
  try {
    final task = await Socket.startConnect('google.com', 80);
    task.cancel();
    await task.socket;
  } catch (e) {}
}
$ dart /tmp/repro.dart
Unhandled exception:
Bad state: Future already completed
#0      _AsyncCompleter.complete (dart:async/future_impl.dart:45:31)
#1      _NativeSocket.tryConnectToResolvedAddresses.connectNext.<anonymous closure> (dart:io-patch/socket_patch.dart:847:16)
#2      _NativeSocket.issueWriteEvent.issue (dart:io-patch/socket_patch.dart:1214:14)
#3      _NativeSocket.issueWriteEvent (dart:io-patch/socket_patch.dart:1221:12)
#4      _NativeSocket.multiplex (dart:io-patch/socket_patch.dart:1242:11)
#5      _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:184:12)

@aam
Copy link
Contributor

aam commented Feb 23, 2021

Thanks @mraleph . That was a good idea for a test. Have https://dart-review.googlesource.com/c/sdk/+/186641 for that.
Hopefully this fixes what @bsutton hit.

@eernstg
Copy link
Member

eernstg commented Feb 24, 2021

Warning, completely off topic Am I the only one who thinks the title 'Bad state: Future already completed' is funny? 😄

@aam
Copy link
Contributor

aam commented Feb 24, 2021

@bsutton if you have a chance could you please give a try to dart sdk with the fix from the above to see if it improved things for you? 2.13.0-71.0.dev is the first dev build that has the fix in it. That dev version has not been made available on https://dart.dev/tools/sdk/archive yet, so until it is published, you can try main channel (https://dart.dev/tools/sdk/archive#main-channel-url-scheme)

@aam
Copy link
Contributor

aam commented Mar 2, 2021

https://dart.dev/tools/sdk/archive has 2.13.0-93.0.dev available that should have anticipated fix.

@aam aam closed this as completed Mar 2, 2021
@mraleph
Copy link
Member

mraleph commented Mar 5, 2021

@aam @a-siva I think we should file CP for stable for this issue if we have not already

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-vm Use area-vm for VM related issues, including code coverage, and the AOT and JIT backends. library-io type-bug Incorrect behavior (everything from a crash to more subtle misbehavior)
Projects
None yet
Development

No branches or pull requests

5 participants