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

WebSocket can send and receive messages despite being closed #25536

Open
nex3 opened this issue Jan 20, 2016 · 3 comments
Open

WebSocket can send and receive messages despite being closed #25536

nex3 opened this issue Jan 20, 2016 · 3 comments
Labels
area-core-library SDK core library issues (core, async, ...); use area-vm or area-web for platform specific libraries. library-io P2 A bug or feature request we're likely to work on type-bug Incorrect behavior (everything from a crash to more subtle misbehavior)

Comments

@nex3
Copy link
Member

nex3 commented Jan 20, 2016

If a WebSocket client's connection is closed by the server after its subscription has been canceled, it's still able to send messages. I haven't read the spec or examined the protocol-level interactions in detail, but this seems wrong.

What's more, Dart's WebSocket server will also receive this message—despite having called both WebSocket.close() and HttpServer.close(). This is clearly wrong, since the documentation of WebSocket.close() says that it "closes the WebSocket connection".

Here's a reproduction:

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

main() async {
  var server = await HttpServer.bind('localhost', 0);
  server.transform(new WebSocketTransformer()).listen((socket) {
    socket.listen((msg) {
      print("got $msg");

      // As soon as the server's socket receives a message, close the server and
      // the socket. Once these complete there should be no server-side sockets
      // open at all.
      socket.close().then((_) => print("socket closed"));
      server.close().then((_) => print("server closed"));
    });
  });

  var ws = await WebSocket.connect('ws://localhost:${server.port}');

  // Create and cancel a subscription.
  ws.listen(null).cancel();

  // Send a message to the server that will cause it to close the WebSocket
  // connection.
  ws.add("foo");

  // Give the server time to shut down.
  await new Future.delayed(new Duration(seconds: 1));

  // Try to send another message.
  ws.add("bar");
}

I reproduced this as of 77101d6.

I'd expect the socket to continue listening to the server behind-the-scenes even after the subscription is cancelled, so that it can determine when the connection is closed.

@nex3 nex3 added Type-Defect area-core-library SDK core library issues (core, async, ...); use area-vm or area-web for platform specific libraries. library-io labels Jan 20, 2016
@kevmoo kevmoo added type-bug Incorrect behavior (everything from a crash to more subtle misbehavior) and removed Type-Defect labels Mar 1, 2016
@sgjesse sgjesse removed their assignment Jun 14, 2016
@NatoBoram
Copy link

I can confirm. Moreover, WebSocket.readyState always returns WebSocket.OPEN. #32876

@mskv
Copy link

mskv commented Mar 5, 2019

I can confirm as well. I have just stumbled upon it while testing how my Android Flutter app behaves after turning on and off the airplane mode. The socket becomes unusable with no indication of the problem. As mentioned, even the readyState is OPEN.

@mskv
Copy link

mskv commented Mar 5, 2019

Actually, my case described above seems to be resolved by reacting to ws.done. I can re-establish the connection then after an offline period.

@sortie sortie added the P2 A bug or feature request we're likely to work on label Mar 22, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-core-library SDK core library issues (core, async, ...); use area-vm or area-web for platform specific libraries. library-io P2 A bug or feature request we're likely to work on type-bug Incorrect behavior (everything from a crash to more subtle misbehavior)
Projects
None yet
Development

No branches or pull requests

6 participants