DotNetty icon indicating copy to clipboard operation
DotNetty copied to clipboard

Environment.TickCount wrap around problem in IdleStateHandler

Open leisurelyclouds opened this issue 2 years ago • 0 comments

https://docs.microsoft.com/en-us/dotnet/api/system.environment.tickcount?view=netframework-4.7.2#remarks

Environment.TickCount wrap around problem may occur if server startup time is around 24.9 days So IdleStateHandler won't trigger idle event

Example:

static void HandleReadTimeout(IdleStateHandler self, IChannelHandlerContext context)
{
    TimeSpan nextDelay = self.readerIdleTime;

    if (!self.reading)
    {
        // TimeSpan.FromSeconds(30) - (TimeSpan.FromMilliseconds(-2128993450)-TimeSpan.FromMilliseconds(2147461190)); 
        nextDelay -= self.Ticks() - self.lastReadTime;
    }

    // nextDelay is 49.11:54:44.6400000
    if (nextDelay.Ticks <= 0)
    {
        // Reader is idle - set a new timeout and notify the callback.
        self.readerIdleTimeout = 
            self.Schedule(context, ReadTimeoutAction, self, context, 
                self.readerIdleTime);

        bool first = self.firstReaderIdleEvent;
        self.firstReaderIdleEvent = false;

        try
        {
            IdleStateEvent stateEvent = self.NewIdleStateEvent(IdleState.ReaderIdle, first);
            self.ChannelIdle(context, stateEvent);
        }
        catch (Exception ex)
        {
            context.FireExceptionCaught(ex);
        }
    }
    else
    {
        // will shedule ReadTimeoutAction after 49.11:54:44.6400000
        self.readerIdleTimeout = self.Schedule(context, ReadTimeoutAction, self, context, 
            nextDelay);
    }
}

leisurelyclouds avatar Mar 30 '22 02:03 leisurelyclouds