Fix System.Net.NetworkInformation.NetworkChange deadlock (dotnet/corefx#26259)
User code can subscribe to the NetworkAvailabilityChanged event. If a Network Address changed event get triggered while the user code is unsubscribing from the NetworkAvailabilityChanged event, there will be a deadlock.
The deadlock can happen because two threads are holding two different locks for AddressChangeListener and AvailabilityChangeListener, and they are waiting for each other to release its lock first. The fix is using a global lock to replace two locks, so that we don’t allow operations on AddressChangeListener and AvailabilityChangeListener happen at the same time.
Without fixing this, users can frequently see the deadlock when they put their computer to sleep, re-awaken it, then call the code that unsubscribes from NetworkAvailabilityChanged event.
This deadlock is only specific to Windows implementation. For Unix implementations (Linux & OSX), their current implementation is to use a global lock object to protect both NetworkAddressChangedEventHandler & NetworkAvailabilityChangedEventHandler.
This issue was reported in internal bug 162830.
Commit migrated from https://github.com/dotnet/corefx/commit/
0628ddac3c88042c9a0e997350bd2346e68c5f07