dart-lang / sdk

The Dart SDK, including the VM, JS and Wasm compilers, analysis, core libraries, and more.
https://dart.dev
BSD 3-Clause "New" or "Revised" License
10.24k stars 1.58k forks source link

[Windows] [ServerSocket] ServerSocket.Close() not closing socket and continue working #54882

Closed NickNevzorov closed 9 months ago

NickNevzorov commented 9 months ago

Steps to reproduce

  1. Run project
  2. Connect to socket
  3. Send data
  4. Wait while close() executed
  5. Send data again

Expected results

ServerSocket may close socket

Actual results

ServerSocket did not close socket

Code sample

Code sample ```dart import 'dart:convert'; import 'dart:io'; void main() async { ServerSocket messagesSocket = await ServerSocket.bind(InternetAddress.anyIPv4, 11000); print('Messages listen: ' + Platform.localHostname + ':' + messagesSocket!.port.toString()); messagesSocket?.listen((Socket client) { client.listen((data) async { var message = utf8.decode(data).trim(); print("server listen : $message"); }); }); await Future.delayed(Duration(seconds: 10)); await messagesSocket.close(); print("server close"); } ```

Logs

Logs ```console flutter: Messages listen: Nick-PC:11000 flutter: server listen : flutter: server close flutter: server listen : flutter: server listen : flutter: server listen : ```

Flutter Doctor output

Doctor output ```console [√] Flutter (Channel stable, 3.16.9, on Microsoft Windows [Version 10.0.19045.3930], locale ru-UA) • Flutter version 3.16.9 on channel stable at d:\sdk\flutter • Upstream repository https://github.com/flutter/flutter.git • Framework revision 41456452f2 (2 weeks ago), 2024-01-25 10:06:23 -0800 • Engine revision f40e976bed • Dart version 3.2.6 • DevTools version 2.28.5 [√] Windows Version (Installed version of Windows is version 10 or higher) [√] Android toolchain - develop for Android devices (Android SDK version 31.0.0) • Android SDK at d:\sdk\android\sdk\ • Platform android-33, build-tools 31.0.0 • ANDROID_HOME = d:\sdk\android\sdk\ • Java binary at: C:\Program Files\Android\Android Studio\jbr\bin\java • Java version OpenJDK Runtime Environment (build 17.0.7+0-b2043.56-10550314) • All Android licenses accepted. [√] Chrome - develop for the web • Chrome at C:\Program Files (x86)\Google\Chrome\Application\chrome.exe [√] Visual Studio - develop Windows apps (Visual Studio Community 2022 17.7.3) • Visual Studio at C:\Program Files\Microsoft Visual Studio\2022\Community • Visual Studio Community 2022 version 17.7.34024.191 • Windows 10 SDK version 10.0.22000.0 [√] Android Studio (version 2023.1) • Android Studio at C:\Program Files\Android\Android Studio • Flutter plugin can be installed from: https://plugins.jetbrains.com/plugin/9212-flutter • Dart plugin can be installed from: https://plugins.jetbrains.com/plugin/6351-dart • android-studio-dir = C:\Program Files\Android\Android Studio • Java version OpenJDK Runtime Environment (build 17.0.7+0-b2043.56-10550314) [√] VS Code (version 1.85.1) • VS Code at C:\Users\Николай\AppData\Local\Programs\Microsoft VS Code https://marketplace.visualstudio.com/items?itemName=Dart-Code.flutter [√] Connected device (3 available) • Windows (desktop) • windows • windows-x64 • Microsoft Windows [Version 10.0.19045.3930] • Chrome (web) • chrome • web-javascript • Google Chrome 121.0.6167.141 • Edge (web) • edge • web-javascript • Microsoft Edge 121.0.2277.106 [√] Network resources • All expected network resources are available. • No issues found! PS C:\1\untitled1> flutter doctor Doctor summary (to see all details, run flutter doctor -v): [√] Android toolchain - develop for Android devices (Android SDK version 31.0.0) [√] Chrome - develop for the web [√] Visual Studio - develop Windows apps (Visual Studio Community 2022 17.7.3) [√] Android Studio (version 2023.1) [√] VS Code (version 1.85.1) [√] Connected device (3 available) [√] Network resources • No issues found! PS C:\1\untitled1> ^C PS C:\1\untitled1> PS C:\1\untitled1> flutter doctor -v [√] Flutter (Channel stable, 3.16.9, on Microsoft Windows [Version 10.0.19045.3930], locale ru-UA) • Flutter version 3.16.9 on channel stable at d:\sdk\flutter • Upstream repository https://github.com/flutter/flutter.git • Framework revision 41456452f2 (2 weeks ago), 2024-01-25 10:06:23 -0800 • Engine revision f40e976bed • Dart version 3.2.6 • DevTools version 2.28.5 [√] Windows Version (Installed version of Windows is version 10 or higher) [√] Android toolchain - develop for Android devices (Android SDK version 31.0.0) • Android SDK at d:\sdk\android\sdk\ • Platform android-33, build-tools 31.0.0 • ANDROID_HOME = d:\sdk\android\sdk\ • Java binary at: C:\Program Files\Android\Android Studio\jbr\bin\java • Java version OpenJDK Runtime Environment (build 17.0.7+0-b2043.56-10550314) • All Android licenses accepted. [√] Chrome - develop for the web • Chrome at C:\Program Files (x86)\Google\Chrome\Application\chrome.exe [√] Visual Studio - develop Windows apps (Visual Studio Community 2022 17.7.3) • Visual Studio at C:\Program Files\Microsoft Visual Studio\2022\Community • Visual Studio Community 2022 version 17.7.34024.191 • Windows 10 SDK version 10.0.22000.0 [√] Android Studio (version 2023.1) • Android Studio at C:\Program Files\Android\Android Studio • Flutter plugin can be installed from: https://plugins.jetbrains.com/plugin/9212-flutter • Dart plugin can be installed from: https://plugins.jetbrains.com/plugin/6351-dart • android-studio-dir = C:\Program Files\Android\Android Studio • Java version OpenJDK Runtime Environment (build 17.0.7+0-b2043.56-10550314) [√] VS Code (version 1.85.1) • VS Code at C:\Users\Николай\AppData\Local\Programs\Microsoft VS Code • Flutter extension can be installed from: https://marketplace.visualstudio.com/items?itemName=Dart-Code.flutter [√] Connected device (3 available) • Windows (desktop) • windows • windows-x64 • Microsoft Windows [Version 10.0.19045.3930] • Chrome (web) • chrome • web-javascript • Google Chrome 121.0.6167.141 • Edge (web) • edge • web-javascript • Microsoft Edge 121.0.2277.106 [√] Network resources • All expected network resources are available. • No issues found! ```
mraleph commented 9 months ago

I think this is working as intended for BSD sockets. Closing listening socket does not affect connections already established - it only prevents new connections from being formed. There is no implicit parent-child relationship between listening socket and sockets created for each incoming connection. They are all independent sockets.

I think in some languages IO implementation maintains a list of active connections on the server socket and shuts all connections down when server socket is closed by the user, but Dart implementation does not do that - so if you want this behavior you will need to implement it manually.