Discussion:
[dart-misc] Why does my dart program hang for 5 seconds after the last line of main()?
Danny Tuppeny
2016-08-13 10:24:39 UTC
Permalink
(Also on SO if that's your thing <http://stackoverflow.com/q/38931710/25124>
)

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");}
--
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+***@dartlang.org.
'Andreas Kirsch' via Dart Misc
2016-08-13 10:42:50 UTC
Permalink
I think you need to close the HttpClient as well.
Post by Danny Tuppeny
(Also on SO if that's your thing
<http://stackoverflow.com/q/38931710/25124>)
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");}
--
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
--
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+***@dartlang.org.
Danny Tuppeny
2016-08-13 10:57:20 UTC
Permalink
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?
Post by 'Andreas Kirsch' via Dart Misc
I think you need to close the HttpClient as well.
Post by Danny Tuppeny
(Also on SO if that's your thing
<http://stackoverflow.com/q/38931710/25124>)
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");}
--
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
--
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+***@dartlang.org.
Danny Tuppeny
2016-08-13 14:11:15 UTC
Permalink
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?

On Sat, 13 Aug 2016 at 15:04 'William Hesse' via Dart Misc <
try {
var request = await http.postUrl(...);
var response = await request.close();
return response.transform(...).join('');
} finally {
http.close();
}
Post by Danny Tuppeny
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?
Post by 'Andreas Kirsch' via Dart Misc
I think you need to close the HttpClient as well.
(Also on SO if that's your thing)
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.
Post by Danny Tuppeny
Post by 'Andreas Kirsch' via Dart Misc
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"); }
Post by Danny Tuppeny
Post by 'Andreas Kirsch' via Dart Misc
--
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
Post by Danny Tuppeny
Post by 'Andreas Kirsch' via Dart Misc
---
You received this message because you are subscribed to the Google
Groups "Dart Misc" group.
Post by Danny Tuppeny
Post by 'Andreas Kirsch' via Dart Misc
To unsubscribe from this group and stop receiving emails from it, send
--
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
Post by Danny Tuppeny
---
You received this message because you are subscribed to the Google
Groups "Dart Misc" group.
Post by Danny Tuppeny
To unsubscribe from this group and stop receiving emails from it, send
--
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
--
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+***@dartlang.org.
'William Hesse' via Dart Misc
2016-08-13 15:18:37 UTC
Permalink
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.
Post by Danny Tuppeny
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?
On Sat, 13 Aug 2016 at 15:04 'William Hesse' via Dart Misc <
try {
var request = await http.postUrl(...);
var response = await request.close();
return response.transform(...).join('');
} finally {
http.close();
}
Post by Danny Tuppeny
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?
Post by 'Andreas Kirsch' via Dart Misc
I think you need to close the HttpClient as well.
(Also on SO if that's your thing)
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.
Post by Danny Tuppeny
Post by 'Andreas Kirsch' via Dart Misc
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"); }
Post by Danny Tuppeny
Post by 'Andreas Kirsch' via Dart Misc
--
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
Post by Danny Tuppeny
Post by 'Andreas Kirsch' via Dart Misc
---
You received this message because you are subscribed to the Google
Groups "Dart Misc" group.
Post by Danny Tuppeny
Post by 'Andreas Kirsch' via Dart Misc
To unsubscribe from this group and stop receiving emails from it,
--
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
Post by Danny Tuppeny
---
You received this message because you are subscribed to the Google
Groups "Dart Misc" group.
Post by Danny Tuppeny
To unsubscribe from this group and stop receiving emails from it, send
--
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
--
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
--
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+***@dartlang.org.
Danny Tuppeny
2016-08-13 15:27:00 UTC
Permalink
On Sat, 13 Aug 2016 at 16:18 'William Hesse' via Dart Misc <
Post by 'William Hesse' via Dart Misc
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!)?
--
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+***@dartlang.org.
Loading...