Files
wine-access/reader_events.cpp
deedra 7d3ac09ea1 Keep Wine editor feedback responsive during reader reply stalls
PresentAccessibleEvent previously waited synchronously for the reader on
Wine Access's message-loop thread. A slow D-Bus acknowledgment prevented
that thread from receiving subsequent WM_COPYDATA editor notifications.
The hooks time out after 50 ms, so feedback disappeared even though the
application accepted the input. With a 350 ms delayed focus reply, the
original helper inserted all of abbacdef but forwarded only e and f.

Move menu, editor and standard-control event delivery to one reader worker.
Keep a FIFO of at most 128 pending events, retain each variant and the bus
connection until delivery completes, and preserve focus-before-text order.
Keep the 500 ms reply deadline and cancel outstanding calls during normal
shutdown without making the event thread wait for the reader. Event-thread
success now means queued; controller RPC calls retain their synchronous
reader status behavior.

Retain the most recent focus dependency on the worker. After a transport
failure, re-establish that focus before sending dependent editor actions.
Treat a successful false reply as a completed exchange because filtering
can suppress presentation while still establishing focus. Never replay a
timed-out text action. Report transport failures, recovery and queue
overflow without including event contents in diagnostics.

Add a native editor fixture and a private-session D-Bus integration runner
for delayed replies, reply timeouts, failed initial focus, repeated typing,
event order, unchanged native text/caret, and shutdown with a withheld
reply. Wait for editor-hook attachment before triggering typing so the
32-bit companion is exercised reliably. Document queue behavior, limits,
regression commands and remaining live acceptance checks.

Validation performed sequentially on a private Xorg dummy display with
disposable Wine prefixes and recording reader services:

- cmake --build build --parallel
- python3 tests/reader_stall_integration.py --build-dir build
- The reader-stall check with --32-bit, --timeout, --focus-error, and
  --32-bit --focus-error
- ctest --test-dir build --output-on-failure: all four checks passed
- python3 tests/menu_integration.py --build-dir build with --fixture menu,
  edit, edit --32-bit, edit --restart-helper, notepad, controls, and
  list --32-bit
- python3 tests/event_stall_integration.py --build-dir build
- python3 tests/controller_integration.py --build-dir build
- git diff --check

The delayed-reply regression failed against the original synchronous
helper and passed after the worker change. The failed-focus scenario also
failed before dependency recovery was added and passed afterward.

These recordings establish the shared failure paths, not the cause of
every live MUSHclient pause or audible speech and physical braille
acceptance. Live checks remain for command typing, incoming plugin speech,
output-buffer arrows, menus, switching between Wine and Linux, reader
restart, and protected fields. An unavailable reader can still cause
feedback loss or bounded queue overflow.
2026-10-13 05:26:11 -07:00

137 lines
5.0 KiB
C++

