Copilot commented on code in PR #3930: URL: https://github.com/apache/thrift/pull/3930#discussion_r4107954363
########## lib/netstd/Thrift/Transport/Client/THttpPerCallTransport.cs: ########## @@ -0,0 +1,262 @@ +// 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 = new CancellationTokenSource(); + if (_parent.ConnectTimeout > 0) + { + responseReadTimeout.CancelAfter(_parent.ConnectTimeout); + } + + Stream responseStream = null; + try + { Review Comment: `Close()` can run after `SendAsync` returns but before this timeout setup. `ConnectTimeout` then races its null check against `_httpClient = null`; a `NullReferenceException` here also occurs before the cleanup `try`, leaving the successful response undisposed. Snapshot the timeout while holding the lifecycle lock and put setup inside the cleanup scope so this race consistently reports `NotOpen` and releases the response. ########## compiler/cpp/src/thrift/generate/t_netstd_generator.cc: ########## @@ -2439,6 +2439,20 @@ void t_netstd_generator::generate_service_client(ostream& out, t_service* tservi << indent() << "public Client(TProtocol inputProtocol, TProtocol outputProtocol) : base(inputProtocol, outputProtocol)" << '\n' << indent() << "{" << '\n' << indent() << "}" << '\n' + << '\n' + << indent() << "public Client(TTransport transport, TProtocolFactory protocolFactory) : this(transport, protocolFactory, protocolFactory)" << '\n' + << indent() << "{" << '\n' + << indent() << "}" << '\n' + << '\n' + << indent() << "public Client(TTransport transport, TProtocolFactory inputProtocolFactory, TProtocolFactory outputProtocolFactory)" << '\n' + << indent() << " : this(inputProtocolFactory.GetProtocol(transport), outputProtocolFactory.GetProtocol(transport), transport, inputProtocolFactory, outputProtocolFactory)" << '\n' Review Comment: These factory calls are evaluated left-to-right in a constructor initializer. If the input factory creates a protocol and the output factory then throws, construction exits with no reference from which to dispose the first protocol, so custom protocol resources leak. Move factory validation/creation into a helper or base constructor that can clean up partial creation before rethrowing. -- 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]
