Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

时间处理可能出现BUG #288

Open
hu372197979 opened this issue Sep 22, 2020 · 1 comment
Open

时间处理可能出现BUG #288

hu372197979 opened this issue Sep 22, 2020 · 1 comment

Comments

@hu372197979
Copy link

if (firstJob.NextRun <= Now || firstJob.PendingRunOnce)

此处验证时间不合适 可能造成正数循环中断,建议修改为

private static void ScheduleJobs()
{
	_timer.Change(Timeout.Infinite, Timeout.Infinite);
	_schedules.Sort();

	if (!_schedules.Any())
		return;

	
	var firstJob = _schedules.First();
	var _now = Now;
	if (firstJob.NextRun <= _now)
	{
		RunJob(firstJob);
		
		if (firstJob.CalculateNextRun == null)
		{
			// probably a ToRunNow().DelayFor() job, there's no CalculateNextRun
		}
		else
		{
			firstJob.NextRun = firstJob.CalculateNextRun(_now.AddMilliseconds(1));
		}

		if (firstJob.NextRun <= _now || firstJob.PendingRunOnce)
		{
			_schedules.Remove(firstJob);
		}

		firstJob.PendingRunOnce = false;
		ScheduleJobs();
		return;
	}

	var interval = firstJob.NextRun - _now;

	if (interval <= TimeSpan.Zero)
	{
		ScheduleJobs();
		return;
	}
	else
	{
		if (interval.TotalMilliseconds > _maxTimerInterval)
			interval = TimeSpan.FromMilliseconds(_maxTimerInterval);

		_timer.Change(interval, interval);
	}
}
@dersew
Copy link

dersew commented Apr 13, 2021

Yes, this is a dangerous bug

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants