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

HttpClient.close() doesn't close the connection #31492

Closed
sgrekhov opened this issue Nov 30, 2017 · 4 comments
Closed

HttpClient.close() doesn't close the connection #31492

sgrekhov opened this issue Nov 30, 2017 · 4 comments
Assignees
Labels
area-core-library SDK core library issues (core, async, ...); use area-vm or area-web for platform specific libraries. library-io

Comments

@sgrekhov
Copy link
Contributor

sgrekhov commented Nov 30, 2017

According to the HttpClient.close() documentation

Shut down the HTTP client. If force is false (the default) the HttpClient will be kept alive until all active connections are done. If force is true any active connections will be closed to immediately release all resources. These closed connections will receive an error event to indicate that the client was shut down. In both cases trying to establish a new connection after calling close will throw an exception.

In fact, HttpClient.close() doesn't close the client even with force true flag. And after close() call HTTP request can be sent from the client. Example

import "dart:io";
import "dart:async";
import "dart:convert";

var localhost = InternetAddress.LOOPBACK_IP_V4.address;

main() async {
  String helloWorld = "Hello world!";
  HttpServer server = await HttpServer.bind(localhost, 0);
  server.listen((HttpRequest request) {
    request.response.write(helloWorld);
    request.response.close();
    print("Request processing completed");
  });

  new Timer(new Duration(seconds: 1), () {
    print("Server closed");
    server.close();
  });

  HttpClient client = new HttpClient();

  client.getUrl(Uri.parse("http://${localhost}:${server.port}"))
      .then((HttpClientRequest request) {
    print("Request 1 sent");
    return request.close();
  }).then((HttpClientResponse response) {
    response.transform(UTF8.decoder).listen((content) {
      print("Response 1:" + content);
      client.close(force: true);
      print("HttpClient closed");

      client.getUrl(Uri.parse("http://${localhost}:${server.port}"))
          .then((HttpClientRequest request) {
        print("Request 2 sent");
        return request.close();
      }).then((HttpClientResponse response) {
        response.transform(UTF8.decoder).listen((content) {
          print("Response 2:" + content);
        });
      });
    });
  });
}

This program produces

Request 1 sent
Request processing completed
Response 1:Hello world!
HttpClient closed
Request 2 sent
Request processing completed
Response 2:Hello world!
Server closed

Tested on Dart VM version: 2.0.0-dev.8.0 (Fri Nov 17 14:04:05 2017) on "windows_x64"

@anders-sandholm anders-sandholm added area-core-library SDK core library issues (core, async, ...); use area-vm or area-web for platform specific libraries. library-io labels Dec 4, 2017
@yhua537
Copy link

yhua537 commented Apr 16, 2018

Having the same issue and it is causing problem with the web server which has limited resource.

Any update on this?

@qwilbird
Copy link

+1
Any update on this?

@gamegrd
Copy link

gamegrd commented Dec 28, 2018

+1
Any update on this?

@kevmoo
Copy link
Member

kevmoo commented Dec 28, 2018

@a-siva @mit-mit – who is looking at these types of issues?

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
Projects
None yet
Development

No branches or pull requests

7 participants