xamarin/Xamarin.Forms

[Bug] Device.Timer Continues Running When Exception Thrown in `callback`

Open

#9,146 opened on 2020年1月10日

GitHub で見る
 (2 comments) (0 reactions) (0 assignees)C# (5,644 stars) (1,926 forks)batch import
Corebreakinge/2 :clock2:good first issuehackathonhelp wantedinactivet/bug :bug:up-for-grabs

説明

Description

In Device.Timer, when an exception is thrown in Func<bool> callback, the timer continues running and will execute callback again as scheduled.

The timer continues to run because the code to disable the timer executes after checking the bool result of callback. If callback throws an exception, the code cannot check its result, the timer is never disposed or invalidated and it continues running.

Steps to Reproduce

Run the Unit Test below in Xamarin.Forms.Core.UnitTests. It fails, returning the following result:

Message: Expected: 1 But was: 2

[Test]
public async Task DeviceTimerWithException()
{
	const int timerDelayInSeconds = 1;
	const int expectedNumberOfExecutions = 1;
	int numberOfSuccessfulTaskExecutions = 0;

	Device.StartTimer(TimeSpan.FromSeconds(timerDelayInSeconds), functionThrowingException);

	//Wait for Timer to trigger twice
	await Task.Delay(TimeSpan.FromSeconds(timerDelayInSeconds * 2 + 1));

	Assert.AreEqual(expectedNumberOfExecutions, numberOfSuccessfulTaskExecutions);

	bool functionThrowingException()
	{
		numberOfSuccessfulTaskExecutions++;
		throw new Exception();
	}
}

Expected Behavior

Device.Timer should cease when an exception is thrown because it indicates that Func<bool> callback has failed

Actual Behavior

Device.Timer continues executing when an exception is thrown in Func<bool> callback.

Basic Information

  • Version with issue: Current
  • Platform Target Frameworks: Xamarin.Forms.Core

Screenshots

Reproduction Link

(Reproduction code above)

コントリビューターガイド