Copilot commented on code in PR #3930: URL: https://github.com/apache/thrift/pull/3930#discussion_r4077668828
########## lib/netstd/Thrift/Transport/Client/THttpPerCallTransport.cs: ########## @@ -0,0 +1,145 @@ +// 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; + _response = await _parent.SendAsync(_request, cancellationToken); + + _responseReadTimeout?.Dispose(); + _responseReadTimeout = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken); + if (_parent.ConnectTimeout > 0) + { + _responseReadTimeout.CancelAfter(_parent.ConnectTimeout); + } + +#if NET5_0_OR_GREATER + _responseStream = await _response.Content.ReadAsStreamAsync(cancellationToken); +#else + _responseStream = await _response.Content.ReadAsStreamAsync(); +#endif + _sent = true; Review Comment: If response stream initialization fails after `SendAsync` has returned (for example, cancellation after the headers arrive), `_response` and `_responseReadTimeout` remain live while `_sent` stays false. Retrying then overwrites the response without disposing the first one, and abandoning the failed call retains these resources; clean up the response, stream, and timeout source in a catch before rethrowing. ########## lib/netstd/Thrift/Transport/Client/THttpPerCallTransport.cs: ########## @@ -0,0 +1,145 @@ +// 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; + _response = await _parent.SendAsync(_request, cancellationToken); + + _responseReadTimeout?.Dispose(); + _responseReadTimeout = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken); + if (_parent.ConnectTimeout > 0) + { + _responseReadTimeout.CancelAfter(_parent.ConnectTimeout); + } + +#if NET5_0_OR_GREATER + _responseStream = await _response.Content.ReadAsStreamAsync(cancellationToken); +#else + _responseStream = await _response.Content.ReadAsStreamAsync(); +#endif + _sent = true; + } + + 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); + + var readToken = _responseReadTimeout?.Token ?? cancellationToken; + var count = await _responseStream.ReadAsync( + buffer, + offset, + length, + readToken); + + CountConsumedMessageBytes(count); + return count; Review Comment: The per-call path lets its response-read timeout escape as `OperationCanceledException`, whereas `THttpTransport.ReadAsync` translates the same timeout to `TTransportException.Interrupted` (lines 181-183). Callers using the new provider therefore receive a different transport error type when `ConnectTimeout` expires; catch this cancellation and apply the same translation here. -- 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]
