Why does my dart program hang for 5 seconds after the last line of main()?

66 views
Skip to first unread message

Danny Tuppeny

unread,
Aug 13, 2016, 6:24:40 AM8/13/16
to Dart Misc

This simple dart app seems to wait approx 5 seconds after the last line of main before terminating. I'm writing the response to the screen, so it's not that my async request is still ongoing.

Any ideas what's going on?

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

Future<String> _sendRequest() async {
  var http = new HttpClient();
  return http
      .postUrl(Uri.parse("https://www.google.com/404"))
      .then((HttpClientRequest request) => request.close())
      .then((HttpClientResponse response) {
    return response.transform(UTF8.decoder).join("");
  });
}

main(List<String> args) async {
  print("starting...");
  print(await _sendRequest());
  print("finished");
}

Andreas Kirsch

unread,
Aug 13, 2016, 6:42:57 AM8/13/16
to General Dart Discussion

I think you need to close the HttpClient as well.


--
For other discussions, see https://groups.google.com/a/dartlang.org/
 
For HOWTO questions, visit http://stackoverflow.com/tags/dart
 
To file a bug report or feature request, go to http://www.dartbug.com/new
---
You received this message because you are subscribed to the Google Groups "Dart Misc" group.
To unsubscribe from this group and stop receiving emails from it, send an email to misc+uns...@dartlang.org.

Danny Tuppeny

unread,
Aug 13, 2016, 6:57:21 AM8/13/16
to Dart Misc
Look like you're right; this works:

Future<String> _sendRequest() async {
  var http = new HttpClient();
  return http
      .postUrl(Uri.parse("https://www.google.com/404"))
      .then((HttpClientRequest request) => request.close())
      .then((HttpClientResponse response) {
    return response.transform(UTF8.decoder).join("");
  }).whenComplete(() => http.close());
}

But, it doesn't look very nice. Is there a better way to write this?

William Hesse

unread,
Aug 13, 2016, 10:04:44 AM8/13/16
to General Dart Discussion

Since your function body is defined as async, you can use try-finally:

try {
  var request = await http.postUrl(...);
  var response = await request.close();
  return response.transform(...).join('');
} finally {
  http.close();
}

Danny Tuppeny

unread,
Aug 13, 2016, 10:11:30 AM8/13/16
to General Dart Discussion
Ah, that looks better (especially with the awaits instead of chaining - not sure why I didn't do that!).

I'm guessing Dart doesn't have anything similar to C#'s using?

William Hesse

unread,
Aug 13, 2016, 11:18:42 AM8/13/16
to General Dart Discussion

No, because Dart doesn't have destructors like C# or C++ or Python, so we can't do cleanup or RIIA (resource allocation is initialization) the same way.

Danny Tuppeny

unread,
Aug 13, 2016, 11:27:14 AM8/13/16
to mi...@dartlang.org
On Sat, 13 Aug 2016 at 16:18 'William Hesse' via Dart Misc <mi...@dartlang.org> wrote:

No, because Dart doesn't have destructors like C# or C++ or Python, so we can't do cleanup or RIIA (resource allocation is initialization) the same way.

Are they required? In C# AFAIK all using does is require an IDisposable (which is just an interface with a Dispose method) and call it's dispose method. Even if you're not using unmanaged resources or destructors you can use IDisposable and usings (in fact, it gets "abused" for stuff like that all the time!)?
Reply all
Reply to author
Forward
0 new messages