Copilot commented on code in PR #3930:
URL: https://github.com/apache/thrift/pull/3930#discussion_r4107171398
##########
lib/netstd/Thrift/Transport/Client/THttpTransport.cs:
##########
@@ -166,10 +200,23 @@ public override async ValueTask<int> ReadAsync(byte[]
buffer, int offset, int le
CountConsumedMessageBytes(ret);
return ret;
}
+ catch (OperationCanceledException ocx) when
(hasResponseReadTimeout)
+ {
+ throw new
TTransportException(TTransportException.ExceptionType.Interrupted, ocx.Message,
ocx);
+ }
Review Comment:
This filter also matches cancellation requested by the current `ReadAsync`
caller because `readCts` links both tokens. Consequently ordinary caller
cancellation is changed from `OperationCanceledException` into
`TTransportException`, unlike the previous implementation. Only translate
cancellation when the response timeout fired; let cancellation of
`cancellationToken` propagate normally.
This issue also appears on line 324 of the same file.
##########
lib/netstd/Thrift/Transport/Client/THttpTransport.cs:
##########
@@ -278,25 +377,237 @@ public override async Task FlushAsync(CancellationToken
cancellationToken)
}
finally
{
- _outputStream = new MemoryStream();
- ResetMessageSizeAndConsumedBytes();
+ inputStream?.Dispose();
+ response?.Dispose();
+ responseReadTimeout?.Dispose();
+ request?.Dispose();
+ if (request == null)
+ {
+ contentStream?.Dispose();
+ }
+
+ lock (_lifecycleLock)
+ {
+ _flushInProgress--;
+ if (_lifecycleOpen)
+ {
+ _outputStream = new MemoryStream();
+ }
+ else
+ {
+ outputStream.Dispose();
+ _outputStream = null;
+ }
+ ResetMessageSizeAndConsumedBytes();
+ }
}
}
// IDisposable
protected override void Dispose(bool disposing)
{
- if (!_isDisposed)
+ if (_isDisposed)
+ {
+ return;
+ }
+
+ if (disposing)
+ {
+ Close();
+ }
+ _isDisposed = true;
+ }
+
+ public ValueTask<TTransport>
CreatePerCallTransportAsync(CancellationToken cancellationToken = default)
+ {
+ cancellationToken.ThrowIfCancellationRequested();
+ lock (_lifecycleLock)
{
- if (disposing)
+ if (!_lifecycleOpen || _httpClient == null)
{
- _inputStream?.Dispose();
- _outputStream?.Dispose();
- _httpClient?.Dispose();
+ throw new
TTransportException(TTransportException.ExceptionType.NotOpen,
+ "The transport has been closed.");
}
+
+ var transport = new THttpPerCallTransport(this);
+ _perCallTransports.Add(transport);
+ return new ValueTask<TTransport>(transport);
+ }
+ }
+
+ internal object LifecycleLock => _lifecycleLock;
+
+ internal bool IsLifecycleOpen
+ {
+ get
+ {
+ lock (_lifecycleLock)
+ {
+ return _lifecycleOpen && _httpClient != null;
+ }
+ }
+ }
+
+ internal void RemovePerCallTransport(THttpPerCallTransport transport)
+ {
+ lock (_lifecycleLock)
+ {
+ _perCallTransports.Remove(transport);
+ }
+ }
+
+ internal static async Task<Stream> ReadAsStreamAsync(HttpContent
content, CancellationToken cancellationToken)
+ {
+#if NET5_0_OR_GREATER
+ return await content.ReadAsStreamAsync(cancellationToken);
+#else
+ var readTask = content.ReadAsStreamAsync();
+ if (readTask.IsCompleted || !cancellationToken.CanBeCanceled)
+ {
+ return await readTask;
+ }
+
+ // Observe a later failure if cancellation wins the race and the
response is disposed.
+ readTask.ContinueWith(
+ task => { var ignored = task.Exception; },
+ CancellationToken.None,
+ TaskContinuationOptions.OnlyOnFaulted |
TaskContinuationOptions.ExecuteSynchronously,
+ TaskScheduler.Default);
+
+ var cancellationTask = new TaskCompletionSource<bool>();
+ using (cancellationToken.Register(() =>
cancellationTask.TrySetResult(true)))
+ {
+ if (await Task.WhenAny(readTask, cancellationTask.Task) !=
readTask)
+ {
+ throw new OperationCanceledException(cancellationToken);
+ }
Review Comment:
On netstandard, if cancellation wins, `readTask` is abandoned and only a
later fault is observed. A custom `HttpContent` can subsequently complete
successfully with a stream, and that stream has no owner left to dispose it
(disposing the response while the task is pending does not dispose a future
result). Attach a success continuation that disposes `task.Result` before
throwing cancellation.
##########
lib/netstd/Thrift/Transport/Client/THttpPerCallTransport.cs:
##########
@@ -0,0 +1,242 @@
+// Licensed to the Apache Software Foundation(ASF) under one
+// or more contributor license agreements.See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License. You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied. See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+using System;
+using System.IO;
+using System.Net.Http;
+using System.Threading;
+using System.Threading.Tasks;
+
+namespace Thrift.Transport.Client
+{
+ /// <summary>
+ /// Implementation of a per-call transport for THttpTransport.
+ /// Each call to the server will create a new HTTP request and response.
+ /// </summary>
+ internal class THttpPerCallTransport : TEndpointTransport
+ {
+ private readonly THttpTransport _parent;
+ private readonly MemoryStream _request = new MemoryStream();
+
+ private HttpResponseMessage _response;
+ private Stream _responseStream;
+ private CancellationTokenSource _responseReadTimeout;
+ private bool _sent;
+ private bool _disposed;
+
+ public THttpPerCallTransport(THttpTransport parent) :
base(parent.Configuration)
+ {
+ this._parent = parent;
+ }
+
+ public override bool IsOpen => !_disposed;
+
+ public override Task WriteAsync(
+ byte[] buffer,
+ int offset,
+ int length,
+ CancellationToken cancellationToken)
+ {
+ if (_sent)
+ {
+ throw new InvalidOperationException("The request has already
been sent.");
+ }
+
+ return _request.WriteAsync(
+ buffer,
+ offset,
+ length,
+ cancellationToken);
+ }
+
+ public override async Task FlushAsync(
+ CancellationToken cancellationToken)
+ {
+ if (_sent)
+ {
+ throw new InvalidOperationException("The request has already
been sent.");
+ }
+
+ _request.Position = 0;
+ var response = await _parent.SendAsync(_request,
cancellationToken);
Review Comment:
`_sent` is not set until after the response stream is initialized, so any
failure after the HTTP request may have reached the server leaves this
transport retryable. Retrying `FlushAsync` can execute a non-idempotent Thrift
method twice; the new retry test explicitly exercises this unsafe behavior.
Mark the call as sent before initiating the HTTP send and require a new
per-call transport after any send attempt.
This issue also appears in the following locations of the same file:
- line 77
- line 189
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]