Skip to content

Commit 5db8186

Browse files
committed
[ECO-5584] Updated Platform.PlatformId property to be thread safe
1 parent 12aee8d commit 5db8186

File tree

3 files changed

+27
-48
lines changed

3 files changed

+27
-48
lines changed

src/IO.Ably.NETFramework/Platform.cs

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,24 +9,14 @@ internal class Platform : IPlatform
99
{
1010
private static readonly object _lock = new object();
1111

12-
static Platform()
13-
{
14-
Initialize();
15-
}
16-
17-
internal static bool HookedUpToNetworkEvents { get; private set; }
12+
internal static bool HookedUpToNetworkEvents { get; set; }
1813

1914
public Agent.PlatformRuntime PlatformId => Agent.PlatformRuntime.Framework;
2015

2116
public ITransportFactory TransportFactory => null;
2217

2318
public IMobileDevice MobileDevice { get; set; }
2419

25-
internal static void Initialize()
26-
{
27-
HookedUpToNetworkEvents = false;
28-
}
29-
3020
public void RegisterOsNetworkStateChanged()
3121
{
3222
lock (_lock)

src/IO.Ably.NETStandard20/Platform.cs

Lines changed: 24 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System.Net.NetworkInformation;
1+
using System;
2+
using System.Net.NetworkInformation;
23
using System.Runtime.InteropServices;
34
using IO.Ably.Push;
45
using IO.Ably.Realtime;
@@ -9,63 +10,51 @@ namespace IO.Ably
910
internal class Platform : IPlatform
1011
{
1112
private static readonly object Lock = new object();
12-
private Agent.PlatformRuntime? _platformId;
13+
private readonly Lazy<Agent.PlatformRuntime> _platformId;
1314

14-
static Platform()
15+
public Platform()
1516
{
16-
Initialize();
17+
_platformId = new Lazy<Agent.PlatformRuntime>(DetectPlatformRuntime);
1718
}
1819

19-
internal static bool HookedUpToNetworkEvents { get; private set; }
20+
internal static bool HookedUpToNetworkEvents { get; set; }
2021

2122
// Use runtime detection via RuntimeInformation.FrameworkDescription
2223
// This detects the actual runtime version, not the compile-time target framework
2324
// This is important because netstandard2.0 assemblies can run on .NET 6/7/8/9+
2425
// and we want to report the actual runtime being used
25-
public Agent.PlatformRuntime PlatformId
26+
public Agent.PlatformRuntime PlatformId => _platformId.Value;
27+
28+
private Agent.PlatformRuntime DetectPlatformRuntime()
2629
{
27-
get
28-
{
29-
if (_platformId.HasValue)
30-
{
31-
return _platformId.Value;
32-
}
30+
// Default fallback for netstandard2.0 or unknown runtimes
31+
var platformId = Agent.PlatformRuntime.Netstandard20;
3332

34-
// Default fallback for netstandard2.0 or unknown runtimes
35-
_platformId = Agent.PlatformRuntime.Netstandard20;
33+
try
34+
{
35+
var frameworkDescription = RuntimeInformation.FrameworkDescription;
3636

37-
try
37+
if (frameworkDescription.StartsWith(".NET 6.", StringComparison.OrdinalIgnoreCase))
3838
{
39-
var frameworkDescription = RuntimeInformation.FrameworkDescription;
40-
41-
if (frameworkDescription.StartsWith(".NET 6.", System.StringComparison.OrdinalIgnoreCase))
42-
{
43-
_platformId = Agent.PlatformRuntime.Net6;
44-
}
45-
46-
if (frameworkDescription.StartsWith(".NET 7.", System.StringComparison.OrdinalIgnoreCase))
47-
{
48-
_platformId = Agent.PlatformRuntime.Net7;
49-
}
39+
platformId = Agent.PlatformRuntime.Net6;
5040
}
51-
catch
41+
else if (frameworkDescription.StartsWith(".NET 7.", StringComparison.OrdinalIgnoreCase))
5242
{
53-
// fall back to Netstandard20
43+
platformId = Agent.PlatformRuntime.Net7;
5444
}
55-
56-
return _platformId.Value;
5745
}
46+
catch
47+
{
48+
// fall back to Netstandard20
49+
}
50+
51+
return platformId;
5852
}
5953

6054
public ITransportFactory TransportFactory => null;
6155

6256
public IMobileDevice MobileDevice { get; set; }
6357

64-
internal static void Initialize()
65-
{
66-
HookedUpToNetworkEvents = false;
67-
}
68-
6958
public void RegisterOsNetworkStateChanged()
7059
{
7160
lock (Lock)

src/IO.Ably.Tests.Shared/Realtime/RealtimeSpecs.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,9 +136,9 @@ public void New_Realtime_HasAuth()
136136
public void AutomaticNetworkDetectionCanBeDisabledByClientOption(bool enabled)
137137
{
138138
// Because this test depends on static state in the 'Platform' type we need
139-
// to (re)Initialize the static 'Platform' state before each test run.
139+
// to reset the static 'Platform' state before each test run.
140140

141-
Platform.Initialize();
141+
Platform.HookedUpToNetworkEvents = false;
142142

143143
_ = new AblyRealtime(new ClientOptions(ValidKey)
144144
{

0 commit comments

Comments
 (0)