Bug 57820 - System.Threading.Tasks.TaskCanceledException: A task was canceled. While update the BindingContaxt at runtime in Xamarin Forms iOS.
Summary: System.Threading.Tasks.TaskCanceledException: A task was canceled. While upda...
Status: RESOLVED ANSWERED
Alias: None
Product: Forms
Classification: Xamarin
Component: Forms ()
Version: 2.3.4
Hardware: PC Windows
: --- normal
Target Milestone: ---
Assignee: Jimmy [MSFT]
URL:
Depends on:
Blocks:
 
Reported: 2017-06-28 07:24 UTC by Ganesan VG
Modified: 2017-07-05 10:38 UTC (History)
3 users (show)

Tags:
Is this bug a regression?: ---
Last known good build:


Attachments
Sample (454.21 KB, application/x-zip-compressed)
2017-06-28 07:24 UTC, Ganesan VG
Details


Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.


Please create a new report on Developer Community or GitHub with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:
Status:
RESOLVED ANSWERED

Description Ganesan VG 2017-06-28 07:24:39 UTC
Created attachment 23163 [details]
Sample

I am facing "System.Threading.Tasks.TaskCanceledException: A task was canceled." exception while update the BindingContaxt at runtime in Xamarin Forms iOS. I have ContentView with image as content of it. I have updating the BindingContaxt at runtime. I have attached the sample here.

Replication procedure,

1. Enable the Common Language Runtime Exceptions setting.
2. Run the attached sample.
3. Update the BindingContaxt by clicking the button "Update BindingContaxt"
4. Now the crash will be occurred.


Configuration Details:

1. Visual Studio Professional 2015 with Update 2.
2. iPod device(OS - 10.1.1(14B100), Model - MKH62BT/A)
Comment 1 Ganesan VG 2017-06-28 10:56:54 UTC
Hi Team,

The above reported issue reproduced in Xamarin Forms UWP also. Can you please check and revert us.
Comment 2 Jimmy [MSFT] 2017-06-29 16:05:15 UTC
I ran the project provided in Visual Studio with all CLR Exceptions enabled and I was able to see the TaskCanceledException get thrown. However I was also able to continue the app's execution and observe that it did not crash. Likewise, debugging the app with the default CLR exceptions enabled did not cause the app to crash.

According to the stack trace the exception is being thrown here[1] due to the CancellationTokenSource. Looking at your Button click handling code, the BindingContext of the ContentView changes several times in the foreach loop. Since the Image Source loads a new stream each time the BindingContext changes, it cancels any previous operation and throws the exception.

You can prevent this by avoiding rapid changes to the Image Source. Regardless, we do catch the exception in this case as well so this shouldn't crash the app even if it does occur. Hope this helps!

[1] https://github.com/xamarin/Xamarin.Forms/blob/2.3.4-2/Xamarin.Forms.Core/StreamImageSource.cs#L36