|
| 1 | +/* ======================================================================== |
| 2 | + * Copyright (c) 2005-2025 The OPC Foundation, Inc. All rights reserved. |
| 3 | + * OPC Foundation MIT License 1.00 |
| 4 | + * ======================================================================*/ |
| 5 | + |
| 6 | +using System; |
| 7 | +using System.Diagnostics; |
| 8 | +using System.Diagnostics.Metrics; |
| 9 | +using System.Linq; |
| 10 | +using System.Threading; |
| 11 | +using System.Threading.Tasks; |
| 12 | +using Microsoft.Extensions.Logging; |
| 13 | +using Microsoft.Extensions.Logging.Abstractions; |
| 14 | +using Opc.Ua; |
| 15 | +using UaLens.Connection; |
| 16 | +using UaLens.Subscriptions; |
| 17 | + |
| 18 | +namespace UaLens; |
| 19 | + |
| 20 | +/// <summary> |
| 21 | +/// Adapter-lifecycle race stress test: spawns many tasks that create + |
| 22 | +/// forget adapters concurrently with a disconnect. Asserts that |
| 23 | +/// <see cref="ConnectionService.ForgetAdapter"/>'s new bool return value |
| 24 | +/// correctly differentiates "you own disposal" vs "Disconnect already |
| 25 | +/// owned it" so no zombies leak through either path. |
| 26 | +/// </summary> |
| 27 | +internal static class AdapterRaceProbe |
| 28 | +{ |
| 29 | + public static async Task<int> RunAsync(string endpointUrl, CancellationToken ct = default) |
| 30 | + { |
| 31 | + var telemetry = new ConsoleTelemetry(); |
| 32 | + Console.WriteLine("== AdapterRace probe =="); |
| 33 | + Console.WriteLine($" endpoint: {endpointUrl}"); |
| 34 | + |
| 35 | + var conn = new ConnectionService(telemetry); |
| 36 | + await using (conn.ConfigureAwait(false)) |
| 37 | + { |
| 38 | + await conn.ConnectAsync(new ConnectionOptions |
| 39 | + { |
| 40 | + EndpointUrl = endpointUrl, |
| 41 | + Engine = SubscriptionEngineKind.ChannelV2 |
| 42 | + }, ct).ConfigureAwait(false); |
| 43 | + |
| 44 | + const int N = 24; |
| 45 | + var adapters = new ISubscriptionAdapter[N]; |
| 46 | + for (int i = 0; i < N; i++) |
| 47 | + { |
| 48 | + adapters[i] = conn.CreateAdapter(); |
| 49 | + } |
| 50 | + |
| 51 | + // Pre-disconnect: each adapter should be forget-able exactly once. |
| 52 | + int forgottenCount = 0; |
| 53 | + for (int i = 0; i < N / 2; i++) |
| 54 | + { |
| 55 | + if (conn.ForgetAdapter(adapters[i])) |
| 56 | + { |
| 57 | + forgottenCount++; |
| 58 | + await adapters[i].DisposeAsync().ConfigureAwait(false); |
| 59 | + } |
| 60 | + } |
| 61 | + if (forgottenCount != N / 2) |
| 62 | + { |
| 63 | + Console.WriteLine($"FAIL: expected {N / 2} ForgetAdapter=true, got {forgottenCount}"); |
| 64 | + return 1; |
| 65 | + } |
| 66 | + |
| 67 | + // Disconnect — should dispose the remaining N/2 adapters that are |
| 68 | + // still tracked; ForgetAdapter for any of those AFTER disconnect |
| 69 | + // must return false (already disposed). |
| 70 | + Task disconnectTask = conn.DisconnectAsync(); |
| 71 | + |
| 72 | + // Race: try ForgetAdapter on the not-yet-forgotten adapters |
| 73 | + // concurrently with the disconnect. Outcome must be EITHER: |
| 74 | + // - ForgetAdapter returns true → caller disposes (no double dispose). |
| 75 | + // - ForgetAdapter returns false → DisconnectInternalAsync is/was disposing. |
| 76 | + int doubleDisposalAttempts = 0; |
| 77 | + var forgotAfter = new bool[N]; |
| 78 | + Parallel.For(N / 2, N, i => |
| 79 | + { |
| 80 | + forgotAfter[i] = conn.ForgetAdapter(adapters[i]); |
| 81 | + if (forgotAfter[i]) |
| 82 | + { |
| 83 | + try |
| 84 | + { |
| 85 | + adapters[i].DisposeAsync().AsTask().Wait(); |
| 86 | + } |
| 87 | + catch (Exception) |
| 88 | + { |
| 89 | + Interlocked.Increment(ref doubleDisposalAttempts); |
| 90 | + } |
| 91 | + } |
| 92 | + }); |
| 93 | + |
| 94 | + await disconnectTask.ConfigureAwait(false); |
| 95 | + |
| 96 | + Console.WriteLine($" forgot pre-disconnect : {forgottenCount}"); |
| 97 | + Console.WriteLine($" forgot during disconn : {forgotAfter.Skip(N / 2).Count(b => b)}"); |
| 98 | + Console.WriteLine($" double-dispose excepts: {doubleDisposalAttempts}"); |
| 99 | + |
| 100 | + if (doubleDisposalAttempts > 0) |
| 101 | + { |
| 102 | + Console.WriteLine("FAIL: double-disposal detected."); |
| 103 | + return 1; |
| 104 | + } |
| 105 | + Console.WriteLine("ADAPTER RACE PROBE PASS"); |
| 106 | + return 0; |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + private sealed class ConsoleTelemetry : ITelemetryContext |
| 111 | + { |
| 112 | + public ILoggerFactory LoggerFactory { get; } = NullLoggerFactory.Instance; |
| 113 | + public Meter CreateMeter() => new("UaLens.AdapterRaceProbe"); |
| 114 | + public ActivitySource ActivitySource { get; } = new("UaLens.AdapterRaceProbe"); |
| 115 | + } |
| 116 | +} |
0 commit comments