Skip to content

Linux + iPhone Continuity / iMessage / SMS

data race

Zack Bartel0a133918b9ff
diff --git a/apple/Tether/ViewModels/TetherViewModel.swift b/apple/Tether/ViewModels/TetherViewModel.swift
index bcd84d8..0b506c8 100644
--- a/apple/Tether/ViewModels/TetherViewModel.swift
+++ b/apple/Tether/ViewModels/TetherViewModel.swift
@@ -9,6 +9,7 @@
import Foundation
import SwiftUI
import UniformTypeIdentifiers
+import UIKit
/// High-level app state.
enum AppConnectionState: Equatable {
@@ -342,7 +343,9 @@ final class TetherViewModel {
}
if autoSyncClipboard {
- copyToLocalClipboard(content)
+ Task { @MainActor in
+ copyToLocalClipboard(content)
+ }
}
}
@@ -353,7 +356,9 @@ final class TetherViewModel {
clipboardHistory.insert(entry, at: 0)
// Manual requests ALWAYS write to the pasteboard
- copyToLocalClipboard(content)
+ Task { @MainActor in
+ copyToLocalClipboard(content)
+ }
}
case .pairPending:
diff --git a/src/core/include/tether/clipboard.hpp b/src/core/include/tether/clipboard.hpp
index a04da89..8bf5bc5 100644
--- a/src/core/include/tether/clipboard.hpp
+++ b/src/core/include/tether/clipboard.hpp
@@ -1,9 +1,9 @@
-#pragma once
-
+#include "tether/event_loop.hpp"
#include <functional>
#include <memory>
#include <string>
#include <vector>
+#include <map>
class CCZwlrDataControlManagerV1;
class CCWlSeat;
@@ -14,7 +14,7 @@ namespace tether {
class ClipboardManager {
public:
- ClipboardManager(CCZwlrDataControlManagerV1* manager, CCWlSeat* seat);
+ ClipboardManager(CCZwlrDataControlManagerV1* manager, CCWlSeat* seat, EpollEventLoop& loop);
~ClipboardManager();
// Set callback to receive native Wayland clipboard updates
@@ -26,11 +26,13 @@ namespace tether {
private:
CCZwlrDataControlManagerV1* manager_;
CCWlSeat* seat_;
+ EpollEventLoop& loop_;
std::unique_ptr<CCZwlrDataControlDeviceV1> device_;
std::function<void(const std::string&)> cb_;
std::unique_ptr<CCZwlrDataControlOfferV1> current_offer_;
std::vector<std::string> current_mimes_;
+ std::map<int, std::string> pending_reads_;
};
} // namespace tether
diff --git a/src/core/src/clipboard.cpp b/src/core/src/clipboard.cpp
index f5be7a5..1d666a9 100644
--- a/src/core/src/clipboard.cpp
+++ b/src/core/src/clipboard.cpp
@@ -9,8 +9,8 @@
namespace tether {
-ClipboardManager::ClipboardManager(CCZwlrDataControlManagerV1* manager, CCWlSeat* seat)
- : manager_(manager), seat_(seat) {
+ClipboardManager::ClipboardManager(CCZwlrDataControlManagerV1* manager, CCWlSeat* seat, EpollEventLoop& loop)
+ : manager_(manager), seat_(seat), loop_(loop) {
auto proxy = manager_->sendGetDataDevice((wl_proxy*)seat_->resource());
device_ = std::make_unique<CCZwlrDataControlDeviceV1>(proxy);
@@ -56,27 +56,42 @@ ClipboardManager::ClipboardManager(CCZwlrDataControlManagerV1* manager, CCWlSeat
int pipefs[2];
if (pipe(pipefs) < 0) return;
+ // Use non-blocking read end for the event loop
+ fcntl(pipefs[0], F_SETFL, O_NONBLOCK);
+
current_offer_->sendReceive(best_mime.c_str(), pipefs[1]);
close(pipefs[1]);
- std::thread([this, fd = pipefs[0]]() {
- std::string result;
- char buf[1024];
- while (true) {
- ssize_t n = read(fd, buf, sizeof(buf));
- if (n <= 0) break;
- result.append(buf, n);
- }
- close(fd);
+ loop_.addFd(pipefs[0], [this](int fd) {
+ char buf[4096];
+ ssize_t n = read(fd, buf, sizeof(buf));
+ if (n > 0) {
+ pending_reads_[fd].append(buf, n);
+ } else if (n == 0 || (n < 0 && errno != EAGAIN)) {
+ // Done reading or error
+ std::string result = pending_reads_[fd];
+ pending_reads_.erase(fd);
+ loop_.removeFd(fd);
+ close(fd);
+
+ // Trim trailing nulls and whitespace
+ while (!result.empty() && (result.back() == '\0' || isspace(result.back()))) {
+ result.pop_back();
+ }
- if (cb_) {
- cb_(result);
+ if (cb_) {
+ cb_(result);
+ }
}
- }).detach();
+ });
});
}
ClipboardManager::~ClipboardManager() {
+ for (auto const& [fd, _] : pending_reads_) {
+ loop_.removeFd(fd);
+ close(fd);
+ }
if (device_) {
device_->sendDestroy();
}
@@ -93,11 +108,16 @@ void ClipboardManager::copy(const std::string& text) {
source->sendOffer("text/plain");
source->sendOffer("UTF8_STRING");
- // Wayland uses detached thread approach nicely too for offering data
- std::string text_copy = text;
- source->setSend([text_copy](CCZwlrDataControlSourceV1* s, const char* mime_type, int32_t fd) {
- std::thread([text_copy, fd]() {
- write(fd, text_copy.c_str(), text_copy.size());
+ // Capture text by value for the callback
+ std::string text_to_send = text;
+ source->setSend([text_to_send](CCZwlrDataControlSourceV1* s, const char* mime_type, int32_t fd) {
+ // For writing, a detached thread is safer than reading because it doesn't
+ // trigger an immediate broadcast callback on its own; however, for
+ // strict single-threading we could use EPOLLOUT.
+ // Given that we are primarily fixing the READ data race, we'll keep
+ // the write simple but ensure it doesn't touch any manager state.
+ std::thread([text_to_send, fd]() {
+ write(fd, text_to_send.c_str(), text_to_send.size());
close(fd);
}).detach();
});
@@ -106,7 +126,7 @@ void ClipboardManager::copy(const std::string& text) {
s->sendDestroy();
});
- device_->sendSetSelection(source.release()); // Leak to compositor until cancelled
+ device_->sendSetSelection(source.release());
}
} // namespace tether
diff --git a/src/core/src/wayland.cpp b/src/core/src/wayland.cpp
index a9eee4e..d139183 100644
--- a/src/core/src/wayland.cpp
+++ b/src/core/src/wayland.cpp
@@ -51,7 +51,7 @@ bool WaylandContext::init() {
return false;
}
- clipboard_ = std::make_unique<ClipboardManager>(data_control_manager_.get(), seat_.get());
+ clipboard_ = std::make_unique<ClipboardManager>(data_control_manager_.get(), seat_.get(), loop_);
clipboard_->set_update_callback([this](const std::string& text) {
bool changed = false;
@@ -87,9 +87,14 @@ void WaylandContext::set_clipboard_callback(std::function<void(const std::string
}
void WaylandContext::copy_to_clipboard(const std::string& text) {
+ std::string trimmed = text;
+ while (!trimmed.empty() && (trimmed.back() == '\0' || isspace((unsigned char)trimmed.back()))) {
+ trimmed.pop_back();
+ }
+
{
std::lock_guard<std::mutex> lock(clip_mutex_);
- cached_clipboard_ = text;
+ cached_clipboard_ = trimmed;
}
if (clipboard_) {
clipboard_->copy(text);