// SPDX-License-Identifier: LGPL-2.1-or-later
#include "reader_events.h"
#include <condition_variable>
#include <cstdio>
#include <deque>
#include <mutex>
#include <string>
struct ReaderEventQueue::State {
State(GDBusConnection *bus, const ReaderEndpoint &reader)
: connection(G_DBUS_CONNECTION(g_object_ref(bus))), endpoint(reader),
calls(g_cancellable_new()) {}
~State() {
clear();
g_object_unref(calls);
g_object_unref(connection);
}
GDBusConnection *connection;
const ReaderEndpoint endpoint;
GCancellable *calls;
std::mutex mutex;
std::condition_variable changed;
std::deque<GVariant *> pending;
bool stopping = false, overflowReported = false;
void clear() {
for (auto *parameters : pending) g_variant_unref(parameters);
pending.clear();
}
bool send(GVariant *parameters, unsigned &failures) {
GError *error = nullptr;
GVariant *reply = g_dbus_connection_call_sync(
connection, endpoint.busName, endpoint.objectPath,
endpoint.interfaceName, "PresentAccessibleEvent", parameters,
G_VARIANT_TYPE("(b)"), G_DBUS_CALL_FLAGS_NONE, 500, calls, &error);
// A false reply can simply mean filtered/duplicate feedback. Only a
// failed exchange warrants diagnostics. Never include event contents.
const bool delivered = reply != nullptr;
if (!delivered && !g_cancellable_is_cancelled(calls)) {
if (++failures == 1)
std::fprintf(stderr, "wine-access: reader event reply failed (domain %s, code %d)\n",
error ? g_quark_to_string(error->domain) : "unknown", error ? error->code : 0);
} else if (delivered && failures) {
std::fprintf(stderr, "wine-access: reader event replies resumed after %u failed calls\n", failures);
failures = 0;
}
if (reply) g_variant_unref(reply);
if (error) g_error_free(error);
return delivered;
}
static DWORD WINAPI work(LPVOID pointer) {
std::unique_ptr<std::shared_ptr<State>> retained(
static_cast<std::shared_ptr<State> *>(pointer));
auto state = *retained;
unsigned failures = 0;
std::unique_ptr<GVariant, decltype(&g_variant_unref)> focus(nullptr, g_variant_unref);
std::string focusSource;
bool focusDelivered = false;
for (;;) {
GVariant *parameters;
{
std::unique_lock<std::mutex> lock(state->mutex);
state->changed.wait(lock, [&] { return state->stopping || !state->pending.empty(); });
if (state->stopping) break;
parameters = state->pending.front();
state->pending.pop_front();
}
const char *source = nullptr, *kind = nullptr;
g_variant_get_child(parameters, 0, "&s", &source);
g_variant_get_child(parameters, 1, "&s", &kind);
if (std::string(kind) == "focus") {
focus.reset(g_variant_ref(parameters));
focusSource = source;
focusDelivered = state->send(parameters, failures);
} else {
// A lost focus exchange must not strand later editor actions. Replay
// only the focus dependency, never a timed-out ordered text action.
if (std::string(kind) == "text" && !focusDelivered && focus && focusSource == source)
focusDelivered = state->send(focus.get(), failures);
if (std::string(kind) != "text" || focusDelivered) {
if (!state->send(parameters, failures)) focusDelivered = false;
}
}
g_variant_unref(parameters);
}
return 0;
}
};
ReaderEventQueue::~ReaderEventQueue() { stop(); }
bool ReaderEventQueue::start(GDBusConnection *connection, const ReaderEndpoint &endpoint) {
if (state_) return false;
state_ = std::make_shared<State>(connection, endpoint);
auto retained = new std::shared_ptr<State>(state_);
worker_ = CreateThread(nullptr, 0, State::work, retained, 0, nullptr);
if (!worker_) { delete retained; state_.reset(); return false; }
return true;
}
bool ReaderEventQueue::submit(GVariant *parameters) {
parameters = g_variant_ref_sink(parameters);
if (state_) {
std::lock_guard<std::mutex> lock(state_->mutex);
// Keep memory bounded if the reader remains unavailable. Do not reorder
// actions or evict the focus on which queued editor actions depend.
if (!state_->stopping && state_->pending.size() < 128) {
state_->pending.push_back(parameters);
state_->changed.notify_one();
return true;
}
if (!state_->stopping && !state_->overflowReported) {
std::fprintf(stderr, "wine-access: reader event queue full; feedback dropped\n");
state_->overflowReported = true;
}
}
g_variant_unref(parameters);
return false;
}
void ReaderEventQueue::stop() {
if (!state_) return;
{
std::lock_guard<std::mutex> lock(state_->mutex);
state_->stopping = true;
state_->clear();
state_->changed.notify_one();
}
g_cancellable_cancel(state_->calls);
// The worker retains its connection and queue state if cancellation has
// not completed yet; it never references event-thread globals.
WaitForSingleObject(worker_, 100);
CloseHandle(worker_);
worker_ = nullptr;
state_.reset();
}