Bug 35581 - Empty NSIndexSet.ToList freezes the app execution
Summary: Empty NSIndexSet.ToList freezes the app execution
Alias: None
Product: Xamarin.Mac
Classification: Desktop
Component: Library (Xamarin.Mac.dll) ()
Version: 2.0.2
Hardware: Macintosh Mac OS
: --- normal
Target Milestone: ---
Assignee: Chris Hamons
Depends on:
Reported: 2015-11-05 11:28 UTC by Norbert Virth
Modified: 2015-11-06 03:14 UTC (History)
1 user (show)

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

Sample project showing the issue (55.05 KB, application/zip)
2015-11-05 11:28 UTC, Norbert Virth

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:

Description Norbert Virth 2015-11-05 11:28:38 UTC
Created attachment 13688 [details]
Sample project showing the issue

There event won't be an exception or crash by me; it just stops the app. (Though I've earlier written an extension method for this named ToListSafe, which made an isEmpty-check before calling ToList on it, so I could swear once there was an exception there...)

Attached the Xamarin.Mac.Samples/MacTables project forked with these 2 rows:

var emptyIndexSet = ProductTable.SelectedRows;
var smg = emptyIndexSet.ToList(); // MainWindow.cs:39

If you run the project, the app window won't appear. You can see that execution stops at row 39 when you click the "pause" (||) button in Xamarin Studio; or just put a breakpoint on that row; you can't step over it. It just freezes the app exectuion
Comment 1 Chris Hamons 2015-11-05 17:21:11 UTC
The following is all that is needed to reproduce:

		static void Main (string[] args)
			NSApplication.Init ();
			var emptyIndexSet = new NSIndexSet ();
			Console.WriteLine ("Before");
			var smg = emptyIndexSet.ToList();
			Console.WriteLine ("Out");
Comment 2 Chris Hamons 2015-11-05 17:50:21 UTC
So under the hood, this is because FirstIndex and LastIndex are giving us bogus data and we're looping forever looking for the end of the list. 

Fixed in master / 777d171b95d9a255c7c9ccfcbe1c86576fc6002f

This won't make it until C7, but testing against Count == 0 before calling ToList is the best workaround.
Comment 3 Chris Hamons 2015-11-05 17:50:44 UTC
Thanks for the report with repro case. It made fixing it easy!
Comment 4 Norbert Virth 2015-11-06 03:14:26 UTC
If someone would need a fast and universal workaround until the fix, I use this extension method everywhere where touching NSIndexSet with LinQ:

public static IEnumerable<nuint> EnumerateSafe(this NSIndexSet nsIndexSet)
	if(nsIndexSet == null || nsIndexSet.Count == 0)
		yield break;

	foreach(var nuint in nsIndexSet)
		yield return nuint;