Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Commit d37a568

Browse files
committed
Linux: Fix NetworkAddressChange NullReferenceException on AddressChange when no Availability subscribers
1 parent f8057cc commit d37a568

File tree

1 file changed

+16
-15
lines changed

1 file changed

+16
-15
lines changed

src/System.Net.NetworkInformation/src/System/Net/NetworkInformation/NetworkAddressChange.Linux.cs

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,8 @@ public class NetworkChange
1515
private static NetworkAddressChangedEventHandler s_addressChangedSubscribers;
1616
private static NetworkAvailabilityChangedEventHandler s_availabilityChangedSubscribers;
1717
private static volatile int s_socket = 0;
18-
// Lock controlling access to delegate subscriptions and socket initialization.
19-
private static readonly object s_subscriberLock = new object();
20-
// Lock controlling access to availability-changed state and timer.
21-
private static readonly object s_availabilityLock = new object();
18+
// Lock controlling access to delegate subscriptions, socket initialization, availability-changed state and timer.
19+
private static readonly object s_gate = new object();
2220
private static readonly Interop.Sys.NetworkChangeEvent s_networkChangeCallback = ProcessEvent;
2321

2422
// The "leniency" window for NetworkAvailabilityChanged socket events.
@@ -39,7 +37,7 @@ public static event NetworkAddressChangedEventHandler NetworkAddressChanged
3937
{
4038
add
4139
{
42-
lock (s_subscriberLock)
40+
lock (s_gate)
4341
{
4442
if (s_socket == 0)
4543
{
@@ -51,7 +49,7 @@ public static event NetworkAddressChangedEventHandler NetworkAddressChanged
5149
}
5250
remove
5351
{
54-
lock (s_subscriberLock)
52+
lock (s_gate)
5553
{
5654
if (s_addressChangedSubscribers == null && s_availabilityChangedSubscribers == null)
5755
{
@@ -72,7 +70,7 @@ public static event NetworkAvailabilityChangedEventHandler NetworkAvailabilityCh
7270
{
7371
add
7472
{
75-
lock (s_subscriberLock)
73+
lock (s_gate)
7674
{
7775
if (s_socket == 0)
7876
{
@@ -88,7 +86,7 @@ public static event NetworkAvailabilityChangedEventHandler NetworkAvailabilityCh
8886
}
8987
remove
9088
{
91-
lock (s_subscriberLock)
89+
lock (s_gate)
9290
{
9391
if (s_addressChangedSubscribers == null && s_availabilityChangedSubscribers == null)
9492
{
@@ -103,6 +101,7 @@ public static event NetworkAvailabilityChangedEventHandler NetworkAvailabilityCh
103101
{
104102
s_availabilityTimer.Dispose();
105103
s_availabilityTimer = null;
104+
s_availabilityHasChanged = false;
106105
}
107106

108107
if (s_addressChangedSubscribers == null)
@@ -156,7 +155,7 @@ private static void ProcessEvent(int socket, Interop.Sys.NetworkChangeKind kind)
156155
{
157156
if (kind != Interop.Sys.NetworkChangeKind.None)
158157
{
159-
lock (s_subscriberLock)
158+
lock (s_gate)
160159
{
161160
if (socket == s_socket)
162161
{
@@ -175,14 +174,16 @@ private static void OnSocketEvent(Interop.Sys.NetworkChangeKind kind)
175174
s_addressChangedSubscribers?.Invoke(null, EventArgs.Empty);
176175
break;
177176
case Interop.Sys.NetworkChangeKind.AvailabilityChanged:
178-
lock (s_availabilityLock)
177+
lock (s_gate)
179178
{
180-
if (!s_availabilityHasChanged)
179+
if (s_availabilityTimer != null)
181180
{
182-
s_availabilityTimer.Change(AvailabilityTimerWindowMilliseconds, -1);
181+
if (!s_availabilityHasChanged)
182+
{
183+
s_availabilityTimer.Change(AvailabilityTimerWindowMilliseconds, -1);
184+
}
185+
s_availabilityHasChanged = true;
183186
}
184-
185-
s_availabilityHasChanged = true;
186187
}
187188
break;
188189
}
@@ -191,7 +192,7 @@ private static void OnSocketEvent(Interop.Sys.NetworkChangeKind kind)
191192
private static void OnAvailabilityTimerFired(object state)
192193
{
193194
bool changed;
194-
lock (s_availabilityLock)
195+
lock (s_gate)
195196
{
196197
changed = s_availabilityHasChanged;
197198
s_availabilityHasChanged = false;

0 commit comments

Comments
 (0)