pitrou commented on a change in pull request #9633:
URL: https://github.com/apache/arrow/pull/9633#discussion_r588143037



##########
File path: cpp/src/arrow/flight/server.cc
##########
@@ -892,6 +914,25 @@ Status FlightServerBase::Serve() {
   impl_->old_signal_handlers_.clear();
   impl_->running_instance_ = impl_.get();
 
+#ifndef _WIN32
+  // Create the self-pipe for signal handlers to use
+  if (pipe(impl_->self_pipe_.data()) != 0) {

Review comment:
       We have `CreatePipe` in `arrow/util/io_util.h`, and it does work on 
Windows.

##########
File path: cpp/src/arrow/flight/server.cc
##########
@@ -791,16 +799,30 @@ struct FlightServerBase::Impl {
 
   void DoHandleSignal(int signum) {
     got_signal_ = signum;
+#ifdef _WIN32
     server_->Shutdown();
+#else
+    int saved_errno = errno;
+    // Ignore errors - pipe is nonblocking
+    write(self_pipe_[1], "0", 1);
+    errno = saved_errno;
+#endif
   }
+
+#ifndef _WIN32
+  static void WaitForSignals(int fd) {
+    // Wait for a signal handler to write to the pipe
+    int8_t buf[1];
+    read(fd, /*buf=*/buf, /*count=*/1);

Review comment:
       This could be fail with `EINTR`. Of course, presumably that's not really 
an error here :-)

##########
File path: cpp/src/arrow/flight/server.cc
##########
@@ -892,6 +914,25 @@ Status FlightServerBase::Serve() {
   impl_->old_signal_handlers_.clear();
   impl_->running_instance_ = impl_.get();
 
+#ifndef _WIN32
+  // Create the self-pipe for signal handlers to use
+  if (pipe(impl_->self_pipe_.data()) != 0) {
+    return arrow::internal::IOErrorFromErrno(
+        errno, "Could not initialize self-pipe to wait for signals");
+  }
+  // Make write end nonblocking
+  int flags = fcntl(impl_->self_pipe_[1], F_GETFL);
+  if (flags == -1) {
+    return arrow::internal::IOErrorFromErrno(
+        errno, "Could not initialize self-pipe to wait for signals");
+  }
+  flags |= O_NONBLOCK;
+  if (fcntl(impl_->self_pipe_[1], F_SETFL, flags) == -1) {
+    return arrow::internal::IOErrorFromErrno(
+        errno, "Could not initialize self-pipe to wait for signals");
+  }
+  std::thread handle_signals(&Impl::WaitForSignals, impl_->self_pipe_[0]);

Review comment:
       Ideally this all would be factored in a RAII object (what happens if 
there is an error below?).

##########
File path: cpp/src/arrow/flight/server.cc
##########
@@ -908,7 +949,20 @@ Status FlightServerBase::Serve() {
     RETURN_NOT_OK(
         SetSignalHandler(impl_->signals_[i], 
impl_->old_signal_handlers_[i]).status());
   }
-
+#ifndef _WIN32
+  // We don't care if writing is blocked - that means a signal was already 
raised
+  if (write(impl_->self_pipe_[1], "0", 1) < 0 && errno != EAGAIN &&
+      errno != EWOULDBLOCK && errno != EINTR) {
+    return arrow::internal::IOErrorFromErrno(errno, "Could not unblock signal 
thread");
+  }
+  if (close(impl_->self_pipe_[0]) < 0) {

Review comment:
       `FileClose`




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

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


Reply via email to