From 486ac2ed68a9d54c86114a773ed70fda1b958d8d Mon Sep 17 00:00:00 2001 From: Gabriel Dufresne Date: Fri, 10 Jul 2026 15:14:04 -0400 Subject: [PATCH] Skip telemetry work when it cannot be sent --- .../TelemetryHandler.cs | 22 +++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/src/UniGetUI.Interface.Telemetry/TelemetryHandler.cs b/src/UniGetUI.Interface.Telemetry/TelemetryHandler.cs index 8d85cbf80..cbfc45ddc 100644 --- a/src/UniGetUI.Interface.Telemetry/TelemetryHandler.cs +++ b/src/UniGetUI.Interface.Telemetry/TelemetryHandler.cs @@ -104,7 +104,10 @@ public static async Task InitializeAsync() { try { - if (Settings.Get(Settings.K.DisableTelemetry)) + // Bail out before waiting for internet or gathering any data when telemetry + // can't be sent — opted out, or this build has no OpenSearch credentials + // (every from-source/community build). Only the official build proceeds. + if (Settings.Get(Settings.K.DisableTelemetry) || !CredentialsConfigured()) return; await CoreTools.WaitForInternetConnection(); @@ -216,7 +219,9 @@ private static void EnqueuePackageEvent( { if (result is null && eventSource is null) throw new ArgumentException("result and eventSource cannot both be null"); - if (Settings.Get(Settings.K.DisableTelemetry)) + // Skip enqueuing entirely when telemetry can't be sent, so events don't + // accumulate only to be drained-and-discarded at flush time. + if (Settings.Get(Settings.K.DisableTelemetry) || !CredentialsConfigured()) return; _pendingPackageEvents.Enqueue(new UniGetUIPackageEvent @@ -240,6 +245,9 @@ private static void EnqueuePackageEvent( /// public static async Task FlushPackageEventsAsync() { + if (Settings.Get(Settings.K.DisableTelemetry) || !CredentialsConfigured()) + return; + if (_pendingPackageEvents.IsEmpty) return; @@ -311,7 +319,7 @@ private static async Task TrackBundleEventAsync(string operation, string bundleT { try { - if (Settings.Get(Settings.K.DisableTelemetry)) + if (Settings.Get(Settings.K.DisableTelemetry) || !CredentialsConfigured()) return; await CoreTools.WaitForInternetConnection(); @@ -397,8 +405,14 @@ private static TelemetryApplicationInfo BuildApplicationInfo() => ArchitectureType = RuntimeInformation.ProcessArchitecture.ToString(), }; + // Every field here is constant for the process lifetime, so build it once and + // reuse the instance across all events (including each doc in a bulk flush). + // The value is only ever read (serialized), never mutated, so sharing is safe; + // a benign race just builds it twice with identical results. + private static TelemetryPlatformInfo? _platformInfo; + private static TelemetryPlatformInfo BuildPlatformInfo() => - new() + _platformInfo ??= new() { Name = GetPlatformName(), Version = Environment.OSVersion.VersionString,