8 pull requests in 48 hours: what I fixed in Monero's light-wallet server

My first contribution to monero-lws was eight pull requests, and all eight were merged within two days. Here is what was broken, how I found it, and what the maintainer's reviews taught me about getting fixes accepted upstream.
What monero-lws is
monero-lws is the Monero Light Wallet Server: it scans the chain for the view keys of many accounts at once, keeps the results in an LMDB database, and serves them to wallets through the MyMonero API. The wallet does not have to scan the chain itself. The server has to keep every account moving, so a stall or a lost index entry is a real bug for its users.
I run Dhiva Labs as a solo developer. I decided to improve this server upstream instead of writing my own. I cloned it, built it in Debug and Release, and went through the issue tracker for bugs I could reproduce. The eight PRs are #307 to #314, all against develop.
The timeout that grew: #311
The most satisfying bug was in do_wait() in src/rpc/client.cpp. It waits on a ZeroMQ socket with zmq_poll(), and when a signal interrupts the poll (EINTR) it retries with whatever time is left. The code that computed the time spent looked like this:
const auto start = std::chrono::steady_clock::now(); const int ready = zmq_poll(items, 2, timeout.count()); const auto end = std::chrono::steady_clock::now(); const auto spent = std::chrono::duration_cast<std::chrono::milliseconds>(start - end); timeout -= std::min(spent, timeout); Read the subtraction again: start - end. That is negative. So timeout -= spent made the remaining timeout larger after every interrupted poll. As long as signals kept arriving, the wait never ran out. The line had been there since the initial commit, and it only mattered on EINTR, which is why nobody had hit it in normal use.
My first fix was to swap the operands. The reviewer asked me to take start once before the loop, so each poll gets timeout minus the time since the first call. The merged diff:
+ // retries (EINTR, ignored signals) wait only for what is left of `timeout` + const auto start = std::chrono::steady_clock::now(); for (;;) { - const auto start = std::chrono::steady_clock::now(); - const int ready = zmq_poll(items, 2, timeout.count()); - const auto end = std::chrono::steady_clock::now(); - const auto spent = std::chrono::duration_cast<std::chrono::milliseconds>(start - end); - timeout -= std::min(spent, timeout); + const auto spent = + std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::steady_clock::now() - start); + const int ready = zmq_poll(items, 2, (timeout - std::min(spent, timeout)).count()); The test matters as much as the fix. It installs a no-op SIGUSR1 handler, queues a request on the client's REQ socket so get_message() has something to wait for, and then sends SIGUSR1 to the waiting thread every 50 ms for two seconds, while get_message() waits one second for a reply that never comes. The expectation is daemon_timeout in under 2.5 seconds. Before the fix the call took 5,109 ms. After it, the test passes in under 2.5 seconds.
New accounts that waited for the next block: #310
Issue #306 reported that new accounts showed zero outputs. I reproduced a stall on regtest. A scan thread that is already at the tip sits in wait_for_block(), and an account pushed to that thread was not read until the next block arrived, or until the four-minute --sub timeout. On a busy server every thread already has accounts, so every new account waited. In my run it took three minutes and one hand-mined block before the account was picked up.
My first version polled: it waited in one-second slices and checked a queue. The maintainer requested changes the same day. No polling, he said; use the ZMQ signalling that is already there. He was right, and I reworked it that day. Now the process publishes an "ACCOUNT" message on the internal signal socket, and scan threads opt in to it:
constexpr const char account_change_signal[] = "ACCOUNT"; case 'A': if (account_changes) return {lws::error::account_list_change}; continue; // only `wait_for_block` reports this Only wait_for_block reports the change; get_message and send ignore it. After the change, the same repro showed the new account being scanned 7.4 seconds after /login, bounded by the existing ten-second account poll.
One honest limit: I never reproduced missed outputs, only the stall. I said so in the PR and let the reporter confirm.
The root cause of issue #225: #312
Issue #225 is an MDB_NOTFOUND error ("Failed to update accounts on disk") after a large resync, with hundreds of accounts stuck at random heights. The cause I found is in change_height, used by rescan and import. It lowered the account's scan_height with std::min, but then re-inserted the height index entry at the requested height:
- user->scan_height = std::min(height, user->scan_height); + const block_id new_height = std::min(height, current_height); + user->scan_height = new_height; - key = lmdb::to_val(height); + // index must match `scan_height`, which never moves forward here + key = lmdb::to_val(new_height); When the requested height is above the account's current one, the account row and its index entry disagree. A later lookup by scan_height finds nothing. Two new test sections, rescan and import above scan_height, fail without the fix. Databases that are already inconsistent are not repaired by this change; the PR says so and offers a repair path.
Assert-only bugs: #307 and #314
Two bugs only showed up in a Debug build, because upstream CI built Release only, where assert compiles away. In #307 one assert used a variable name that no longer existed, and another was a bogus element-count check:
- assert(sizeof(G) + uoffset <= end - pos); - assert(thread_count || !lws_server_addr.empty()); + assert(thread_count || !lws_server.address.empty()); Fixing the code was half the job. #314 adds a CI job that builds Debug and runs the unit tests, so the next one is caught automatically. One detail from its comment: shared libraries, the Debug default, do not link here, so the job passes -DBUILD_SHARED_LIBS=OFF.
The other three: #308, #309, #313
308 stops the /feed WebSocket from starting two async_close calls on one stream, which happened when shutting down the subscriber re-entered close_check from zmq_loop. #309 fixes issue #279 by decrypting encrypted payment IDs per account, with a new unit test; on review the maintainer asked for the test to move to tests/unit/util, which I did, making the helper an object library so the static linker keeps the test case. #313 adds webhook_add and webhook_list to monero-lws-admin, so webhooks can be managed from the command line.
What I would tell anyone sending fixes upstream
- Build the configuration CI does not. Both #307 and #314 exist because I ran the tests in Debug before every PR. Upstream had a blind spot, and I only found it by looking where CI did not.
- Write the test that fails first. The EINTR test turns a vague "timeouts feel long" into 5,109 ms against a 2.5 second limit. Maintainers merge numbers faster than opinions.
- State what you did not reproduce. In #310 I said I saw the stall, not the missed outputs. It built trust for the review.
- Take the review as the design. The polling version worked. The ZMQ version fits the codebase. Reworking it the same day kept the PR moving.
- Keep each PR to one bug. Eight focused PRs were reviewed and merged in two days, each one easy to judge on its own.
- Answer in the thread, push extra commits. The maintainer squash-merges, so a follow-up commit is cleaner than rewriting history.
The code is public in vtnerd/monero-lws, and the eight PRs are #307 to #314.
I wrote this with help from an AI assistant and checked every claim and code excerpt against the source.

