Copilot commented on code in PR #3930: URL: https://github.com/apache/thrift/pull/3930#discussion_r4107498249
########## lib/netstd/Thrift/Transport/ITPerCallTransportProvider.cs: ########## @@ -0,0 +1,35 @@ +// 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.Threading; +using System.Threading.Tasks; + +namespace Thrift.Transport +{ + /// <summary> + /// Provides a mechanism to create per-call transports for each request. + /// </summary> + public interface ITPerCallTransportProvider Review Comment: The netstd generator still emits clients with shared `InputProtocol`/`OutputProtocol` instances and flushes `OutputProtocol.Transport` directly (`compiler/cpp/src/thrift/generate/t_netstd_generator.cc:2435-2504`); no production code consumes this provider. As a result, clients generated by this repository cannot create per-call transports and remain susceptible to the interleaving failure this PR claims to address. Please include the generator/client-construction integration and generated-client tests in this change. ########## lib/netstd/Thrift/Transport/Client/THttpPerCallTransport.cs: ########## @@ -0,0 +1,257 @@ +// 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) + { + 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) + { + 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; + } + + var response = await _parent.SendAsync(_request, cancellationToken); + + var responseReadTimeout = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken); + if (_parent.ConnectTimeout > 0) + { + responseReadTimeout.CancelAfter(_parent.ConnectTimeout); + } + + Stream responseStream = null; + try + { +responseStream = await THttpTransport.ReadAsStreamAsync(response.Content, responseReadTimeout.Token); Review Comment: This per-call path also keeps the completed flush operation's token linked for the entire response lifetime. If that token is cancelled after `FlushAsync` returns, a subsequent read with an unrelated token is aborted. Link it only for stream acquisition and retain a separate timeout source for reads. ########## lib/netstd/Thrift/Transport/Client/THttpTransport.cs: ########## @@ -237,28 +284,80 @@ private void ConfigureClient(HttpClient httpClient) public override async Task FlushAsync(CancellationToken cancellationToken) { + HttpClient httpClient; + Stream outputStream; + lock (_lifecycleLock) + { + if (!_lifecycleOpen || _httpClient == null || _outputStream == null) + { + throw new TTransportException(TTransportException.ExceptionType.NotOpen, + "The transport has been closed."); + } + httpClient = _httpClient; + outputStream = _outputStream; + _flushInProgress++; + } + + StreamContent contentStream = null; + HttpRequestMessage request = null; + HttpResponseMessage response = null; + CancellationTokenSource responseReadTimeout = null; + Stream inputStream = null; try { - _outputStream.Seek(0, SeekOrigin.Begin); + outputStream.Seek(0, SeekOrigin.Begin); - using (var contentStream = new StreamContent(_outputStream)) + contentStream = new StreamContent(outputStream); + contentStream.Headers.ContentType = ContentType ?? new MediaTypeHeaderValue(@"application/x-thrift"); + + request = new HttpRequestMessage(HttpMethod.Post, _uri) { - contentStream.Headers.ContentType = ContentType ?? new MediaTypeHeaderValue(@"application/x-thrift"); + Content = contentStream + }; - var response = (await _httpClient.PostAsync(_uri, contentStream, cancellationToken)).EnsureSuccessStatusCode(); + response = await httpClient.SendAsync( + request, + HttpCompletionOption.ResponseHeadersRead, + cancellationToken); + response.EnsureSuccessStatusCode(); - _inputStream?.Dispose(); -#if NET5_0_OR_GREATER - _inputStream = await response.Content.ReadAsStreamAsync(cancellationToken); -#else - _inputStream = await response.Content.ReadAsStreamAsync(); -#endif - if (_inputStream.CanSeek) + responseReadTimeout = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken); + if (_connectTimeout > 0) + { + responseReadTimeout.CancelAfter(_connectTimeout); + } + + inputStream = await ReadAsStreamAsync(response.Content, responseReadTimeout.Token); Review Comment: `FlushAsync` retains a linked source containing its caller's cancellation token. Cancelling that token after `FlushAsync` has completed therefore cancels a later `ReadAsync` that may use a different token, and the read is misreported as a response timeout. Keep the response timeout source independent, and link the flush token only while acquiring the response stream. -- 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]
