Repository: bitcoin
Issue: Assertion chainman().ActiveChain()[height] fails on startup on memory-constrained system
Labels: Bug,UTXO Db and Indexes,Data corruption

### Is there an existing issue for this?

- [X] I have searched the existing issues

### Current behaviour

bitcoind crashes on startup with this error: (not logged in debug.log)
```
node/interfaces.cpp:525 getBlockHash: Assertion `chainman().ActiveChain()[height]' failed.
Aborted (core dumped)
```
This is after trying to bring the node up after it was killed by the system during IBD (likely due to OOM).

### Expected behaviour

bitcoind should not crash, or at least it should exit with a more helpful error log.

### Steps to reproduce

Run bitcoind with these configuration options:
```
disablewallet=1
prune=550
listen=0
coinstatsindex=1
```

### Relevant log output

Last few lines before being killed (last line not recorded in debug.log):

```
2024-02-11T19:09:40Z UpdateTip: new best=00000000000005ab0d71214ffa5b38cd51e13525892a221bbf748fee8de806ad height=140916 version=0x00000001 log2_work=66.122154 tx=1282449 date='2011-08-14T11:18:30Z' progress=0.001328 cache=104.2MiB(896112txo)
2024-02-11T19:09:40Z UpdateTip: new best=0000000000000390717df9a1baaf8e65f9b96c87ee994d20dd897a95e8adc3e9 height=140917 version=0x00000001 log2_work=66.122300 tx=1282533 date='2011-08-14T11:27:11Z' progress=0.001328 cache=104.2MiB(896392txo)
2024-02-11T19:09:41Z UpdateTip: new best=000000000000038559aa65f99789894199f2f4f08bfba66454864b5e514ba58a height=140918 version=0x00000001 log2_work=66.122445 tx=1282552 date='2011-08-14T11:32:11Z' progress=0.001328 cache=104.2MiB(896387txo)
2024-02-11T19:09:41Z UpdateTip: new best=000000000000000121734a048a6dcd9f5184929ef6b4d60fab74612de0e5e26c height=140919 version=0x00000001 log2_work=66.122591 tx=1282659 date='2011-08-14T12:12:06Z' progress=0.001328 cache=104.2MiB(896508txo)
2024-02-11T19:09:41Z UpdateTip: new best=000000000000017fab19329089a1ab54e1f4ab110b1ef0c5d4f9990c2815d624 height=140920 version=0x00000001 log2_work=66.122737 tx=1282673 date='2011-08-14T12:16:33Z' progress=0.001328 cache=104.2MiB(896564txo)
2024-02-11T19:09:41Z UpdateTip: new best=00000000000006a0e9cbe57335835c382bc09f11045b68dbb834495f4ae22faf height=140921 version=0x00000001 log2_work=66.122882 tx=1282777 date='2011-08-14T12:41:24Z' progress=0.001328 cache=104.2MiB(896561txo)
2024-02-11T19:09:41Z UpdateTip: new best=000000000000052e30dd0b273b500fde21e4bee67d1f5cd05fef91a663dadfcd height=140922 version=0x00000001 log2_work=66.123028 tx=1282926 date='2011-08-14T12:47:38Z' progress=0.001329 cache=104.2MiB(896633txo)
Killed
```

After trying to start the node again (last two lines not recorded in debug.log):

```
2024-02-11T19:46:07Z Bitcoin Core version v26.0.0 (release build)
2024-02-11T19:46:07Z InitParameterInteraction: parameter interaction: -listen=0 -> setting -upnp=0
2024-02-11T19:46:07Z InitParameterInteraction: parameter interaction: -listen=0 -> setting -natpmp=0
2024-02-11T19:46:07Z InitParameterInteraction: parameter interaction: -listen=0 -> setting -discover=0
2024-02-11T19:46:07Z InitParameterInteraction: parameter interaction: -listen=0 -> setting -listenonion=0
2024-02-11T19:46:07Z InitParameterInteraction: parameter interaction: -listen=0 -> setting -i2pacceptincoming=0
2024-02-11T19:46:07Z Using the 'x86_shani(1way,2way)' SHA256 implementation
2024-02-11T19:46:07Z Using RdSeed as an additional entropy source
2024-02-11T19:46:07Z Using RdRand as an additional entropy source
2024-02-11T19:46:07Z Default data directory /home/***/.bitcoin
2024-02-11T19:46:07Z Using data directory /home/***/.bitcoin
2024-02-11T19:46:07Z Config file: /home/***/.bitcoin/bitcoin.conf
2024-02-11T19:46:07Z Config file arg: coinstatsindex="1"
2024-02-11T19:46:07Z Config file arg: disablewallet="1"
2024-02-11T19:46:07Z Config file arg: listen="0"
2024-02-11T19:46:07Z Config file arg: prune="550"
2024-02-11T19:46:07Z Using at most 125 automatic connections (1024 file descriptors available)
2024-02-11T19:46:07Z Using 16 MiB out of 16 MiB requested for signature cache, able to store 524288 elements
2024-02-11T19:46:07Z Using 16 MiB out of 16 MiB requested for script execution cache, able to store 524288 elements
2024-02-11T19:46:07Z Script verification uses 0 additional threads
2024-02-11T19:46:07Z Wallet disabled!
2024-02-11T19:46:07Z scheduler thread start
2024-02-11T19:46:07Z Binding RPC on address ::1 port 8332
2024-02-11T19:46:07Z Binding RPC on address 127.0.0.1 port 8332
2024-02-11T19:46:07Z [http] creating work queue of depth 16
2024-02-11T19:46:07Z Using random cookie authentication.
2024-02-11T19:46:07Z Generated RPC authentication cookie /home/***/.bitcoin/.cookie
2024-02-11T19:46:07Z [http] starting 4 worker threads
2024-02-11T19:46:07Z Using /16 prefix for IP bucketing
2024-02-11T19:46:07Z init message: Loading P2P addresses…
2024-02-11T19:46:07Z Loaded 8837 addresses from peers.dat  20ms
2024-02-11T19:46:07Z init message: Loading banlist…
2024-02-11T19:46:07Z SetNetworkActive: true
2024-02-11T19:46:07Z /home/***/.bitcoin/fee_estimates.dat is not found. Continue anyway.
2024-02-11T19:46:07Z Cache configuration:
2024-02-11T19:46:07Z * Using 2.0 MiB for block index database
2024-02-11T19:46:07Z * Using 8.0 MiB for chain state database
2024-02-11T19:46:07Z * Using 440.0 MiB for in-memory UTXO set (plus up to 286.1 MiB of unused mempool space)
2024-02-11T19:46:07Z init message: Loading block index…
2024-02-11T19:46:07Z Assuming ancestors of block 00000000000000000001a0a448d6cf2546b06801389cc030b2b18c6491266815 have valid signatures.
2024-02-11T19:46:07Z Setting nMinimumChainWork=000000000000000000000000000000000000000052b2559353df4117b7348b64
2024-02-11T19:46:07Z Prune configured to target 550 MiB on disk for block and undo files.
2024-02-11T19:46:07Z Opening LevelDB in /home/***/.bitcoin/blocks/index
2024-02-11T19:46:13Z Opened LevelDB successfully
2024-02-11T19:46:13Z Using obfuscation key for /home/***/.bitcoin/blocks/index: 0000000000000000
2024-02-11T19:46:17Z LoadBlockIndexDB: last block file = 3
2024-02-11T19:46:17Z LoadBlockIndexDB: last block file info: CBlockFileInfo(blocks=4007, size=94150965, heights=136926...140942, time=2011-07-18...2011-08-14)
2024-02-11T19:46:17Z Checking all blk files are present...
2024-02-11T19:46:17Z LoadBlockIndexDB(): Block files have previously been pruned
2024-02-11T19:46:17Z Initializing chainstate Chainstate [ibd] @ height -1 (null)
2024-02-11T19:46:17Z Opening LevelDB in /home/***/.bitcoin/chainstate
2024-02-11T19:46:17Z Opened LevelDB successfully
2024-02-11T19:46:17Z Using obfuscation key for /home/***/.bitcoin/chainstate: c31b181c8ff3ce9b
2024-02-11T19:46:17Z Opening LevelDB in /home/***/.bitcoin/chainstate
2024-02-11T19:46:17Z Opened LevelDB successfully
2024-02-11T19:46:17Z Using obfuscation key for /home/***/.bitcoin/chainstate: c31b181c8ff3ce9b
2024-02-11T19:46:17Z [Chainstate [ibd] @ height -1 (null)] resized coinsdb cache to 8.0 MiB
2024-02-11T19:46:17Z [Chainstate [ibd] @ height -1 (null)] resized coinstip cache to 440.0 MiB
2024-02-11T19:46:17Z init message: Verifying blocks…
2024-02-11T19:46:17Z  block index            9824ms
2024-02-11T19:46:17Z Opening LevelDB in /home/***/.bitcoin/indexes/coinstats/db
2024-02-11T19:46:17Z Opened LevelDB successfully
2024-02-11T19:46:17Z Using obfuscation key for /home/***/.bitcoin/indexes/coinstats/db: 0000000000000000
2024-02-11T19:46:17Z init message: Pruning blockstore…
2024-02-11T19:46:17Z initload thread start
node/interfaces.cpp:525 getBlockHash: Assertion `chainman().ActiveChain()[height]' failed.
Aborted (core dumped)
```

### How did you obtain Bitcoin Core

Pre-built binaries

### What version of Bitcoin Core are you using?

v26.0

### Operating system and version

Ubuntu 20.04.6 LTS

### Machine specifications

The system only has 1 GB of RAM.