From 2db57e5a8afbfa5f89c50f1745e437a6168b53e8 Mon Sep 17 00:00:00 2001 From: Tuomas Hietanen Date: Wed, 18 Nov 2026 03:02:51 +0000 Subject: [PATCH] Pass cancellation tokens, use Lock for lock fields and Random.Shared (#10623) * Automatic code-cleanup with csharp-refactor tool: Pass cancellation tokens, use Lock for lock fields and Random.Shared * Added one more file I missed from previous commit --- .../Services/CoreConfig/V2ray/V2rayInboundService.cs | 2 +- v2rayN/v2rayN.WebAPI/Services/LogBuffer.cs | 2 +- .../Services/RuntimeOperationCoordinator.cs | 2 +- v2rayN/v2rayN.WebAPI/Services/V2rayRuntime.Backup.cs | 2 +- .../v2rayN.WebAPI/Services/V2rayRuntime.CoreUpdate.cs | 2 +- v2rayN/v2rayN.WebAPI/Services/V2rayRuntime.cs | 10 +++++----- 6 files changed, 10 insertions(+), 10 deletions(-) diff --git a/v2rayN/ServiceLib/Services/CoreConfig/V2ray/V2rayInboundService.cs b/v2rayN/ServiceLib/Services/CoreConfig/V2ray/V2rayInboundService.cs index 0c89e731..ebc638bb 100644 --- a/v2rayN/ServiceLib/Services/CoreConfig/V2ray/V2rayInboundService.cs +++ b/v2rayN/ServiceLib/Services/CoreConfig/V2ray/V2rayInboundService.cs @@ -62,7 +62,7 @@ public partial class CoreConfigV2rayService var tunInbound = JsonUtils.Deserialize(EmbedUtils.GetEmbedText(Global.V2raySampleTunInbound)) ?? new Inbounds4Ray(); - tunInbound.settings.name = context.IsMacOS ? $"utun{new Random().Next(99)}" : "xray_tun"; + tunInbound.settings.name = context.IsMacOS ? $"utun{Random.Shared.Next(99)}" : "xray_tun"; tunInbound.settings.MTU = _config.TunModeItem.Mtu; var address = _config.TunModeItem.IPv4Address.NullIfEmpty() ?? Global.TunIPv4Address.First(); diff --git a/v2rayN/v2rayN.WebAPI/Services/LogBuffer.cs b/v2rayN/v2rayN.WebAPI/Services/LogBuffer.cs index a81bfa15..b00afdda 100644 --- a/v2rayN/v2rayN.WebAPI/Services/LogBuffer.cs +++ b/v2rayN/v2rayN.WebAPI/Services/LogBuffer.cs @@ -7,7 +7,7 @@ public sealed class LogBuffer private const int Capacity = 2000; private const int MaximumMessageCharacters = 16 * 1024; private const int MaximumRetainedCharacters = 1_000_000; - private readonly object _gate = new(); + private readonly Lock _gate = new(); private readonly Queue _items = new(); private int _retainedCharacters; private long _generation; diff --git a/v2rayN/v2rayN.WebAPI/Services/RuntimeOperationCoordinator.cs b/v2rayN/v2rayN.WebAPI/Services/RuntimeOperationCoordinator.cs index 9c0e1e07..570cdab1 100644 --- a/v2rayN/v2rayN.WebAPI/Services/RuntimeOperationCoordinator.cs +++ b/v2rayN/v2rayN.WebAPI/Services/RuntimeOperationCoordinator.cs @@ -8,7 +8,7 @@ namespace v2rayN.WebAPI.Services; public sealed class RuntimeOperationCoordinator { private static readonly TimeSpan DefaultDrainTimeout = TimeSpan.FromSeconds(10); - private readonly object _sync = new(); + private readonly Lock _sync = new(); private readonly CancellationTokenSource _shutdown = new(); private TaskCompletionSource _changed = NewSignal(); private int _activeOperations; diff --git a/v2rayN/v2rayN.WebAPI/Services/V2rayRuntime.Backup.cs b/v2rayN/v2rayN.WebAPI/Services/V2rayRuntime.Backup.cs index 7ce76a48..3745242a 100644 --- a/v2rayN/v2rayN.WebAPI/Services/V2rayRuntime.Backup.cs +++ b/v2rayN/v2rayN.WebAPI/Services/V2rayRuntime.Backup.cs @@ -8,7 +8,7 @@ namespace v2rayN.WebAPI.Services; public sealed partial class V2rayRuntime { - private readonly object _restoreShutdownGate = new(); + private readonly Lock _restoreShutdownGate = new(); private Task? _restoreShutdownTask; private const long MaxBackupArchiveBytes = 64L * 1024 * 1024; private const long MaxBackupExpandedBytes = 256L * 1024 * 1024; diff --git a/v2rayN/v2rayN.WebAPI/Services/V2rayRuntime.CoreUpdate.cs b/v2rayN/v2rayN.WebAPI/Services/V2rayRuntime.CoreUpdate.cs index 18b27cb2..bd113a17 100644 --- a/v2rayN/v2rayN.WebAPI/Services/V2rayRuntime.CoreUpdate.cs +++ b/v2rayN/v2rayN.WebAPI/Services/V2rayRuntime.CoreUpdate.cs @@ -16,7 +16,7 @@ public sealed partial class V2rayRuntime internal static List? NormalizeWebUpdateTargets(IEnumerable? targets) => targets?.Select(NormalizeWebUpdateTarget).Distinct(StringComparer.Ordinal).ToList(); - private readonly object _updateTaskGate = new(); + private readonly Lock _updateTaskGate = new(); private readonly GeoFilesUpdateGate _geoUpdateGate = new(); private readonly ConcurrentDictionary _coreUpdateTasks = new(); private readonly ConcurrentDictionary _updateProgress = new(StringComparer.Ordinal); diff --git a/v2rayN/v2rayN.WebAPI/Services/V2rayRuntime.cs b/v2rayN/v2rayN.WebAPI/Services/V2rayRuntime.cs index d2b3d0e3..eb8bbea4 100644 --- a/v2rayN/v2rayN.WebAPI/Services/V2rayRuntime.cs +++ b/v2rayN/v2rayN.WebAPI/Services/V2rayRuntime.cs @@ -28,8 +28,8 @@ public sealed partial class V2rayRuntime( private readonly SemaphoreSlim _coreGate = new(1, 1); private readonly SemaphoreSlim _regionalPresetGate = new(1, 1); private readonly SemaphoreSlim _coreMonitorGate = new(1, 1); - private readonly object _subscriptionGate = new(); - private readonly object _speedtestGate = new(); + private readonly Lock _subscriptionGate = new(); + private readonly Lock _speedtestGate = new(); private readonly Dictionary _subscriptionTasks = new(StringComparer.Ordinal); private SpeedtestService? _speedtestService; private Task? _speedtestTask; @@ -246,7 +246,7 @@ public sealed partial class V2rayRuntime( Config.Inbound[0].AllowLANConn = allowProxyFromLan; } await EnsureConfigSaveSucceededAsync(() => ConfigTextHandler.SaveConfig()); - }); + }, cancellationToken); await ConfigDbHandler.InitBuiltinDNS(); await BuiltinDnsProfileBootstrap.EnsureAsync( async () => await ConfigDbQuery.DNSItems() ?? [], @@ -268,7 +268,7 @@ public sealed partial class V2rayRuntime( } var selectedProfileId = Config.IndexId; var defaultProfile = await ConfigTextHandler.GetDefaultServer(); - await _mutations.RunAsync(() => EnsureConfigSaveSucceededAsync(() => ConfigTextHandler.SaveConfig())); + await _mutations.RunAsync(() => EnsureConfigSaveSucceededAsync(() => ConfigTextHandler.SaveConfig()), cancellationToken); await ProfileExManager.Instance.Init(); await CertPemManager.Instance.Init(Config); // The Web frontend owns its runtime and rejects generated launch contexts that enable TUN. @@ -780,7 +780,7 @@ public sealed partial class V2rayRuntime( { Config.IndexId = profile.IndexId; await EnsureConfigSaveSucceededAsync(() => ConfigTextHandler.SaveConfig()); - }); + }, cancellationToken); } return await LaunchPreflightedCoreLockedAsync(preflight, cancellationToken);