Fix System.Net.NetworkInformation.NetworkChange deadlock (dotnet/corefx#26259)
authorCaesar Chen <caesar1995@users.noreply.github.com>
Sat, 20 Jan 2018 18:29:06 +0000 (10:29 -0800)
committerKarel Zikmund <karelz@microsoft.com>
Sat, 20 Jan 2018 18:29:06 +0000 (10:29 -0800)
commit78e99f001dda6fd1aa3a35d44406b527554514cd
treef9f94bebb81e3a841c9013ca651ac3660edad892
parenta064cb185832285c01feaf8a9480982ba16ab7df
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
src/libraries/System.Net.NetworkInformation/src/System/Net/NetworkInformation/NetworkAddressChange.Windows.cs