forked from apache/arrow-adbc
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(python/adbc_driver_manager): handle KeyboardInterrupt
Fixes apache#1484.
- Loading branch information
Showing
11 changed files
with
363 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
182 changes: 182 additions & 0 deletions
182
python/adbc_driver_manager/adbc_driver_manager/_blocking_impl.cc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,182 @@ | ||
// 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. | ||
|
||
#include "_blocking_impl.h" | ||
|
||
#include <fcntl.h> | ||
#include <unistd.h> | ||
|
||
#include <csignal> | ||
#include <cstring> | ||
#include <iostream> | ||
#include <mutex> | ||
#include <thread> | ||
|
||
namespace pyadbc_driver_manager { | ||
|
||
// This is somewhat derived from io_util.cc in arrow, but that implementation | ||
// isn't easily used outside of Arrow's monolith. | ||
namespace { | ||
static std::once_flag kInitOnce; | ||
// We may encounter errors below that we can't do anything about. Use this to | ||
// print out an error, once. | ||
static std::once_flag kWarnOnce; | ||
// This thread reads from a pipe forever. Whenver it reads something, it | ||
// calls the callback below. | ||
static std::thread kCancelThread; | ||
|
||
static std::mutex cancel_mutex; | ||
// This callback is registered by the Python side; basically it will call | ||
// cancel() on an ADBC object. | ||
static void (*cancel_callback)(void*) = nullptr; | ||
// Callback state (a pointer to the ADBC PyObject). | ||
static void* cancel_callback_data = nullptr; | ||
// A nonblocking self-pipe. | ||
static int pipe[2]; | ||
// The old signal handler (most likely Python's). | ||
struct sigaction old_sigint; | ||
// Our signal handler (below). | ||
struct sigaction our_sigint; | ||
|
||
std::string MakePipe() { | ||
int rc = 0; | ||
#if defined(__linux__) && defined(__GLIBC__) | ||
rc = pipe2(pipe, O_CLOEXEC); | ||
#elif defined(_WIN32) | ||
return "Unsupported platform"; | ||
#else | ||
rc = ::pipe(pipe); | ||
#endif | ||
|
||
if (rc != 0) { | ||
return std::strerror(errno); | ||
} | ||
|
||
// We may need to set FD_CLOEXEC on platforms without pipe2. | ||
#if (!defined(__linux__) || !defined(__GLIBC__)) && !defined(_WIN32) | ||
{ | ||
int flags = fcntl(pipe[0], F_GETFD, 0); | ||
if (flags < 0) { | ||
return std::strerror(errno); | ||
} | ||
rc = fcntl(pipe[0], F_SETFD, flags | FD_CLOEXEC); | ||
if (rc < 0) { | ||
return std::strerror(errno); | ||
} | ||
|
||
flags = fcntl(pipe[1], F_GETFD, 0); | ||
if (flags < 0) { | ||
return std::strerror(errno); | ||
} | ||
rc = fcntl(pipe[1], F_SETFD, flags | FD_CLOEXEC); | ||
if (rc < 0) { | ||
return std::strerror(errno); | ||
} | ||
} | ||
#endif | ||
|
||
// Make the write side nonblocking (the read side should stay blocking!) | ||
#if defined(_WIN32) | ||
return "Unsupported platform"; | ||
#else | ||
{ | ||
int flags = fcntl(pipe[1], F_GETFL, 0); | ||
if (flags < 0) { | ||
return std::strerror(errno); | ||
} | ||
rc = fcntl(pipe[1], F_SETFL, flags | O_NONBLOCK); | ||
if (rc < 0) { | ||
return std::strerror(errno); | ||
} | ||
} | ||
#endif | ||
|
||
return ""; | ||
} | ||
|
||
void InterruptThread() { | ||
while (true) { | ||
char buf = 0; | ||
ssize_t bytes_read = read(pipe[0], &buf, 1); | ||
if (bytes_read < 0) { | ||
if (errno == EINTR) continue; | ||
|
||
// XXX: we failed reading from the pipe | ||
std::string message = std::strerror(errno); | ||
std::call_once(kWarnOnce, [&]() { | ||
std::cerr << "adbc_driver_manager (native code): error handling interrupt: " | ||
<< message << std::endl; | ||
}); | ||
} else if (bytes_read > 0) { | ||
std::lock_guard<std::mutex> lock(cancel_mutex); | ||
if (cancel_callback != nullptr) { | ||
cancel_callback(cancel_callback_data); | ||
} | ||
cancel_callback = nullptr; | ||
cancel_callback_data = nullptr; | ||
} | ||
} | ||
} | ||
|
||
void SigintHandler(int) { (void)write(pipe[1], "X", 1); } | ||
|
||
} // namespace | ||
|
||
std::string InitBlockingCallback() { | ||
std::string error; | ||
std::call_once(kInitOnce, [&]() { | ||
error = MakePipe(); | ||
if (!error.empty()) { | ||
return; | ||
} | ||
|
||
our_sigint.sa_handler = &SigintHandler; | ||
our_sigint.sa_flags = 0; | ||
sigemptyset(&our_sigint.sa_mask); | ||
|
||
kCancelThread = std::thread(InterruptThread); | ||
kCancelThread.detach(); | ||
// TODO: set name of thread | ||
}); | ||
return error; | ||
} | ||
|
||
std::string SetBlockingCallback(void (*callback)(void*), void* data) { | ||
std::lock_guard<std::mutex> lock(cancel_mutex); | ||
cancel_callback = callback; | ||
cancel_callback_data = data; | ||
|
||
int rc = sigaction(SIGINT, &our_sigint, &old_sigint); | ||
if (rc != 0) { | ||
return std::strerror(errno); | ||
} | ||
return ""; | ||
} | ||
|
||
std::string ClearBlockingCallback() { | ||
std::lock_guard<std::mutex> lock(cancel_mutex); | ||
cancel_callback = nullptr; | ||
cancel_callback_data = nullptr; | ||
|
||
int rc = sigaction(SIGINT, &old_sigint, nullptr); | ||
if (rc != 0) { | ||
return std::strerror(errno); | ||
} | ||
return ""; | ||
} | ||
|
||
} // namespace pyadbc_driver_manager |
38 changes: 38 additions & 0 deletions
38
python/adbc_driver_manager/adbc_driver_manager/_blocking_impl.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
// 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. | ||
|
||
/// Allow KeyboardInterrupt to function with ADBC in Python. | ||
/// | ||
/// Call SetBlockingCallback to register a callback. This will temporarily | ||
/// suppress the Python SIGINT handler. When SIGINT is received, this module | ||
/// will handle it by calling the callback. | ||
|
||
#include <string> | ||
|
||
namespace pyadbc_driver_manager { | ||
|
||
/// \brief Set up internal state to handle. | ||
/// \return An error message (or empty string). | ||
std::string InitBlockingCallback(); | ||
/// \brief Set the callback for when SIGINT is received. | ||
/// \return An error message (or empty string). | ||
std::string SetBlockingCallback(void (*callback)(void*), void* data); | ||
/// \brief Clear the callback for when SIGINT is received. | ||
/// \return An error message (or empty string). | ||
std::string ClearBlockingCallback(); | ||
|
||
} // namespace pyadbc_driver_manager |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.