You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@arrow.apache.org by GitBox <gi...@apache.org> on 2022/05/30 15:45:45 UTC

[GitHub] [arrow] pitrou commented on a diff in pull request #13176: ARROW-16597: [Python][FlightRPC] Force server shutdown at interpreter exit

pitrou commented on code in PR #13176:
URL: https://github.com/apache/arrow/pull/13176#discussion_r884947195


##########
python/pyarrow/_flight.pyx:
##########
@@ -2516,6 +2517,32 @@ cdef class _ServerMiddlewareWrapper(ServerMiddleware):
             instance.call_completed(exception)
 
 
+cdef class _FlightServerFinalizer(_Weakrefable):
+    """
+    A finalizer that shuts down the server on destruction.
+
+    See ARROW-16597. If the server is still active at interpreter
+    exit, the process may segfault.
+    """
+
+    cdef:
+        shared_ptr[PyFlightServer] server
+
+    def finalize(self):
+        cdef:
+            PyFlightServer* server = self.server.get()
+            CStatus status
+        if server == NULL:
+            return
+        with nogil:
+            status = server.Shutdown()
+        check_flight_status(status)
+        with nogil:
+            status = server.Wait()
+        check_flight_status(status)
+        self.server.reset()

Review Comment:
   `reset()` won't be called if there was an error above, could this be a problem?



##########
python/pyarrow/_flight.pyx:
##########
@@ -2550,11 +2577,13 @@ cdef class FlightServerBase(_Weakrefable):
     """
 
     cdef:
-        unique_ptr[PyFlightServer] server
+        shared_ptr[PyFlightServer] server
+        object _finalizer

Review Comment:
   Nit, but I don't think `cdef` members are exposed in Python, so could name it `finalizer`?



##########
python/pyarrow/tests/arrow_16597.py:
##########
@@ -0,0 +1,36 @@
+# 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.
+
+# This file is called from a test in test_flight.py.
+import time
+
+import pyarrow as pa
+import pyarrow.flight as flight
+
+
+class Server(flight.FlightServerBase):
+    def do_put(self, context, descriptor, reader, writer):
+        time.sleep(1)
+        raise flight.FlightCancelledError("")
+
+
+server = Server("grpc://localhost:0")

Review Comment:
   Nit: put this under a `if __name__ == "__main__"` guard?



##########
python/pyarrow/tests/test_flight.py:
##########
@@ -1993,6 +1993,11 @@ def test(read_all):
         descriptor = flight.FlightDescriptor.for_command(b"echo")
         writer, reader = client.do_exchange(descriptor)
         test(reader.read_all)
+        try:
+            writer.close()
+        except Exception:
+            # Silence the Cancelled/Interrupt exception

Review Comment:
   If we only want to silence a certain class of exceptions, can the `except` clause be more specific?



##########
python/pyarrow/_flight.pyx:
##########
@@ -2516,6 +2517,32 @@ cdef class _ServerMiddlewareWrapper(ServerMiddleware):
             instance.call_completed(exception)
 
 
+cdef class _FlightServerFinalizer(_Weakrefable):
+    """
+    A finalizer that shuts down the server on destruction.
+
+    See ARROW-16597. If the server is still active at interpreter
+    exit, the process may segfault.
+    """
+
+    cdef:
+        shared_ptr[PyFlightServer] server
+
+    def finalize(self):
+        cdef:
+            PyFlightServer* server = self.server.get()
+            CStatus status
+        if server == NULL:
+            return
+        with nogil:
+            status = server.Shutdown()
+        check_flight_status(status)
+        with nogil:
+            status = server.Wait()
+        check_flight_status(status)

Review Comment:
   Is it instead possible to write this?
   ```suggestion
           with nogil:
               status = server.Shutdown() & server.Wait()
           check_flight_status(status)
   ```



-- 
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: github-unsubscribe@arrow.apache.org

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