meebey / SmartIrc4net

IRC C# Library
http://www.meebey.net/projects/smartirc4net/
Other
126 stars 52 forks source link

Fix race condition between starting PING stopwatch and receiving PONG #44

Closed RavuAlHemio closed 8 years ago

RavuAlHemio commented 8 years ago

Looks like my previous PR introduced a race condition that I then observed in the wild.

If the server responds with a PONG message quickly enough, the stopwatch swap-out performed after sending the PING message:

_Connection.WriteLine(Rfc2812.Ping(_Connection.Address), Priority.Critical);
_Connection.NextPingStopwatch.Stop();
_Connection.PingStopwatch.Reset();
_Connection.PingStopwatch.Start();

may come into a race with the stopwatch swap-out performed after receiving a PONG message:

switch (command) {
    // ...
    case "PONG":
        PingStopwatch.Stop();
        NextPingStopwatch.Reset();
        NextPingStopwatch.Start();
        // ...
        break;
}

Avoid this by moving the call sending the PING message until after the swap-out (top block).