Skip to content

Commit

Permalink
[vm/io] Fix an exception when attempting to immediately cancel socket…
Browse files Browse the repository at this point in the history
… connection.

Fixes dartbug.com/45047

Change-Id: Ie4630684722e8c17cbc3766f91111b2a76bce7c1
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/186641
Commit-Queue: Alexander Aprelev <[email protected]>
Reviewed-by: Siva Annamalai <[email protected]>
  • Loading branch information
aam authored and athomas committed Mar 10, 2021
1 parent 72c1995 commit 19a5733
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 1 deletion.
23 changes: 23 additions & 0 deletions runtime/tests/vm/dart/regress45047_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Copyright (c) 2021, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

// Verify that socket connection gracefully closes if cancelled.

import 'dart:async';
import 'dart:io';

import 'package:expect/expect.dart';

void main() async {
final task = await Socket.startConnect('google.com', 80);
task.cancel();
try {
await task.socket;
} catch (e) {
Expect.isTrue(e is SocketException);
final socketException = e as SocketException;
Expect.isTrue(
socketException.message.startsWith('Connection attempt cancelled'));
}
}
23 changes: 23 additions & 0 deletions runtime/tests/vm/dart_2/regress45047_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Copyright (c) 2021, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

// Verify that socket connection gracefully closes if cancelled.

import 'dart:async';
import 'dart:io';

import 'package:expect/expect.dart';

void main() async {
final task = await Socket.startConnect('google.com', 80);
task.cancel();
try {
await task.socket;
} catch (e) {
Expect.isTrue(e is SocketException);
final socketException = e as SocketException;
Expect.isTrue(
socketException.message.startsWith('Connection attempt cancelled'));
}
}
6 changes: 5 additions & 1 deletion sdk/lib/_internal/vm/bin/socket_patch.dart
Original file line number Diff line number Diff line change
Expand Up @@ -844,7 +844,10 @@ class _NativeSocket extends _NativeSocketNativeWrapper with _ServiceObject {
}
connecting.clear();
addressesSubscription.cancel();
result.complete(socket);
if (!result.isCompleted) {
// Might be already completed via onCancel
result.complete(socket);
}
}, error: (e, st) {
connecting.remove(socket);
socket.close();
Expand All @@ -864,6 +867,7 @@ class _NativeSocket extends _NativeSocketNativeWrapper with _ServiceObject {
s.setHandlers();
s.setListening(read: false, write: false);
}
addressesSubscription.cancel();
connecting.clear();
if (!result.isCompleted) {
error ??= createError(
Expand Down

0 comments on commit 19a5733

Please sign in to comment.