Skip to content

cln-rpc: verify response id and recover cleanly from cancelled calls - #9763

Open
daywalker90 wants to merge 1 commit into
ElementsProject:masterfrom
daywalker90:cln-rpc-fix-id-and-cancel-safety
Open

daywalker90 wants to merge 1 commit into
ElementsProject:masterfrom
daywalker90:cln-rpc-fix-id-and-cancel-safety

Conversation

@daywalker90

Copy link
Copy Markdown
Collaborator

ClnRpc::call_raw_request incremented next_id on every request but never checked the id field of the response read from the socket. If a call was cancelled mid-read (e.g., via tokio::time::timeout or tokio::select!), its delayed response stayed in the Unix socket buffer and was silently returned to the next RPC call, permanently shifting all subsequent responses off by one. Similarly, cancelling mid-write could leave a partially flushed frame buffered in FramedWrite, and socket read errors were discarded with |_|.

Track incomplete writes with dirty_write and flush any buffered frame before sending the next request, verify msg["id"] against the sent id, discard stale responses from earlier cancelled numeric request IDs (got < expected) and notifications without an id, return an error on unexpected/mismatched IDs, and include the underlying read error in the returned RpcError.

Fix the mismatched mock response "id": "1" in test::call_raw_request and add regression tests for post-cancellation desync, out-of-order stale responses, unexpected response IDs, and mid-write cancellation.

Changelog-Fixed: cln-rpc: verify JSON-RPC response IDs and flush interrupted writes so cancelling an RPC future (e.g., via timeout or select!) no longer desynchronizes subsequent calls on the same ClnRpc.

`ClnRpc::call_raw_request` incremented `next_id` on every request but
never checked the `id` field of the response read from the socket. If a
call was cancelled mid-read (e.g., via `tokio::time::timeout` or
`tokio::select!`), its delayed response stayed in the Unix socket buffer
and was silently returned to the next RPC call, permanently shifting all
subsequent responses off by one. Similarly, cancelling mid-write could
leave a partially flushed frame buffered in `FramedWrite`, and socket
read errors were discarded with `|_|`.

Track incomplete writes with `dirty_write` and flush any buffered frame
before sending the next request, verify `msg["id"]` against the sent
`id`, discard stale responses from earlier cancelled numeric request IDs
(`got < expected`) and notifications without an `id`, return an error on
unexpected/mismatched IDs, and include the underlying read error in the
returned `RpcError`.

Fix the mismatched mock response `"id": "1"` in `test::call_raw_request`
and add regression tests for post-cancellation desync, out-of-order
stale responses, unexpected response IDs, and mid-write cancellation.

Changelog-Fixed: cln-rpc: verify JSON-RPC response IDs and flush interrupted writes so cancelling an RPC future (e.g., via `timeout` or `select!`) no longer desynchronizes subsequent calls on the same `ClnRpc`.
@daywalker90
daywalker90 requested a review from cdecker as a code owner November 24, 2026 16:34

This branch has not been deployed

No deployments
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