Skip to content

fix(rpc): read chunked responses from the bitcoind RPC server - #393

Open
eynhaender wants to merge 1 commit into
bitcoindevkit:masterfrom
eynhaender:fix/rpc-chunked-responses
Open

eynhaender wants to merge 1 commit into
bitcoindevkit:masterfrom
eynhaender:fix/rpc-chunked-responses

Conversation

@eynhaender

@eynhaender eynhaender commented Nov 23, 2026 •

Copy link
Copy Markdown

Description

Fixes #392.

--client-type rpc built its client with bitcoincore_rpc::Client::new, which always uses jsonrpc's simple_http transport and fails on any response sent with Transfer-Encoding: chunked.

This PR builds the client with Client::from_jsonrpc over jsonrpc's minreq_http transport, which reads both chunked and Content-Length responses. bitcoincore-rpc 0.19 already enables minreq_http, so Cargo.toml and Cargo.lock are unchanged.

Notes to the reviewers

  • minreq requires a URL scheme, while simple_http assumed http. http:// is therefore prepended when --url has none, so 127.0.0.1:18443 from the README keeps working.

  • Basic auth (--basic-auth, and the cookie file via Auth::get_user_pass) is passed to the minreq_http builder. Transport errors map to the existing Error::BitcoinCoreRpcError(bitcoincore_rpc::Error::JsonRpc(..)).

  • Tested manually on regtest with wallet full_scan and the same wpkh wallet against Bitcoin Core 32.1 and libbitcoin-server master d09a5c2a (bitcoind endpoint, which sends every response with Transfer-Encoding: chunked):

    binary server result
    master Bitcoin Core 32.1 11 txs
    master libbitcoin-server d09a5c2a fails: "The server replied with a chunked response which is not supported"
    this PR Bitcoin Core 32.1 11 txs
    this PR libbitcoin-server d09a5c2a (with and without http://) 11 txs

    In every run that succeeded, the txid set and the balance were identical.

  • There is no automated regression test: bdk_testenv's bitcoind only sends Content-Length responses. A test would need a mock HTTP server that sends chunked responses; I can add one if wanted.

Checklists

All Submissions:

Bugfixes:

  • I've added tests to reproduce the issue which are now passing
  • I'm linking the issue being fixed by this PR

@emma31-dev emma31-dev left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approach ACK.

Thank you for working on this. Merge master into this branch and rebase to one commit that i can ACK.

Comment thread CHANGELOG.md Outdated
- Fixed `key generate` silently producing a 24-word mnemonic for any `--entropy` value other than 12; values other than 12 and 24 are now rejected
- Gated `full_scan`'s `--scan-stop-gap` to the Electrum and Esplora backends that use it; RPC and CBF wallets now reject the flag instead of silently ignoring it, while a `STOP_GAP` environment variable keeps working for supported backends and no longer breaks unsupported ones
- Changed `payjoin history` to return JSON instead of a rendered table
- Fixed the `rpc` backend failing against servers that send chunked responses ("The server replied with a chunked response which is not supported"); the RPC client now uses jsonrpc's `minreq_http` transport

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The changelog entry can be simplified to this:

Suggested change
- Fixed the `rpc` backend failing against servers that send chunked responses ("The server replied with a chunked response which is not supported"); the RPC client now uses jsonrpc's `minreq_http` transport
- Fixed the `rpc` backend failing against servers that send chunked responses

Comment thread src/client.rs Outdated

let rpc_error =
|e: jsonrpc::Error| Error::BitcoinCoreRpcError(bitcoincore_rpc::Error::JsonRpc(e));
let (user, pass) = auth.get_user_pass().map_err(Error::BitcoinCoreRpcError)?;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NIT: .map_err can be safely removed

Suggested change
let (user, pass) = auth.get_user_pass().map_err(Error::BitcoinCoreRpcError)?;
let (user, pass) = auth.get_user_pass()?;

`bitcoincore_rpc::Client::new` always uses jsonrpc's `simple_http`
transport, which rejects responses sent with `Transfer-Encoding: chunked`
("The server replied with a chunked response which is not supported").
Servers that stream JSON-RPC responses chunked, such as libbitcoin-server,
cannot be used with `--client-type rpc`.

Build the RPC client over jsonrpc's `minreq_http` transport instead, which
reads chunked and content-length responses. bitcoincore-rpc 0.19 already
enables the `minreq_http` feature of jsonrpc, so no dependency changes.

minreq requires a URL scheme while simple_http defaulted to http, so
`http://` is prepended when the URL has none (e.g. `127.0.0.1:18443`).
@eynhaender
eynhaender force-pushed the fix/rpc-chunked-responses branch from 830780a to 0837bc1 Compare November 26, 2026 13:32
@eynhaender

Copy link
Copy Markdown
Author

Rebased on master as a single commit and applied both suggestions. CI is waiting for workflow approval. Thanks for your quick response, let's keep on building.

@emma31-dev emma31-dev left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ACK 0837bc1

Everything looks good and online commands still work as expected.

@codecov

codecov Bot commented Nov 30, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 80.00000% with 4 lines in your changes missing coverage. Please review.
✅ Project coverage is 59.94%. Comparing base (4b93fe6) to head (0837bc1).
⚠️ Report is 3 commits behind head on master.

Files with missing lines Patch % Lines
src/client.rs 80.00% 4 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master     #393      +/-   ##
==========================================
+ Coverage   59.87%   59.94%   +0.07%     
==========================================
  Files          25       25              
  Lines        4558     4576      +18     
==========================================
+ Hits         2729     2743      +14     
- Misses       1829     1833       +4     
Flag Coverage Δ
rust 59.94% <80.00%> (+0.07%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@tvpeter tvpeter left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you @eynhaender for the issue and this PR. It fixes the chunked response problem, but the transport swap introduces two problems that affect every RPC user, including Bitcoin Core users who never had the chunked response problem:

  • minreq opens a new TCP connection per request, which runs out of local ports on longer scans. simple_http keeps one socket open and reuses it. On regtest, wallet full_scan on this branch fails around 15,000 blocks with Can't assign requested address (os error 49) (16k sockets in TIME_WAIT), while master finishes in ~7s with one connection. bitreq has the same issue for blocking callers.
  • A URL with no scheme and no port now goes to port 80 instead of 8332. simple_http defaults to 8332 in that case. This PR prepends http://, so minreq uses port 80. --url 127.0.0.1 then misses bitcoind and sends the RPC credentials to whatever is listening on 80.

A better approach might be to implement jsonrpc's Transport trait over an HTTP client that both reuses connections and reads chunked responses (e.g. a ureq agent), then wrap it with Client::from_jsonrpc as this PR already does. I am open to alternatives that you might have.

Thank you.

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

Status: Todo

Development

Successfully merging this pull request may close these issues.

RPC backend fails against servers that send chunked responses

3 participants