2

Resolved

Race condition in NetworkModule

description

There is a race condition between the main thread and DCHPThread in NetworkModule.cs. Thus when starting with an ethernet cable connected, the isNetworkUp status is sometimes not correctly updated.

When executed within the Visual Studio debugger, the synchronisation instructions are executed in the following correct order and isNetworkUp is updated:
NetworkModule(): _isNetworkUp = false
DHCPThread(): _needDHCP.WaitOne()
UseDHCP(): _needDHCP.Set()
Program Started
DHCPThread(): _isNetworkUp = true
DHCPThread(): _needDHCP.WaitOne()
However without Visual Studio, the main thread notify the use of dhcp, but DHCPThread is not waiting yet and the event is missed:
NetworkModule(): _isNetworkUp = false
UseDHCP(): _needDHCP.Set()
Program Started
DHCPThread(): _needDHCP.WaitOne()
As a workaround, adding a sleep before the UseDHCP() ensures that the DHCPThread is allocated a time slice before UseDHCP():
void ProgramStarted()
{
    Thread.Sleep(1);
    ethernetForMountaineerEth.UseDHCP();

    ethernetForMountaineerEth.NetworkUp += ethernetForMountaineerEth_NetworkUp;
    ethernetForMountaineerEth.NetworkDown += ethernetForMountaineerEth_NetworkDown;

    Debug.Print("Program Started");
}
In attachment, NetworkModule.cs with some debug.print().

file attachments

comments