Skip to content

Use a weak reference list to store server options #35098

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 6, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
using System.Runtime.CompilerServices;
using System.Text;
using System.Text.Json;
using System.Threading;
using Microsoft.AspNetCore.Connections;
using Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http;

Expand Down Expand Up @@ -39,7 +38,7 @@ internal sealed class KestrelEventSource : EventSource
private long _httpRequestQueueLength;
private long _currentUpgradedHttpRequests;

private readonly List<KestrelServerOptions> _options = new();
private readonly List<WeakReference<KestrelServerOptions>> _options = new();

private KestrelEventSource()
{
Expand Down Expand Up @@ -224,7 +223,7 @@ public void TlsHandshakeFailed(string connectionId)
public void Configuration(int instanceId, string configuration)
{
// If the event source is already enabled, dump configuration
WriteEvent(11, instanceId, configuration);
WriteEvent(11, instanceId, configuration);
}

[NonEvent]
Expand Down Expand Up @@ -252,7 +251,7 @@ public void AddServerOptions(KestrelServerOptions options)
{
lock (_options)
{
_options.Add(options);
_options.Add(new(options));
}

Configuration(options);
Expand All @@ -263,7 +262,14 @@ public void RemoveServerOptions(KestrelServerOptions options)
{
lock (_options)
{
_options.Remove(options);
for (var i = _options.Count - 1; i >= 0; i--)
{
var weakReference = _options[i];
if (!weakReference.TryGetTarget(out var target) || ReferenceEquals(target, options))
{
_options.RemoveAt(i);
}
}
}
}

Expand Down Expand Up @@ -353,9 +359,18 @@ protected override void OnEventCommand(EventCommandEventArgs command)
// Log the options here
lock (_options)
{
foreach (var option in _options)
for (var i = _options.Count - 1; i >= 0; i--)
{
Configuration(option);
var weakReference = _options[i];
if (!weakReference.TryGetTarget(out var target))
{
// Remove any options that have been collected
_options.RemoveAt(i);
}
else
{
Configuration(target);
}
}
}
}
Expand Down