Bug 49681 - Access dictionary entry with key after checking it DOESN'T contain the key
Summary: Access dictionary entry with key after checking it DOESN'T contain the key
Status: RESOLVED FIXED
Alias: None
Product: Forms
Classification: Xamarin
Component: Forms ()
Version: unspecified
Hardware: PC Windows
: Normal normal
Target Milestone: ---
Assignee: Chris King
URL:
Depends on:
Blocks:
 
Reported: 2016-12-12 15:18 UTC by François M
Modified: 2017-06-19 23:13 UTC (History)
4 users (show)

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

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 FIXED

Description François M 2016-12-12 15:18:59 UTC
I have a crash due to uncaught exception on that line:
https://github.com/xamarin/Xamarin.Forms/blob/master/Xamarin.Forms.Core/MessagingCenter.cs#L112

I don't understand the logic here. You check that the dictionary contains a key and if it doesn't you access the dictionary entry for that key?

//ERROR MSG
Terminating app due to uncaught exception 'System.AggregateException', reason: 'System.AggregateException: A Task's exception(s) were not observed either by Waiting on the Task or accessing its Exception property. As a result, the unobserved exception was rethrown by the finalizer thread. ---> System.IndexOutOfRangeException: Index was outside the bounds of the array. at System.Collections.Generic.Dictionary`2[TKey,TValue].Insert (TKey key, TValue value, System.Boolean add) <0x1003190a0 + 0x002c8> in <15e850188d9f425bbeae90f0bbc51e17#b5a87f601e65028d7a7238eeb2863873>:0 at System.Collections.Generic.Dictionary`2[TKey,TValue].set_Item (TKey key, TValue value) <0x100318150 + 0x00027> in <15e850188d9f425bbeae90f0bbc51e17#b5a87f601e65028d7a7238eeb2863873>:0 at Xamarin.Forms.MessagingCenter.InnerSubscribe (System.Object subscriber, System.String message, System.Type senderType, System.Type argType, System.Action`2[T1,T2] callback) <0x100c6db30 + 0x00177> in <b0fc14d4e5b04749b7241d1235a68329#b5a87f601e65028d7a7238eeb2863873>:0 at Xamarin.Forms.MessagingCenter.Subscribe[TSender] (System.Object subscriber, System.String message, System.Action`1[T] callback, TSender source) <0x100c6d6d0 + 0x00127>
Comment 1 François M 2016-12-12 15:21:15 UTC
I guess it's a typo and what you wanted to do is:
s_callbacks.Add(key, list);
Comment 2 adrianknight89 2016-12-12 16:49:08 UTC
At first sight, that looks like an issue. Do you have a minimal repro?
Comment 3 adrianknight89 2016-12-12 16:58:27 UTC
Actually, looks like they are the same things with different impacts. See http://stackoverflow.com/a/1838494. Your issue might be different, so a repro will help.
Comment 4 François M 2016-12-12 17:08:05 UTC
Thanks, I learnt something today. I didn't know you could do dictionary[key] = value. Until now I thought Add was mandatory.
I was wondering how the code could even work as there was no s_callback.Add anywhere within the class.

Unfortunately I don't have a repro. I just got that report from HockeyApp for an app in prod. I can't reproduce the issue on my side yet.
The line throwing exception may actually even be 
https://github.com/xamarin/Xamarin.Forms/blob/1f3d5ecd260c836563b34efe2be3f8e287582eaf/Xamarin.Forms.Core/MessagingCenter.cs#L107

I'll dig further and revert here when I get more.
Tks.
Comment 6 François M 2016-12-12 17:17:35 UTC
It's a threading issue apparently:
http://stackoverflow.com/questions/15095817/adding-to-a-generic-dictionary-causes-indexoutofrangeexception

I'll make my Subscribe calls thread-safe.
Comment 7 adrianknight89 2016-12-12 21:19:09 UTC
That raises an interesting question. I do wonder if we should use ConcurrentDictionary in the implementation of MessagingCenter.
Comment 8 adrianknight89 2016-12-12 21:21:45 UTC
I redirected this to Jason. 

See https://github.com/xamarin/Xamarin.Forms/pull/541.
Comment 9 Chris King 2017-02-16 21:19:07 UTC
Marking as need info as there is no repo and another issue has been created to further track this issue.
Comment 10 Samantha Houts [MSFT] 2017-06-19 23:13:34 UTC
Thank you for taking the time to submit this report. After reviewing the description of this bug, we believe it no longer affects the current version of Xamarin.Forms, since we've made some significant changes to the MessageCenter. If you are still experiencing the issue after updating your packages, please reopen this report with an attached reproduction. 
 
Here are some reproduction best practices: https://gist.github.com/jassmith/92405c300e54a01dcc6d