@@ -62,7 +62,13 @@ static TestBase()
62
62
#if NETCOREAPP1_0
63
63
if ( IgnorableExceptionPredicates . Any ( predicate => predicate ( args . Exception . InnerException ) ) ) return ;
64
64
#endif
65
- Interlocked . Increment ( ref sharedFailCount ) ;
65
+ lock ( sharedFailCount )
66
+ {
67
+ if ( sharedFailCount != null )
68
+ {
69
+ sharedFailCount . Value ++ ;
70
+ }
71
+ }
66
72
lock ( backgroundExceptions )
67
73
{
68
74
backgroundExceptions . Add ( args . Exception . ToString ( ) ) ;
@@ -97,7 +103,7 @@ protected void OnInternalError(object sender, InternalErrorEventArgs e)
97
103
}
98
104
99
105
private int privateFailCount ;
100
- private static int sharedFailCount ;
106
+ private static AsyncLocal < int > sharedFailCount = new AsyncLocal < int > ( ) ;
101
107
private volatile int expectedFailCount ;
102
108
103
109
private static readonly List < string > privateExceptions = new List < string > ( ) ;
@@ -107,7 +113,10 @@ public void ClearAmbientFailures()
107
113
{
108
114
Collect ( ) ;
109
115
Interlocked . Exchange ( ref privateFailCount , 0 ) ;
110
- Interlocked . Exchange ( ref sharedFailCount , 0 ) ;
116
+ lock ( sharedFailCount )
117
+ {
118
+ sharedFailCount . Value = 0 ;
119
+ }
111
120
expectedFailCount = 0 ;
112
121
lock ( privateExceptions )
113
122
{
@@ -136,7 +145,13 @@ private static void Collect()
136
145
public void Teardown ( )
137
146
{
138
147
Collect ( ) ;
139
- if ( expectedFailCount >= 0 && ( Interlocked . CompareExchange ( ref sharedFailCount , 0 , 0 ) + privateFailCount ) != expectedFailCount )
148
+ int sharedFails ;
149
+ lock ( sharedFailCount )
150
+ {
151
+ sharedFails = sharedFailCount . Value ;
152
+ sharedFailCount . Value = 0 ;
153
+ }
154
+ if ( expectedFailCount >= 0 && ( sharedFails + privateFailCount ) != expectedFailCount )
140
155
{
141
156
lock ( privateExceptions )
142
157
{
0 commit comments