Open matanlurey opened 2 months ago
I might check with the flutter
CLI folks to see how they handle it.
Summary: Dart CLI commands, like dart run
and dart test
, hang when internet connectivity is slow or unstable due to analytics collection blocking the execution flow. This issue can be resolved by disabling analytics, but the process itself is also affected by the network latency.
In our previous implementation of analytics it was possible to get a future for the last item sent, wait on that for a brief time w/ a 100ms-200ms timeout, and exit the process if there was a delay in sending (to not hang the VM exit waiting for analytics). We've since re-written the analytics implementation; I'm not sure who's responsible for the integration into the dart cli, but @bkonyi would be the best person to start with.
In our previous implementation of analytics it was possible to get a future for the last item sent, wait on that for a brief time w/ a 100ms-200ms timeout, and exit the process if there was a delay in sending (to not hang the VM exit waiting for analytics). We've since re-written the analytics implementation; I'm not sure who's responsible for the integration into the dart cli, but @bkonyi would be the best person to start with.
Yeah, I'm responsible for the CLI. I thought Elias had done some work to account for this (I know the legacy analytics timeout behavior was brought up), but maybe this never ended up happening.
So this call: https://github.com/dart-lang/sdk/blob/main/pkg/dartdev/lib/dartdev.dart#L262
Should default to timing out after 250ms: https://github.com/dart-lang/tools/blob/main/pkgs/unified_analytics/lib/src/analytics.dart#L548
I wonder if there is some other code path by which we don't run this finally
? Or some other async call to unified_analytics is hanging, before we get to the finally?
Hmm, when we timeout, we call _gaClient.close();
, but I'm thinking maybe that doesn't cancel our pending requests?
https://github.com/dart-lang/tools/blob/main/pkgs/unified_analytics/lib/src/analytics.dart#L554
If I trace the code, it eventually gets to <IOClient>.close()
, which does a .close(force: true
), which should cancel pending requests.
If I trace the code, it eventually gets to
<IOClient>.close()
, which does a.close(force: true
), which should cancel pending requests.
Ok, that makes sense. Also, assuming the flutter cli tool is not susceptible to this bug, then presumably Analytics.close()
is closing these connections.
It also happen to me on a newly created project, it consistently freeze for 20 seconds before exiting when analytics is enable.
CC @DanTup ref: https://github.com/Dart-Code/Dart-Code/issues/3270
To reproduce, intentionally make your internet suck or be unstable. Try a subway or something :)
Then, any command that sends analytics, i.e.
dart run
ordart test
hangs for a period of time.To "fix", disable analytics (which ironically itself hangs for that same period):
Analytics collection should run "out of band", that is, it should not be blocking (I would not use
await
). I might go an additional step further and say that perhaps the actual mechanism that sends the analytics to a server should happen elsewhere, i.e. I should never have this happen: