Bug 32685

Summary: NRE in ServicePoint.CheckAvaliableForRecycling
Product: [Mono] Class Libraries Reporter: Tom Philpot <tom.philpot>
Component: SystemAssignee: Marek Safar <masafa>
Severity: normal CC: armin_willer, brendan.zagaeski, chrisntr, dominic, Kent.Green, kumpera, ltorje, masafa, mono-bugs+mono, mono-bugs+monotouch, owen.stewart, ted, todd.diehl
Priority: ---    
Version: master   
Target Milestone: Untriaged   
Hardware: Macintosh   
OS: Mac OS   
Tags: Is this bug a regression?: ---
Last known good build:

Description Tom Philpot 2015-07-31 18:12:10 UTC
We are seeing the following stack trace in a Xamarin.iOS app in Raygun.

NullReferenceException: Object reference not set to an instance of an object
 System.Net.ServicePoint.CheckAvailableForRecycling(System.DateTime& outIdleSince) in <filename unknown>:0
 System.Net.ServicePoint.IdleTimerCallback(System.Object obj) in <filename unknown>:0
 System.Threading.Timer+Scheduler.TimerCB(System.Object o) in <filename unknown>:0

We are using Xamarin.iOS and have seen the error reported on iOS 8 and 9.

I believe the error has to do with this commit: https://github.com/mono/mono/commit/a769e74c5320f2cfa85efe80f2e1c1d8470c271b

Specifically, https://github.com/mono/mono/blob/a769e74c5320f2cfa85efe80f2e1c1d8470c271b/mcs/class/System/System.Net/ServicePoint.cs#L317 might need a check for groups != null.

Possible repro:
1. Thread 1 calls CheckAvailableForRecycling
2. Thread 2 calls CheckAvailableForRecycling
3. Thread 1 locks (this) and builds groupList
4. Thread 2 locks (this) and builds groupList, while Thread 1 is iterating groupList (lines 304-310)
5. Thread 1 locks (this) on line 312, while Thread 2 is iterating groupList.
6. While holding lock, Thread 1 sets groups to null.
7. Thread 1 exits lock
8. Thread 2 locks (this) on line 312 and groups is null. => NRE
Comment 1 Tom Philpot 2015-07-31 18:43:22 UTC
See https://github.com/mono/mono/pull/1955
Comment 2 Marek Safar 2015-08-01 02:55:31 UTC
Fixed in mono master and Mono 4.2
Comment 3 Marek Safar 2015-08-02 07:26:23 UTC
*** Bug 32689 has been marked as a duplicate of this bug. ***
Comment 4 Marek Safar 2016-01-14 14:50:27 UTC
*** Bug 34745 has been marked as a duplicate of this bug. ***
Comment 5 Marek Safar 2016-01-14 14:50:36 UTC
*** Bug 35226 has been marked as a duplicate of this bug. ***
Comment 6 Marek Safar 2016-01-14 14:51:40 UTC
Only Xamarin.iOS 9.2 or newer versions have the fix included
Comment 7 Lucian Torje 2016-02-11 09:41:24 UTC
Not fixed: Xcode 7.2, mtouch (cycle6-xi: eb4c1ef), /Library/Frameworks/Mono.framework/Versions/4.2.1/lib/mono/4.5