Skip to content

Commit

Permalink
[io/socket/win] Ensure udp sockets survive sending data to unreachabl…
Browse files Browse the repository at this point in the history
…e destination.

TEST=tests\standalone\io\socket_udp_readwrite_test.dart
BUG=flutter/flutter#155823

Change-Id: Ic38bc9c022fe3964bcca003bf498c4a5435d5104
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/390140
Commit-Queue: Alexander Aprelev <aam@google.com>
Reviewed-by: Siva Annamalai <asiva@google.com>
  • Loading branch information
aam authored and Commit Queue committed Oct 23, 2024
1 parent b0e2396 commit ea5fdaa
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
6 changes: 6 additions & 0 deletions runtime/bin/socket_win.cc
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,12 @@ intptr_t Socket::CreateBindDatagram(const RawAddr& addr,
return -1;
}

// Ensure the socket doesn't get closed if used to send data to unreachable.
BOOL value = FALSE;
DWORD bytes;
WSAIoctl(s, SIO_UDP_CONNRESET, &value, sizeof value, NULL, 0, &bytes, nullptr,
nullptr);

int status;
if (reuseAddress) {
BOOL optval = true;
Expand Down
36 changes: 36 additions & 0 deletions tests/standalone/io/socket_udp_readwrite_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// Copyright (c) 2024, 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.

// Test writing to unreachable udp socket doesn't cause problems.

import 'dart:typed_data';
import 'dart:io';

import "package:expect/expect.dart";

main() async {
var _socket = await RawDatagramSocket.bind(InternetAddress.anyIPv4, 9099);
_socket.listen((RawSocketEvent event) {
print("event: $event");
switch (event) {
case RawSocketEvent.read:
Datagram? d = _socket.receive();
if (d != null) {
print("recv: $d, all done");
_socket.close();
}
break;
case RawSocketEvent.write:
print('received write event $event');
break;
}
}, onError: (e) {
Expect.fail('Should be no exceptions, but got $e');
});

for (int i = 0; i < 100; i++) {
_socket.send(Uint8List(10), InternetAddress("127.0.0.1"), 9100);
}
_socket.send(Uint8List(10), InternetAddress("127.0.0.1"), 9099);
}

0 comments on commit ea5fdaa

Please sign in to comment.