<!-- Describe the issue -->
Description
It is possible to consistently cause a bitcoind process to be OOM killed on Linux (and probably termed on other operating systems) by making excessively large batched JSON RPC requests. There is currently no configurable way to limit batch sizes, so this can be done to any bitcoind instance providing the JSON RPC interfaces.
<!--- What behavior did you expect? -->
Expected Behavior
The server responds with a "batch too large" error or equivalent whenever a configured batch size limit is reached.
<!--- What was the actual behavior (provide screenshots if the issue is GUI-related)? -->
Actual Behavior
Bitcoind attempts to satisfy the request until it exhausts all available memory and is then OOM killed by the Linux kernel.
<!--- How reliably can you reproduce the issue, what are the steps to do so? -->
Reproduction
This issue can be easily reproduced under the following conditions:
- Create a fresh Bitcoin Docker image using Ubuntu Linux.
- Run the image with
-p 8332:8332 -p 8333:8333 -m=100mto restrict the available memory to 100MB and expose RPC and chatter protocols. - Run
bitcoindwith the following flags:-testnet -rpcuser=<user> -rpcpassword=<pass> -rpcbind=0.0.0.0 -rpcallowip=0.0.0.0/0 - Wait for the first ~25,000 testnet blocks to sync.
- Execute JSON RPC batch request to
getblockhashfor the first 20,000 block hashes - Execute JSON RPC batch request
getblock(<hash>, 2)for the first 20,000 block hashes, requesting all 20,000 blocks and included transactions in one batch response. - Bitcoin process will eventually exhaust memory and be terminated.
The bitcoind process will be OOM killed by the Linux kernel:

The socket on the RPC request will EOF:

Looking in the system logs you will see something like:
Out of memory: kill process 1984 (bitcoind) score 910 or sacrifice child
<snip>
bitcoin-httpwor invoked oom-killer: gfp_mask=0x14201ca(GFP_HIGHUSER_MOVABLE|__GFP_COLD), nodemask=(null), order=0, oom_score_adj=0
<!-- What version of Bitcoin Core are you using, where did you get it (website, self-compiled, etc)? -->
<!-- What type of machine are you observing the error on (OS/CPU and disk type)? -->
Machine Type
This works on all AWS instance types running Gentoo or Ubuntu Linux, and should be reproducible on any Linux instance type that has the OOM killer enabled.
<!-- Any extra information that might be useful in the debugging process. -->
<!--- This is normally the contents of a `debug.log` or `config.log` file. Raw text or a link to a pastebin type site are preferred. -->
References
- Respite from the OOM killer - Good overview of the Linux OOM killer and the security issues it can cause.
- Slides: Avoid OOM On Embedded Linux - Overview of Linux kernel overcommit and issues it can cause in embedded systems.