Copilot commented on code in PR #3930: URL: https://github.com/apache/thrift/pull/3930#discussion_r4108850773
########## lib/netstd/Thrift/Transport/Client/THttpPerCallTransport.cs: ########## @@ -0,0 +1,288 @@ +// 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 readonly CancellationTokenSource _lifecycleCancellation = new CancellationTokenSource(); + + 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; Review Comment: `IsOpen` can report `true` after the parent has closed: `THttpTransport.Close()` marks the parent lifecycle closed before it disposes each child, and this property checks only the child flag without synchronization. Read the child and parent lifecycle state under `LifecycleLock` so callers do not attempt operations on a transport whose shared HTTP client is already gone. ########## lib/netstd/Thrift/Transport/Client/THttpTransport.cs: ########## @@ -237,28 +293,88 @@ private void ConfigureClient(HttpClient httpClient) public override async Task FlushAsync(CancellationToken cancellationToken) { + HttpClient httpClient; + Stream outputStream; + int connectTimeout; + lock (_lifecycleLock) + { + if (!_lifecycleOpen || _httpClient == null || _outputStream == null) + { + throw new TTransportException(TTransportException.ExceptionType.NotOpen, + "The transport has been closed."); + } + httpClient = _httpClient; + outputStream = _outputStream; + connectTimeout = _connectTimeout; + _flushInProgress++; + } + + StreamContent contentStream = null; + HttpRequestMessage request = null; + HttpResponseMessage response = null; + CancellationTokenSource responseReadTimeout = null; + Stream inputStream = null; + var responseDeadline = Stopwatch.StartNew(); try { - _outputStream.Seek(0, SeekOrigin.Begin); + outputStream.Seek(0, SeekOrigin.Begin); - using (var contentStream = new StreamContent(_outputStream)) - { - contentStream.Headers.ContentType = ContentType ?? new MediaTypeHeaderValue(@"application/x-thrift"); + contentStream = new StreamContent(outputStream); + contentStream.Headers.ContentType = ContentType ?? new MediaTypeHeaderValue(@"application/x-thrift"); - var response = (await _httpClient.PostAsync(_uri, contentStream, cancellationToken)).EnsureSuccessStatusCode(); + request = new HttpRequestMessage(HttpMethod.Post, _uri) + { + Content = contentStream + }; + + response = await httpClient.SendAsync( + request, + HttpCompletionOption.ResponseHeadersRead, + cancellationToken); + response.EnsureSuccessStatusCode(); + + responseReadTimeout = new CancellationTokenSource(); + var remainingTimeout = connectTimeout - (int)responseDeadline.ElapsedMilliseconds; + if (connectTimeout > 0) + { + responseReadTimeout.CancelAfter(Math.Max(0, remainingTimeout)); + } + + using (var acquisitionCts = CancellationTokenSource.CreateLinkedTokenSource( + cancellationToken, responseReadTimeout.Token)) Review Comment: The response-stream acquisition is linked only to the caller token and timeout, not to the transport lifecycle. If `Close()` runs after headers arrive while `ReadAsStreamAsync` is still pending, closing the transport cancels `_lifecycleCancellation`, but this flush remains alive until the timeout (the legacy cancellation shim especially depends on receiving a token). Capture a lifecycle-linked operation token while holding the initial lifecycle lock and use it for both `SendAsync` and stream acquisition so closing the parent promptly terminates the flush. -- 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]
