xamarin/Xamarin.Forms

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

オープン

#9,146 opened on 2020/01/10

 (2 件のコメント) (0 件のリアクション) (0 人の担当者)C# (1,926 件のフォーク)batch import
Corebreakinge/2 :clock2:good first issuehackathonhelp wantedinactivet/bug :bug:up-for-grabs

Repository metrics

Stars
 (5,644 個のスター)
PR merge metrics
 (30d に merged PR はありません)

説明

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)

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