From 3145d05210805a9bb10537a4b19056e2d2658f19 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jean-Micha=C3=ABl=20Celerier?= Date: Fri, 3 Apr 2026 06:33:21 -0400 Subject: [PATCH] pipewire: validity fixes on polling things --- .../libremidi/backends/pipewire/context.hpp | 2 ++ .../libremidi/backends/pipewire/helpers.hpp | 26 ++++++++++++++----- 2 files changed, 22 insertions(+), 6 deletions(-) diff --git a/include/libremidi/backends/pipewire/context.hpp b/include/libremidi/backends/pipewire/context.hpp index d13b7af1..450ea04d 100644 --- a/include/libremidi/backends/pipewire/context.hpp +++ b/include/libremidi/backends/pipewire/context.hpp @@ -172,7 +172,9 @@ struct pipewire_context // Add a manual 1ms event loop iteration at the end of // ctor to ensure synchronous clients will still see the ports + pw_loop_enter(this->lp); pw_loop_iterate(this->lp, 1); + pw_loop_leave(this->lp); } void initialize_observation() diff --git a/include/libremidi/backends/pipewire/helpers.hpp b/include/libremidi/backends/pipewire/helpers.hpp index a8dc2be0..6e1edb43 100644 --- a/include/libremidi/backends/pipewire/helpers.hpp +++ b/include/libremidi/backends/pipewire/helpers.hpp @@ -156,11 +156,18 @@ struct pipewire_helpers { // Note: called from a std::jthread. assert(this->global_context); + auto lp = this->global_context->lp; + if (!lp) + return; + if (int fd = this->global_context->get_fd(); fd != -1) { fds[0] = {.fd = fd, .events = POLLIN, .revents = 0}; current_state = poll_state::in_poll; + // pw_loop_iterate requires the loop to be entered first. + pw_loop_enter(lp); + for (;;) { if (int err = poll(fds, 2, -1); err < 0) @@ -174,13 +181,10 @@ struct pipewire_helpers // Check pipewire fd: if (fds[0].revents & POLLIN) { - if (auto lp = this->global_context->lp) + int result = pw_loop_iterate(lp, 0); + if (result < 0) { - int result = pw_loop_iterate(lp, 0); - if (result < 0) - { - LIBREMIDI_LOG(spa_strerror(result)); - } + LIBREMIDI_LOG(spa_strerror(result)); } fds[0].revents = 0; } @@ -191,6 +195,8 @@ struct pipewire_helpers break; } } + + pw_loop_leave(lp); } current_state = poll_state::not_in_poll; } @@ -347,7 +353,11 @@ struct pipewire_helpers { // Wait for the pipewire server to send us back our node's info for (int i = 0; i < 1000; i++) + { this->filter->synchronize_node(); + if (this->filter->filter_node_id() != 4294967295) + break; + } auto this_node = this->filter->filter_node_id(); auto& midi = this->global_context->current_graph.software_midi; @@ -380,7 +390,11 @@ struct pipewire_helpers { // Wait for the pipewire server to send us back our node's info for (int i = 0; i < 1000; i++) + { this->filter->synchronize_node(); + if (this->filter->filter_node_id() != 4294967295) + break; + } auto this_node = this->filter->filter_node_id(); auto& midi = this->global_context->current_graph.software_midi;