Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

wasm: fix CPU profiler deadlock #17877

Merged
merged 2 commits into from
Apr 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cmake/dependencies.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ set(Seastar_API_LEVEL 6 CACHE STRING "" FORCE)
set(Seastar_CXX_FLAGS -Wno-error)
fetch_dep(seastar
REPO https://github.com/redpanda-data/seastar.git
TAG v23.3.x
TAG v24.1.x
PATCH_COMMAND sed -i "s/add_subdirectory (tests/# add_subdirectory (tests/g" CMakeLists.txt)

fetch_dep(avro
Expand Down
19 changes: 18 additions & 1 deletion src/v/wasm/tests/wasm_transform_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@

#include "bytes/bytes.h"
#include "pandaproxy/schema_registry/types.h"
#include "serde/rw/rw.h"
#include "wasm/errc.h"
#include "wasm/tests/wasm_fixture.h"

#include <seastar/core/internal/cpu_profiler.hh>
#include <seastar/core/reactor.hh>

#include <absl/strings/str_cat.h>
Expand Down Expand Up @@ -134,3 +134,20 @@ TEST_F(WasmTestFixture, LogsAreEmitted) {
expected.append(reinterpret_cast<const char*>(bytes.data()), bytes.size());
EXPECT_THAT(log_lines(), ElementsAre(expected));
}

TEST_F(WasmTestFixture, WorksWithCpuProfiler) {
bool original_enabled = ss::engine().get_cpu_profiler_enabled();
std::chrono::nanoseconds original_period
= ss::engine().get_cpu_profiler_period();
ss::engine().set_cpu_profiler_enabled(true);
ss::engine().set_cpu_profiler_period(100us);
load_wasm("dynamic.wasm");
EXPECT_THROW(execute_command("loop", 0), wasm::wasm_exception);
ss::engine().set_cpu_profiler_enabled(original_enabled);
ss::engine().set_cpu_profiler_period(original_period);
std::vector<ss::cpu_profiler_trace> traces;
ss::engine().profiler_results(traces);
for (const auto& t : traces) {
std::cout << t.user_backtrace << "\n";
}
}
9 changes: 9 additions & 0 deletions src/v/wasm/wasmtime.cc
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@

#include <seastar/core/align.hh>
#include <seastar/core/future.hh>
#include <seastar/core/internal/cpu_profiler.hh>
#include <seastar/core/posix.hh>
#include <seastar/core/sharded.hh>
#include <seastar/core/shared_ptr.hh>
Expand Down Expand Up @@ -599,7 +600,13 @@ class wasmtime_engine : public engine {
// Poll the call future to completion, yielding to the scheduler when
// the future yields.
auto start = ss::steady_clock_type::now();
// Disable profiling backtraces inside the VM - at the time of writing
// backtraces lead to segfaults causing deadlock in Seastar's signal
// handlers.
auto _ = ss::internal::scoped_disable_profile_temporarily();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what is the comment mean on this object in seastar tree This is not reentrant?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It means that it doesn't handle the case of nesting these RAII objects - when being destructed it doesn't set the flag to the original state, but unconditionally turns the flag off.

while (!wasmtime_call_future_poll(fut.get())) {
// Re-enable stacktraces before we yield control to the scheduler.
ss::internal::profiler_drop_stacktraces(false);
auto end = ss::steady_clock_type::now();
_probe.increment_cpu_time(end - start);
if (_pending_host_function) {
Expand All @@ -609,6 +616,8 @@ class wasmtime_engine : public engine {
co_await ss::coroutine::maybe_yield();
}
start = ss::steady_clock_type::now();
// Disable stacktraces as we enter back into Wasmtime
ss::internal::profiler_drop_stacktraces(true);
}
auto end = ss::steady_clock_type::now();
_probe.increment_cpu_time(end - start);
Expand Down
Loading