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
This commit is contained in:
Tuomas Hietanen
2026-11-18 03:02:51 +00:00
committed by GitHub
parent 56cfc4d0f8
commit 2db57e5a8a
6 changed files with 10 additions and 10 deletions
@@ -62,7 +62,7 @@ public partial class CoreConfigV2rayService
var tunInbound =
JsonUtils.Deserialize<Inbounds4Ray>(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();
+1 -1
View File
@@ -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<LogView> _items = new();
private int _retainedCharacters;
private long _generation;
@@ -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;
@@ -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;
@@ -16,7 +16,7 @@ public sealed partial class V2rayRuntime
internal static List<string>? NormalizeWebUpdateTargets(IEnumerable<string>? 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<ECoreType, Task> _coreUpdateTasks = new();
private readonly ConcurrentDictionary<string, CoreUpdateProgressView> _updateProgress = new(StringComparer.Ordinal);
@@ -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<string, Task> _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);