Skip to content

Commit ea5fdaa

Browse files
aamCommit Queue
authored andcommitted
[io/socket/win] Ensure udp sockets survive sending data to unreachable 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 <[email protected]> Reviewed-by: Siva Annamalai <[email protected]>
1 parent b0e2396 commit ea5fdaa

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed

runtime/bin/socket_win.cc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,12 @@ intptr_t Socket::CreateBindDatagram(const RawAddr& addr,
158158
return -1;
159159
}
160160

161+
// Ensure the socket doesn't get closed if used to send data to unreachable.
162+
BOOL value = FALSE;
163+
DWORD bytes;
164+
WSAIoctl(s, SIO_UDP_CONNRESET, &value, sizeof value, NULL, 0, &bytes, nullptr,
165+
nullptr);
166+
161167
int status;
162168
if (reuseAddress) {
163169
BOOL optval = true;
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// Copyright (c) 2024, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
// Test writing to unreachable udp socket doesn't cause problems.
6+
7+
import 'dart:typed_data';
8+
import 'dart:io';
9+
10+
import "package:expect/expect.dart";
11+
12+
main() async {
13+
var _socket = await RawDatagramSocket.bind(InternetAddress.anyIPv4, 9099);
14+
_socket.listen((RawSocketEvent event) {
15+
print("event: $event");
16+
switch (event) {
17+
case RawSocketEvent.read:
18+
Datagram? d = _socket.receive();
19+
if (d != null) {
20+
print("recv: $d, all done");
21+
_socket.close();
22+
}
23+
break;
24+
case RawSocketEvent.write:
25+
print('received write event $event');
26+
break;
27+
}
28+
}, onError: (e) {
29+
Expect.fail('Should be no exceptions, but got $e');
30+
});
31+
32+
for (int i = 0; i < 100; i++) {
33+
_socket.send(Uint8List(10), InternetAddress("127.0.0.1"), 9100);
34+
}
35+
_socket.send(Uint8List(10), InternetAddress("127.0.0.1"), 9099);
36+
}

0 commit comments

Comments
 (0)