8
8
using System . Runtime . CompilerServices ;
9
9
using System . Text ;
10
10
using System . Text . Json ;
11
- using System . Threading ;
12
11
using Microsoft . AspNetCore . Connections ;
13
12
using Microsoft . AspNetCore . Server . Kestrel . Core . Internal . Http ;
14
13
@@ -39,7 +38,7 @@ internal sealed class KestrelEventSource : EventSource
39
38
private long _httpRequestQueueLength ;
40
39
private long _currentUpgradedHttpRequests ;
41
40
42
- private readonly List < KestrelServerOptions > _options = new ( ) ;
41
+ private readonly List < WeakReference < KestrelServerOptions > > _options = new ( ) ;
43
42
44
43
private KestrelEventSource ( )
45
44
{
@@ -224,7 +223,7 @@ public void TlsHandshakeFailed(string connectionId)
224
223
public void Configuration ( int instanceId , string configuration )
225
224
{
226
225
// If the event source is already enabled, dump configuration
227
- WriteEvent ( 11 , instanceId , configuration ) ;
226
+ WriteEvent ( 11 , instanceId , configuration ) ;
228
227
}
229
228
230
229
[ NonEvent ]
@@ -252,7 +251,7 @@ public void AddServerOptions(KestrelServerOptions options)
252
251
{
253
252
lock ( _options )
254
253
{
255
- _options . Add ( options ) ;
254
+ _options . Add ( new ( options ) ) ;
256
255
}
257
256
258
257
Configuration ( options ) ;
@@ -263,7 +262,14 @@ public void RemoveServerOptions(KestrelServerOptions options)
263
262
{
264
263
lock ( _options )
265
264
{
266
- _options . Remove ( options ) ;
265
+ for ( var i = _options . Count - 1 ; i >= 0 ; i -- )
266
+ {
267
+ var weakReference = _options [ i ] ;
268
+ if ( ! weakReference . TryGetTarget ( out var target ) || ReferenceEquals ( target , options ) )
269
+ {
270
+ _options . RemoveAt ( i ) ;
271
+ }
272
+ }
267
273
}
268
274
}
269
275
@@ -353,9 +359,18 @@ protected override void OnEventCommand(EventCommandEventArgs command)
353
359
// Log the options here
354
360
lock ( _options )
355
361
{
356
- foreach ( var option in _options )
362
+ for ( var i = _options . Count - 1 ; i >= 0 ; i -- )
357
363
{
358
- Configuration ( option ) ;
364
+ var weakReference = _options [ i ] ;
365
+ if ( ! weakReference . TryGetTarget ( out var target ) )
366
+ {
367
+ // Remove any options that have been collected
368
+ _options . RemoveAt ( i ) ;
369
+ }
370
+ else
371
+ {
372
+ Configuration ( target ) ;
373
+ }
359
374
}
360
375
}
361
376
}
0 commit comments