Copilot commented on code in PR #3930:
URL: https://github.com/apache/thrift/pull/3930#discussion_r4108622276
##########
lib/netstd/Thrift/Transport/Client/THttpTransport.cs:
##########
@@ -123,21 +130,28 @@ public override Task OpenAsync(CancellationToken
cancellationToken)
public override void Close()
{
- if (_inputStream != null)
+ lock (_lifecycleLock)
{
- _inputStream.Dispose();
- _inputStream = null;
- }
-
- if (_outputStream != null)
- {
- _outputStream.Dispose();
- _outputStream = null;
- }
+ _lifecycleOpen = false;
+ var transports = new
List<THttpPerCallTransport>(_perCallTransports);
+ _perCallTransports.Clear();
+ foreach (var transport in transports)
+ {
+ transport.Dispose();
+ }
- if (_httpClient != null)
- {
- _httpClient.Dispose();
+ _inputStream?.Dispose();
+ _inputStream = null;
+ if (_flushInProgress == 0)
+ {
+ _outputStream?.Dispose();
+ _outputStream = null;
+ }
+ _response?.Dispose();
+ _response = null;
+ _responseReadTimeout?.Dispose();
+ _responseReadTimeout = null;
Review Comment:
Disposing the timeout source does not signal cancellation; it also removes
the scheduled `CancelAfter`. An in-flight `ReadAsync` is linked to this token,
so if the response stream does not abort pending reads when disposed (the test
`BlockingReadStream` is one such stream), `Close()` can turn a bounded read
into one that never completes. The active read token must be cancelled as part
of shutdown, with cancellation performed in a way that does not invoke
arbitrary callbacks while holding `_lifecycleLock`.
##########
lib/netstd/Thrift/Transport/Client/THttpPerCallTransport.cs:
##########
@@ -0,0 +1,276 @@
+// 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.Diagnostics;
+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)
+ {
+ lock (_parent.LifecycleLock)
+ {
+ if (_disposed || !_parent.IsLifecycleOpen)
+ {
+ throw new
TTransportException(TTransportException.ExceptionType.NotOpen,
+ "The transport has been closed.");
+ }
+ 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)
+ {
+ int connectTimeout;
+ var responseDeadline = Stopwatch.StartNew();
+ lock (_parent.LifecycleLock)
+ {
+ if (_disposed || !_parent.IsLifecycleOpen)
+ {
+ throw new
TTransportException(TTransportException.ExceptionType.NotOpen,
+ "The transport has been closed.");
+ }
+ if (_sent)
+ {
+ throw new InvalidOperationException("The request has
already been sent.");
+ }
+
+ // Once a send is attempted the server may have processed the
RPC, even if
+ // response acquisition fails. Require a fresh per-call
transport to retry.
+ _sent = true;
+ _request.Position = 0;
+ connectTimeout = _parent.ConnectTimeout;
+ }
+
+ var response = await _parent.SendAsync(_request,
cancellationToken);
+
+CancellationTokenSource responseReadTimeout = null;
+ Stream responseStream = null;
+ try
+ {
+ lock (_parent.LifecycleLock)
+ {
+ if (_disposed || !_parent.IsLifecycleOpen)
+ {
+ throw new
TTransportException(TTransportException.ExceptionType.NotOpen,
+ "The transport has been closed.");
+ }
+
+ responseReadTimeout = new CancellationTokenSource();
+ if (connectTimeout > 0)
+ {
+ var remainingTimeout = connectTimeout -
(int)responseDeadline.ElapsedMilliseconds;
+ responseReadTimeout.CancelAfter(Math.Max(0,
remainingTimeout));
+ }
+ }
+ using (var acquisitionCts =
CancellationTokenSource.CreateLinkedTokenSource(
+ cancellationToken, responseReadTimeout.Token))
+ {
+ responseStream = await THttpTransport.ReadAsStreamAsync(
+ response.Content, acquisitionCts.Token);
+ }
+ if (responseStream.CanSeek)
+ {
+ responseStream.Seek(0, SeekOrigin.Begin);
+ }
+
+ // Only take ownership of the new response/timeout/stream once
stream acquisition
+ // and normalization have succeeded, so a failed FlushAsync
leaves no dangling
+ // resources behind and a subsequent retry does not leak the
previous response.
+ lock (_parent.LifecycleLock)
+ {
+ if (_disposed || !_parent.IsLifecycleOpen)
+ {
+ throw new
TTransportException(TTransportException.ExceptionType.NotOpen,
+ "The transport has been closed.");
+ }
+
+ _response?.Dispose();
+ _response = response;
+ response = null;
+ _responseReadTimeout?.Dispose();
+ _responseReadTimeout = responseReadTimeout;
+ responseReadTimeout = null;
+ _responseStream = responseStream;
+ responseStream = null;
+ }
+ }
+ catch (TTransportException)
+ {
+ throw;
+ }
+ catch (IOException iox)
+ {
+ throw new
TTransportException(TTransportException.ExceptionType.Unknown, iox.ToString(),
iox);
+ }
+ catch (HttpRequestException wx)
+ {
+ throw new
TTransportException(TTransportException.ExceptionType.Unknown,
+ "Couldn't connect to server: " + wx, wx);
+ }
+ catch (OperationCanceledException ocx)
+ {
+ throw new
TTransportException(TTransportException.ExceptionType.Interrupted, ocx.Message,
ocx);
+ }
+ catch (Exception ex)
+ {
+ throw new
TTransportException(TTransportException.ExceptionType.Unknown, ex.Message, ex);
+ }
+ finally
+ {
+ responseStream?.Dispose();
+ response?.Dispose();
+ responseReadTimeout?.Dispose();
+ }
+ }
+
+ public override async ValueTask<int> ReadAsync(
+ byte[] buffer,
+ int offset,
+ int length,
+ CancellationToken cancellationToken)
+ {
+ if (!_sent)
+ {
+ throw new TTransportException(
TTransportException.ExceptionType.NotOpen, "The request has not been sent.");
+ }
+
+ CheckReadBytesAvailable(length);
+
+ CancellationTokenSource readCts = null;
+ Stream responseStream;
+ bool hasResponseReadTimeout;
+ lock (_parent.LifecycleLock)
+ {
+ if (_disposed || !_parent.IsLifecycleOpen || _responseStream
== null)
+ {
+ throw new
TTransportException(TTransportException.ExceptionType.NotOpen,
+ "The transport has been closed.");
+ }
+
+ responseStream = _responseStream;
+ hasResponseReadTimeout = _responseReadTimeout != null;
+ if (hasResponseReadTimeout)
+ {
+ readCts = CancellationTokenSource.CreateLinkedTokenSource(
+ cancellationToken,
+ _responseReadTimeout.Token);
+ }
+ }
+
+ var readToken = readCts?.Token ?? cancellationToken;
+ try
+ {
+ var count = await responseStream.ReadAsync(
+ buffer,
+ offset,
+ length,
+ readToken);
+
+ CountConsumedMessageBytes(count);
+ return count;
+ }
+ catch (OperationCanceledException ocx) when
(hasResponseReadTimeout && !cancellationToken.IsCancellationRequested)
+ {
+ throw new
TTransportException(TTransportException.ExceptionType.Interrupted, ocx.Message,
ocx);
+ }
+ catch (ObjectDisposedException odx)
+ {
+ throw new
TTransportException(TTransportException.ExceptionType.NotOpen,
+ "The transport has been closed.", odx);
+ }
+ catch (IOException iox)
+ {
+ throw new
TTransportException(TTransportException.ExceptionType.Unknown, iox.ToString(),
iox);
+ }
+ finally
+ {
+ readCts?.Dispose();
+ }
+ }
+
+ protected override void Dispose(bool disposing)
+ {
+ lock (_parent.LifecycleLock)
+ {
+ if (_disposed)
+ {
+ return;
+ }
+
+ if (disposing)
+ {
+ _response?.Dispose();
+ _response = null;
+ _responseReadTimeout?.Dispose();
+ _responseReadTimeout = null;
Review Comment:
Disposing a per-call transport drops the scheduled response timeout without
cancelling its token. If a response stream does not terminate an
already-running read on `Dispose`, that read remains pending indefinitely after
the call is disposed. Cancel the active read token during disposal, but do so
outside `_parent.LifecycleLock` to avoid running stream/token callbacks under
the shared lifecycle lock.
--
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]