Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
JamesNK committed Oct 13, 2023
1 parent 7fe04cb commit 0af3b5b
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
using System.IO.Pipelines;
using Grpc.Core;
using Grpc.Shared;
using Microsoft.AspNetCore.Http.Features;

namespace Grpc.AspNetCore.Server.Internal;

Expand All @@ -30,7 +31,7 @@ internal class HttpContextStreamReader<TRequest> : IAsyncStreamReader<TRequest>
private readonly HttpContextServerCallContext _serverCallContext;
private readonly Func<DeserializationContext, TRequest> _deserializer;
private readonly PipeReader _bodyReader;
private readonly CancellationToken _cancellationToken;
private readonly IHttpRequestLifetimeFeature _requestLifetimeFeature;
private bool _completed;
private long _readCount;
private bool _endOfStream;
Expand All @@ -43,7 +44,8 @@ public HttpContextStreamReader(HttpContextServerCallContext serverCallContext, F
// Copy HttpContext values.
// This is done to avoid a race condition when reading them from HttpContext later when running in a separate thread.
_bodyReader = _serverCallContext.HttpContext.Request.BodyReader;
_cancellationToken = _serverCallContext.HttpContext.RequestAborted;
// Copy lifetime feature because HttpContext.RequestAborted on .NET 6 doesn't return the real cancellation token.
_requestLifetimeFeature = _serverCallContext.HttpContext.Features.Get<IHttpRequestLifetimeFeature>()!;
}

public TRequest Current { get; private set; } = default!;
Expand All @@ -62,7 +64,7 @@ async Task<bool> MoveNextAsync(ValueTask<TRequest?> readStreamTask)
return Task.FromCanceled<bool>(cancellationToken);
}

if (_completed || _cancellationToken.IsCancellationRequested)
if (_completed || _requestLifetimeFeature.RequestAborted.IsCancellationRequested)
{
return Task.FromException<bool>(new InvalidOperationException("Can't read messages after the request is complete."));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
using System.IO.Pipelines;
using Grpc.Core;
using Grpc.Shared;
using Microsoft.AspNetCore.Http.Features;

namespace Grpc.AspNetCore.Server.Internal;

Expand All @@ -31,7 +32,7 @@ internal class HttpContextStreamWriter<TResponse> : IServerStreamWriter<TRespons
private readonly HttpContextServerCallContext _context;
private readonly Action<TResponse, SerializationContext> _serializer;
private readonly PipeWriter _bodyWriter;
private readonly CancellationToken _cancellationToken;
private readonly IHttpRequestLifetimeFeature _requestLifetimeFeature;
private readonly object _writeLock;
private Task? _writeTask;
private bool _completed;
Expand All @@ -46,7 +47,8 @@ public HttpContextStreamWriter(HttpContextServerCallContext context, Action<TRes
// Copy HttpContext values.
// This is done to avoid a race condition when reading them from HttpContext later when running in a separate thread.
_bodyWriter = context.HttpContext.Response.BodyWriter;
_cancellationToken = context.HttpContext.RequestAborted;
// Copy lifetime feature because HttpContext.RequestAborted on .NET 6 doesn't return the real cancellation token.
_requestLifetimeFeature = context.HttpContext.Features.Get<IHttpRequestLifetimeFeature>()!;
}

public WriteOptions? WriteOptions
Expand Down Expand Up @@ -85,7 +87,7 @@ private async Task WriteCoreAsync(TResponse message, CancellationToken cancellat
{
cancellationToken.ThrowIfCancellationRequested();

if (_completed || _cancellationToken.IsCancellationRequested)
if (_completed || _requestLifetimeFeature.RequestAborted.IsCancellationRequested)
{
throw new InvalidOperationException("Can't write the message because the request is complete.");
}
Expand Down

0 comments on commit 0af3b5b

Please sign in to comment.