Skip to content

Commit 8279c2a

Browse files
committed
fix(sway/ipc): reconnect on disconnect instead of breaking + CPU-spinning
When sway's event-subscription send buffer overflows during an event flood, sway closes the client connection. The sway IPC event worker (SleeperThread running handleEvent -> recv) then threw on every iteration and the SleeperThread immediately re-invoked it, leaving the sway modules broken while busy-looping on a dead socket and pegging a CPU. Mirror the niri backend's reconnect loop: on a read/EOF/parse error from the event socket, close the old connection, back off for a couple of seconds (so we don't busy-spin), re-open the socket and replay the same subscriptions, then resume. A running_ flag set at the start of teardown makes the worker bail out cleanly instead of reconnecting to a socket that is being closed on purpose. The IPC message protocol and event parsing are unchanged. Fixes #3166.
1 parent d9f2a43 commit 8279c2a

2 files changed

Lines changed: 57 additions & 5 deletions

File tree

include/modules/sway/ipc/client.hpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@
22

33
#include <sigc++/sigc++.h>
44

5+
#include <atomic>
56
#include <cstdint>
67
#include <functional>
78
#include <mutex>
89
#include <string>
10+
#include <vector>
911

1012
#include "ipc.hpp"
1113
#include "util/SafeSignal.hpp"
@@ -43,6 +45,14 @@ class Ipc {
4345
struct ipc_response send(int fd, uint32_t type, const std::string& payload = "");
4446
struct ipc_response recv(int fd);
4547

48+
// Re-establish the event socket and re-subscribe after sway drops us, backing
49+
// off between attempts so we don't busy-loop while sway is unavailable.
50+
void reconnectEvent();
51+
52+
std::string socketPath_;
53+
std::vector<std::string> subscribed_events_;
54+
std::atomic<bool> running_{true};
55+
4656
util::ScopedFd fd_;
4757
util::ScopedFd fd_event_;
4858
std::mutex mutex_;

src/modules/sway/ipc/client.cpp

Lines changed: 47 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,14 @@
88
#include <unistd.h>
99

1010
#include <cerrno>
11+
#include <chrono>
1112
#include <cstdio>
1213
#include <cstdlib>
1314
#include <cstring>
1415
#include <limits>
1516
#include <stdexcept>
1617
#include <string_view>
18+
#include <thread>
1719
#include <utility>
1820

1921
#include "modules/sway/ipc/ipc.hpp"
@@ -41,12 +43,15 @@ void sendAll(int fd, const char* data, size_t size, const char* what) {
4143
} // namespace
4244

4345
Ipc::Ipc() {
44-
const std::string socketPath = getSocketPath();
45-
fd_ = util::ScopedFd(open(socketPath));
46-
fd_event_ = util::ScopedFd(open(socketPath));
46+
socketPath_ = getSocketPath();
47+
fd_ = util::ScopedFd(open(socketPath_));
48+
fd_event_ = util::ScopedFd(open(socketPath_));
4749
}
4850

4951
Ipc::~Ipc() {
52+
// Signal the worker before stopping it so an in-flight recv/reconnect bails
53+
// out instead of trying to reconnect to a socket we're tearing down.
54+
running_ = false;
5055
thread_.stop();
5156

5257
if (fd_ > 0) {
@@ -191,11 +196,48 @@ void Ipc::subscribe(const std::string& payload) {
191196
if (res.payload != "{\"success\": true}") {
192197
throw std::runtime_error("Unable to subscribe ipc event");
193198
}
199+
// Remember the subscription so we can replay it if we have to reconnect.
200+
subscribed_events_.push_back(payload);
201+
}
202+
203+
void Ipc::reconnectEvent() {
204+
// Sway closed our event connection (typically because its send buffer filled
205+
// up during an event flood). Re-establish the socket and re-subscribe to the
206+
// same events, backing off between attempts so we don't busy-loop and peg a
207+
// CPU while sway is unavailable or keeps dropping us.
208+
while (running_) {
209+
std::this_thread::sleep_for(std::chrono::seconds(2));
210+
if (!running_) {
211+
return;
212+
}
213+
try {
214+
fd_event_.reset(open(socketPath_));
215+
for (const auto& payload : subscribed_events_) {
216+
const auto res = Ipc::send(fd_event_, IPC_SUBSCRIBE, payload);
217+
if (res.payload != "{\"success\": true}") {
218+
throw std::runtime_error("Unable to re-subscribe ipc event");
219+
}
220+
}
221+
spdlog::info("Reconnected to sway IPC event socket");
222+
return;
223+
} catch (const std::exception& e) {
224+
spdlog::warn("Failed to reconnect to sway IPC ({}), retrying", e.what());
225+
}
226+
}
194227
}
195228

196229
void Ipc::handleEvent() {
197-
const auto res = Ipc::recv(fd_event_);
198-
signal_event.emit(res);
230+
try {
231+
const auto res = Ipc::recv(fd_event_);
232+
signal_event.emit(res);
233+
} catch (const std::exception& e) {
234+
if (!running_) {
235+
// The Ipc is being torn down; the socket was closed on purpose.
236+
return;
237+
}
238+
spdlog::warn("Lost sway IPC event connection ({}), reconnecting", e.what());
239+
reconnectEvent();
240+
}
199241
}
200242

201243
} // namespace waybar::modules::sway

0 commit comments

Comments
 (0)