Files
scylladb/lang/wasm_alien_thread_runner.cc
Kefu Chai 0ba6627d5c wasm: block all signals in alien thread
as in main(), we use `stop_signal` to handle SIGINT and SIGTERM,
so when scylla receives a SIGTERM, the corresponding signal handler
could get called on any threads created by this program. so there
is chance that the alien_runner thread could be choosen to run the
signal handler setup by `main()`, but that signal handler assumes
the availability of Seastar reactor. unfortunately, we don't have
a Seastar reactor in alien thread. the same applies to Seastar's
`thread_pool` which handles the slow and blocking POSIX calls typically
used for interacting with files.

so, in this change, we use the same approach as Seastar's
`thread_pool::work()` -- just block all signals, so the alien threads
used by wasm for compiling UDF won't handle the signals using the
handlers planted by `main()`.

Fixes #13228
Signed-off-by: Kefu Chai <kefu.chai@scylladb.com>

Closes #13233
2023-03-20 11:20:19 +02:00

74 lines
2.2 KiB
C++

/*
* Copyright (C) 2023-present ScyllaDB
*/
/*
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
#include <exception>
#include <seastar/core/alien.hh>
#include <seastar/core/posix.hh>
#include <seastar/core/reactor.hh>
#include "lang/wasm.hh"
#include "lang/wasm_alien_thread_runner.hh"
#include "seastar/core/posix.hh"
namespace wasm {
std::optional<wasm_compile_task> task_queue::pop_front() {
std::unique_lock lock(_mut);
_cv.wait(lock, [this] { return !_pending.empty(); });
auto work_item = std::move(_pending.front());
_pending.pop();
return work_item;
}
void task_queue::push_back(std::optional<wasm_compile_task> work_item) {
std::unique_lock lock(_mut);
_pending.emplace(std::move(work_item));
lock.unlock();
_cv.notify_one();
}
alien_thread_runner::alien_thread_runner()
: _thread([this] {
sigset_t mask;
sigfillset(&mask);
auto r = ::pthread_sigmask(SIG_BLOCK, &mask, nullptr);
throw_pthread_error(r);
for (;;) {
auto work_item = _pending_queue.pop_front();
if (work_item) {
work_item->func();
} else {
break;
}
}
})
{ }
alien_thread_runner::~alien_thread_runner() {
_pending_queue.push_back(std::nullopt);
_thread.join();
}
void alien_thread_runner::submit(seastar::promise<rust::Box<wasmtime::Module>>& p, std::function<rust::Box<wasmtime::Module>()> f) {
seastar::noncopyable_function<void()> packaged([f = std::move(f), &p, &alien = seastar::engine().alien(), shard = seastar::this_shard_id()] () mutable {
try {
rust::Box<wasmtime::Module> mod = f();
seastar::alien::run_on(alien, shard, [&p, mod = std::move(mod)] () mutable {
p.set_value(std::move(mod));
});
} catch (...) {
seastar::alien::run_on(alien, shard, [&, eptr = std::current_exception()] {
p.set_exception(wasm::exception(format("Compilation failed: {}", eptr)));
});
}
});
_pending_queue.push_back(wasm_compile_task{.func = std::move(packaged), .done = p, .shard = seastar::this_shard_id()});
}
} // namespace wasm