Skip to content

UnreferenceDisposable made slimmer #627

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 2 commits into from
Jul 18, 2025
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
8 changes: 5 additions & 3 deletions src/ModelContextProtocol.AspNetCore/HttpMcpSession.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,15 @@ internal sealed class HttpMcpSession<TTransport>(
public bool IsActive => !SessionClosed.IsCancellationRequested && _referenceCount > 0;
public long LastActivityTicks { get; private set; } = timeProvider.GetTimestamp();

private TimeProvider TimeProvider => timeProvider;

public IMcpServer? Server { get; set; }
public Task? ServerRunTask { get; set; }

public IDisposable AcquireReference()
{
Interlocked.Increment(ref _referenceCount);
return new UnreferenceDisposable(this, timeProvider);
return new UnreferenceDisposable(this);
}

public bool TryStartGetRequest() => Interlocked.Exchange(ref _getRequestStarted, 1) == 0;
Expand Down Expand Up @@ -70,13 +72,13 @@ public async ValueTask DisposeAsync()
public bool HasSameUserId(ClaimsPrincipal user)
=> UserIdClaim == StreamableHttpHandler.GetUserIdClaim(user);

private sealed class UnreferenceDisposable(HttpMcpSession<TTransport> session, TimeProvider timeProvider) : IDisposable
private sealed class UnreferenceDisposable(HttpMcpSession<TTransport> session) : IDisposable
{
public void Dispose()
{
if (Interlocked.Decrement(ref session._referenceCount) == 0)
{
session.LastActivityTicks = timeProvider.GetTimestamp();
session.LastActivityTicks = session.TimeProvider.GetTimestamp();
}
}
}
Expand Down
Loading