Skip to content

rpc: fix sendrawtransaction hang when tx is already in mempool - #2990

Open
Liquid369 wants to merge 1 commit into
PIVX-Project:masterfrom
Liquid369:2026_fix_sendrawtransaction_mempool_hang
Open

Liquid369 wants to merge 1 commit into
PIVX-Project:masterfrom
Liquid369:2026_fix_sendrawtransaction_mempool_hang

Conversation

@Liquid369

Copy link
Copy Markdown
Member

Problem

TryATMP sets its promise in the accepted branch only. A rejected tx throws, a tx already
in the chain throws, but a tx already in the mempool matches neither branch and falls
through to promise.get_future().wait() with nothing left to set the promise. That RPC
thread is lost for the life of the process.

-rpcthreads defaults to 4 (DEFAULT_HTTP_THREADS, httpserver.h:13) and HTTP workers run
each work item synchronously (httpserver.cpp:118), so four duplicate broadcasts end RPC
service on the node. Shutdown stops working too: StopHTTPServer joins every worker
(httpserver.cpp:461-465), so the node needs SIGKILL.

The wait sits outside cs_main, so block validation keeps running. Only RPC is lost.

Reachable from two callers, sendrawtransaction and protx registration via
rpcevo.cpp:383.

Origin

7d0599733a cherry-picked bitcoin#11117, which introduced the promise and the wait.
bitcoin#11738 fixed the duplicate-broadcast hang that mechanism creates and was never
cherry-picked. This restores that branch.

Why the call is direct

promise.set_value() is called directly rather than through
CallFunctionInValidationInterfaceQueue, matching bitcoin#11738. This branch accepted
nothing, so it has no validation callback of its own to await. Queueing the wake would hold
the duplicate broadcast behind any callback still pending from the original insertion
(TransactionAddedToMempool, validation.cpp:627).

Test

rpc_rawtransaction.py re-broadcasts a tx still in the mempool and asserts the txid comes
back.

result
with the fix passes, 33s
without the fix fails, 1230s

Without the fix the RPC hits rpc_timewait at 600s, then teardown fails again with
Unable to stop node. The duplicate send uses its own 30s proxy so a regression costs 30s
to spot rather than 600s. Cost: a client timeout does not cancel the server-side handler,
so teardown stays slow under regression.

Production

Reported 2026-11-09 to 2026-11-14. Both PIVX electrum servers froze for about 25h after
swap claims were broadcast to each of them, the second copy finding the tx already relayed
over P2P. Swaps failed. Worked around in ElectrumX by probing getrawtransaction before
broadcasting, which is a workaround and not a fix: any wallet or service that re-sends a
known tx can still wedge a thread.

TryATMP sets its promise in the accepted branch only. A rejected tx throws and a tx
already in the chain throws, but a tx already in the mempool matches neither branch and
reaches promise.get_future().wait() with nothing left to set the promise, parking that RPC
thread for the life of the process. -rpcthreads defaults to 4 and HTTP workers run each
item synchronously, so four duplicate broadcasts end RPC service on the node. Shutdown
hangs after that too, because StopHTTPServer joins every worker.

PIVX cherry-picked bitcoin#11117 as 7d05997, which introduced the promise and the wait.
bitcoin#11738 fixed the duplicate-broadcast hang that same mechanism creates and was never
cherry-picked. This restores that branch.

promise.set_value() is called directly rather than through
CallFunctionInValidationInterfaceQueue because this branch accepted nothing and so has no
validation callback of its own to await. Queueing the wake would hold the duplicate
broadcast behind any callback still pending from the tx's original insertion.

rpc_rawtransaction.py re-broadcasts a tx still in the mempool and asserts the txid comes
back. Without the fix that test fails in 1230s, hitting rpc_timewait at 600s and then
failing again because the node cannot be stopped. With it, 37s. The duplicate send uses
its own 30s proxy so a regression is cheap to spot.

Reported in production 2026-11-09 to 2026-11-14: both PIVX electrum servers froze for
about 25h after swap claims were broadcast to each of them, the second copy finding the tx
already relayed over P2P. Worked around in ElectrumX bd47967, which probes with
getrawtransaction before broadcasting.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant