Copilot commented on code in PR #50407:
URL: https://github.com/apache/arrow/pull/50407#discussion_r4208017066


##########
cpp/src/arrow/flight/server.h:
##########
@@ -195,10 +195,9 @@ class ARROW_FLIGHT_EXPORT FlightServerBase {
   Status Init(const FlightServerOptions& options);
 
   /// \brief Get the port that the Flight server is listening on.
-  /// This method must only be called after Init().  Will return a
-  /// non-positive value if no port exists (e.g. when listening on a
-  /// domain socket).
-  int port() const;
+  /// This method must only be called after Init().
+  /// \return Arrow result with the port number, or an error if it cannot be 
determined.
+  arrow::Result<int> port() const;

Review Comment:
   This changes the public return type from `int` to `Result<int>`, but the 
in-tree callers still stream, compare, stringify, and pass `server->port()` as 
an `int` (for example `flight/flight_test.cc:215`, `flight/test_util.h:89`, and 
`flight/test_definitions.cc:116`). `Result` has no implicit conversion, so this 
change does not compile without updating all callers (or preserving the 
existing API and adding a separate error-returning method).



##########
cpp/src/arrow/flight/transport/grpc/util_internal.h:
##########
@@ -51,6 +35,12 @@ class Status;
 
 namespace flight {
 
+#define GRPC_CPP_VERSION_CHECK(major, minor, patch)                            
 \
+  ((GRPC_CPP_VERSION_MAJOR > (major) ||                                        
 \
+    (GRPC_CPP_VERSION_MAJOR == (major) && GRPC_CPP_VERSION_MINOR > (minor)) || 
 \
+    ((GRPC_CPP_VERSION_MAJOR == (major) && GRPC_CPP_VERSION_MINOR == (minor) 
&& \
+      GRPC_CPP_VERSION_PATCH >= (patch)))))

Review Comment:
   These macros are now expanded without checking that `GRPC_CPP_VERSION_*` 
exists. `util_internal.h` is included before `<grpcpp/grpcpp.h>` in 
`util_internal.cc` and directly from `server_tracing_middleware.cc`, so 
supported gRPC versions/translation units where the version macros are not 
transitively defined will fail preprocessing; the previous guard handled this. 
Restore the fallback definition for older or missing gRPC version macros.
   
   This issue also appears on line 100 of the same file.



##########
cpp/src/arrow/flight/transport_server.h:
##########
@@ -21,6 +21,7 @@
 #include <memory>
 
 #include "arrow/flight/transport.h"
+#include "arrow/flight/transport_server_internal.h"

Review Comment:
   `transport_server.h` is a public header, but `ARROW_INSTALL_ALL_HEADERS` 
deliberately excludes headers whose basename contains `internal` 
(`cmake_modules/BuildUtils.cmake:960-962`). Consequently installed consumers 
include this file and fail because `arrow/flight/transport_server_internal.h` 
is not installed. The internal header is not needed for the public 
declarations, so remove this include and include it only from the 
implementation files.



##########
cpp/src/arrow/flight/transport_server_internal.h:
##########
@@ -0,0 +1,275 @@
+// 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.
+
+#pragma once
+
+#include <atomic>
+#include <chrono>
+#include <memory>
+#include <thread>
+#include <utility>
+#include <vector>
+
+#include "arrow/flight/platform.h"
+#include "arrow/flight/server.h"
+#include "arrow/flight/visibility.h"
+#include "arrow/result.h"
+#include "arrow/status.h"
+#include "arrow/util/io_util.h"
+#include "arrow/util/logging.h"
+#include "arrow/util/uri.h"

Review Comment:
   `transport_server.h` includes this header, but the declarations below use 
`ipc::ReadStats` and `ipc::WriteStats` without including headers that declare 
those structs. `server.h` only brings in `ipc/type_fwd.h`, which does not 
declare either struct, so consumers of `transport_server.h` will fail with 
unknown-type errors. Include `arrow/ipc/reader.h` and `arrow/ipc/writer.h` here 
(or add suitable declarations).



##########
cpp/src/arrow/flight/transport_server_internal.h:
##########
@@ -0,0 +1,275 @@
+// 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.
+
+#pragma once
+
+#include <atomic>
+#include <chrono>
+#include <memory>
+#include <thread>
+#include <utility>
+#include <vector>
+
+#include "arrow/flight/platform.h"
+#include "arrow/flight/server.h"
+#include "arrow/flight/visibility.h"
+#include "arrow/result.h"
+#include "arrow/status.h"
+#include "arrow/util/io_util.h"
+#include "arrow/util/logging.h"
+#include "arrow/util/uri.h"
+
+namespace arrow::flight::internal {
+
+class ServerDataStream;
+class PeekableFlightDataReader;
+
+/// \brief Adapt TransportDataStream to the FlightMessageReader
+///   interface for DoPut.
+class TransportMessageReader final : public FlightMessageReader {
+ public:
+  TransportMessageReader(ServerDataStream* stream,
+                         std::shared_ptr<MemoryManager> memory_manager);
+
+  Status Init();
+
+  const FlightDescriptor& descriptor() const override;
+
+  arrow::Result<std::shared_ptr<Schema>> GetSchema() override;
+
+  arrow::Result<FlightStreamChunk> Next() override;
+
+  ipc::ReadStats stats() const override;
+
+ private:
+  /// Ensure we are set up to read data.
+  Status EnsureDataStarted();
+
+  FlightDescriptor descriptor_;
+  std::shared_ptr<PeekableFlightDataReader> peekable_reader_;
+  std::shared_ptr<MemoryManager> memory_manager_;
+  std::shared_ptr<ipc::RecordBatchStreamReader> batch_reader_;
+  std::shared_ptr<Buffer> app_metadata_;
+};
+
+/// \brief A writer for IPC payloads to a ServerDataStream.
+class TransportMessageWriter final : public FlightMessageWriter {
+ public:
+  explicit TransportMessageWriter(ServerDataStream* stream);
+
+  Status Begin(const std::shared_ptr<Schema>& schema,
+               const ipc::IpcWriteOptions& options) override;
+
+  Status WriteRecordBatch(const RecordBatch& batch) override;
+
+  Status WriteMetadata(std::shared_ptr<Buffer> app_metadata) override;
+
+  Status WriteWithMetadata(const RecordBatch& batch,
+                           std::shared_ptr<Buffer> app_metadata) override;
+
+  Status Close() override;
+
+  ipc::WriteStats stats() const override;
+
+ private:
+  Status CheckStarted();
+
+  ServerDataStream* stream_;
+  std::unique_ptr<ipc::RecordBatchWriter> batch_writer_;
+  std::shared_ptr<Buffer> app_metadata_;
+  ::arrow::ipc::IpcWriteOptions ipc_options_;
+};
+
+/// \brief A writer for application-defined metadata to a ServerDataStream.
+class TransportMetadataWriter final : public FlightMetadataWriter {
+ public:
+  explicit TransportMetadataWriter(ServerDataStream* stream);
+
+  Status WriteMetadata(const Buffer& buffer) override;
+
+ private:
+  ServerDataStream* stream_;
+};
+
+/// \brief Write a FlightDataStream to a transport-level stream.
+///
+/// Used by DoGet and DoExchange to stream results back to the client.
+/// The schema is written first, followed by record batches until the
+/// stream is exhausted.
+///
+/// \param[in] data_stream The Arrow data stream to read from.
+/// \param[in] stream The transport-specific data stream to write to.
+Status WriteDataStream(std::unique_ptr<FlightDataStream> data_stream,
+                       ServerDataStream* stream);
+
+/// \brief Manages a background thread that waits for OS signals.
+///
+/// Creates a signal-safe SelfPipe and spawns a thread that blocks on it.
+/// When a signal is received, the signal handler writes to the SelfPipe
+/// to wake the thread. The destructor ensures the thread is joined.
+class ServerSignalHandler {
+ public:
+  ARROW_DISALLOW_COPY_AND_ASSIGN(ServerSignalHandler);
+  ServerSignalHandler() = default;
+
+  /// \brief Initialize the signal handler and start the background thread.
+  ///
+  /// \param[in] handler A callable that will be invoked on the background
+  ///   thread, receiving the SelfPipe as an argument.
+  /// \return The SelfPipe, which can be used to signal the handler thread.
+  template <typename Fn>
+  arrow::Result<std::shared_ptr<::arrow::internal::SelfPipe>> Init(Fn handler) 
{
+    ARROW_ASSIGN_OR_RAISE(self_pipe_,
+                          
::arrow::internal::SelfPipe::Make(/*signal_safe=*/true));
+    handle_signals_ = std::thread(handler, self_pipe_);
+    return self_pipe_;
+  }
+
+  /// \brief Shut down the signal handler and join the background thread.
+  Status Shutdown() {
+    RETURN_NOT_OK(self_pipe_->Shutdown());
+    handle_signals_.join();
+    return Status::OK();
+  }
+
+  ~ServerSignalHandler() { ARROW_CHECK_OK(Shutdown()); }
+
+ private:
+  std::shared_ptr<::arrow::internal::SelfPipe> self_pipe_;
+  std::thread handle_signals_;
+};
+
+/// \brief Manages signal-driven graceful shutdown for server transports.
+///
+/// Installs signal handlers for the given set of signals. When a signal is
+/// received, the registered shutdown callback is invoked. Uses a SelfPipe
+/// to safely bridge the signal handler (which runs in a restricted context)
+/// to a background thread that performs the actual shutdown.
+class ARROW_FLIGHT_EXPORT ServerSignalState {
+ public:
+  ServerSignalState() = default;
+
+  /// \brief Configure which signals should trigger shutdown.
+  ///
+  /// \param[in] sigs List of signal numbers (e.g. SIGINT, SIGTERM).
+  Status SetShutdownOnSignals(const std::vector<int>& sigs);
+  /// \brief Return the last signal that was received, or 0 if none.
+  int GotSignal() const;
+
+  /// \brief Run the server with signal-aware lifecycle management.
+  ///
+  /// Installs signal handlers, calls \p wait (which should block until the
+  /// server stops), and restores original signal handlers on return.
+  /// If a signal is received during \p wait, \p shutdown is called first.
+  ///
+  /// \param[in] wait A callable that blocks until the server stops.
+  /// \param[in] shutdown A callable invoked on signal receipt to stop the 
server.
+  /// \param[in] not_started_message Logged if shutdown is called before the 
server
+  /// starts. \param[in] shutdown_warning Logged if shutdown itself fails.
+  template <typename WaitFn, typename ShutdownFn>
+  Status Serve(WaitFn&& wait, ShutdownFn&& shutdown, const char* 
not_started_message,
+               const char* shutdown_warning) {
+    got_signal_ = 0;
+    old_signal_handlers_.clear();
+    running_instance_ = this;
+
+    ServerSignalHandler signal_handler;
+    ARROW_ASSIGN_OR_RAISE(self_pipe_,
+                          
signal_handler.Init(&ServerSignalState::WaitForSignals));
+    for (size_t i = 0; i < signals_.size(); ++i) {
+      int signum = signals_[i];
+      ::arrow::internal::SignalHandler 
new_handler(&ServerSignalState::HandleSignal),
+          old_handler;
+      ARROW_ASSIGN_OR_RAISE(old_handler,
+                            ::arrow::internal::SetSignalHandler(signum, 
new_handler));
+      old_signal_handlers_.push_back(std::move(old_handler));
+    }
+
+    shutdown_ = [shutdown = std::forward<ShutdownFn>(shutdown)]() mutable {
+      return shutdown(nullptr);
+    };
+    shutdown_warning_ = shutdown_warning;

Review Comment:
   The signal thread is started at line 192 and signal handlers are installed 
before `shutdown_` is assigned here. If a registered signal arrives in that 
window, `WaitForSignals` consumes the wakeup while observing a null callback, 
then `Serve()` waits forever because the signal has been lost. Initialize the 
shutdown callback before starting/installing signal handling, or make the 
waiter synchronize with callback setup.
   
   This issue also appears on line 213 of the same file.



##########
cpp/src/arrow/flight/types.h:
##########
@@ -564,6 +564,16 @@ struct ARROW_FLIGHT_EXPORT Location : public 
internal::BaseType<Location> {
   /// Use `Deserialize(serialized)` if you want a Result-returning version.
   static arrow::Status Deserialize(std::string_view serialized, Location* out);
 
+  /// \brief Get the URI representation of this location.
+  ///
+  /// \return Arrow result with the URI.
+  arrow::Result<arrow::util::Uri> uri() const;

Review Comment:
   `types.h` only forward-declares `arrow::util::Uri`, but `arrow::Result<T>` 
embeds `AlignedStorage<T>` and therefore needs `T` complete when this return 
type is instantiated. Including this public header now fails because 
`Result<arrow::util::Uri>` requires `sizeof(Uri)`; include `arrow/util/uri.h` 
in `types.h` rather than relying on the implementation file.



-- 
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]

Reply via email to