{"id":"XE-0040","kind":"bounty","visibility":"public","source":"github","source_repo":"xeprotocol/xe","source_ref":"59","source_url":"https://github.com/xeprotocol/xe/issues/59","reporter_id":17,"title":"net/sync: no in-flight guard on sync rounds; ordinary first sync reaches 1.9 GB RSS and 2.7 cores, OOM on a 2 GB node (High)","area":"p2p-sync","claimed_tier":"high","assessed_tier":"high","verdict":"valid","duplicate_of":null,"tracked":true,"internal_ref":"#1052","internal_url":"https://github.com/xeprotocol/core/issues/1052","status":"awarded","closed_reason":null,"github_state":"open","disclosed_at":null,"award_xe":5000000000,"award_approved":true,"paid_ref":"","created_at":"2026-10-29T13:57:17.612038+00:00","updated_at":"2026-11-11T06:28:24.864113+00:00","source_created_at":"2026-10-26T07:33:50+00:00","told_at":null,"status_changed_at":"2026-11-06T19:57:20.110883+00:00","url":"https://bounty.xe.network/XE-0040","body":"Track 2 measurement: resource exhaustion during ordinary catch-up. No malformed input and no campaign load were used.\n\n## Summary\n\n`SetupSync` starts a `requestSync` goroutine on every new peer connection and again on every 10 second tick for each peer whose frontier is behind. The only backpressure is `syncRateLimiter`, a 5 second per-peer cooldown that limits starts, not concurrency. Each request may live for 60 seconds and buffers up to 10,000 decoded blocks before applying them. A round that cannot apply its blocks lasts tens of seconds, so later ticks start more rounds for the same peer while the previous ones are still running. On `testnet-0005` at commit `978785929bc1` a fresh node with six peers reached 75 concurrent `requestSync` goroutines, 1.86 GB RSS, 2.7 cores and 41.9 million goroutine IDs in under three minutes, while its data directory was only 252 MB. On a 2 GB VPS this is an OOM during first sync once additional peer IDs multiply the rounds, and the controlled run below reproduced it under a 2 GiB cgroup; on a larger host the node saturates cores and still does not converge, so the state repeats every tick.\n\n## Affected component\n\n- `net/sync.go:22-37`: `defaultPageSize = 64`, `maxPageSize = 256`, `maxTotalBlocks = 10000`, `maxServerBlocks = 10000`, `periodicSyncInterval = 10s`, `syncCooldown = 5s`, `maxSyncResponseBytes = 10 MiB`.\n- `net/sync.go:328-351` (`ConnectedF`): spawns `requestSync` for every connection, gated only by `outboundRL`.\n- `net/sync.go:363-387` (ticker): every 10 seconds spawns `requestSync` for each peer where `tracker.shouldSync` is true.\n- `net/sync.go:49-68`: `syncRateLimiter.allow` returns false only within 5 seconds of the previous start for the same peer. It keeps no in-flight state.\n- `net/sync.go:536-596`: the request accumulates `allBlocks` until 10,000 blocks, then applies them with up to 10 retry passes. `allBlocks` stays referenced until the function returns.\n- `net/sync.go:531`: the 60 second context derives from `context.Background()`.\n\n## Measurement\n\nTwo fresh runs, same host, same binary (master `978785929bc1`, `xe 9787859`). The data directory was removed before each run. The node used the two documented bootstrap dials with `--no-discovery --disable-mdns`. Samples were taken every 5 seconds from `/proc/<pid>/status`, `/metrics` and `/node`.\n\n| metric | idle (no peers) | run 1 | run 2 |\n| --- | --- | --- | --- |\n| duration | 15 s | 151 s | 120 s |\n| peers | 0 | 6 | 6 |\n| accounts | 1 | 4,617 | 4,379 |\n| blocks | 1 | 20,817 | 17,787 |\n| RSS | 34 MB | 1.82 GB at the last sample, 1.86 GB metric peak | 1.33 GB at the last sample, VmHWM 1.46 GB |\n| CPU | 1.9 % of one core | 2.72 cores | 2.73 cores |\n| live goroutines | 98 | 521 | 486 |\n| max goroutine ID | n/a | 41,938,183 | 32,581,639 |\n| goroutine IDs per second | n/a | ~238,000 | ~263,000 |\n| concurrent `requestSync` at SIGQUIT | n/a | 75 | 62 |\n| `hit maxTotalBlocks cap` log lines | 0 | 62 | 52 |\n| data directory | n/a | 252 MB | 157 MB |\n| API latency, median / max | ~2 ms | ~30 ms / 116 ms | ~31 ms / 132 ms |\n\nCPU is the delta of `utime + stime` at 100 Hz over the sampling window. Goroutine IDs per second are derived from the maximum ID in the SIGQUIT dump and the process age at that dump. The concurrent counts are from the dumps, 728 and 758 goroutines in total.\n\nThe heap is 6 to 9 times the on-disk data. The periodic path alone allows 60 s / 5 s = 12 starts per peer, so 72 rounds for six peers, and the connection callback adds one more per connection; this matches the observed 75 and 62. At 10,000 buffered blocks per round this is up to 720,000 block slots, or 1.4 to 1.9 GB at 2 to 2.6 KB per signed block, which is consistent with the measured peak. That last step is arithmetic from the caps, not a heap profile; the repository exposes no pprof endpoint.\n\n## Sybil amplification (measured)\n\nThe per-peer bound suggests that peer identities multiply the work. Three controlled runs used the same host, the same binary and a copied chain of 4,379 accounts and 17,787 blocks served by local nodes. Each source had a freshly generated `host.key`, so the victim saw eight distinct libp2p peer IDs; all sources shared the copied ledger identity, which the sync limiter does not key on. The victim ran in a cgroup v2 with `memory.max = 2 GiB` and no swap. The sources ran on loopback on the same 4 core host, so CPU was oversubscribed; the victim's own resource counters remain valid, but wall clock gaps reflect host contention.\n\n| case | peers seen | concurrent `requestSync` at dump | peak RSS / VmHWM | CPU | outcome |\n| --- | --- | --- | --- | --- | --- |\n| 1 source | 1 | 3 | 212 MB | 0.36 cores | survived 87 s |\n| 8 sources | 8 | 110 | 1.23 GB | 1.31 cores | survived 147 s |\n| 8 sources plus public network | 15 (8 local + 7 public) | no dump | 1.40 GB last readable VmHWM | 1.31 cores to the last sample | cgroup OOM killed at 157 s, `oom_kill=1` |\n\nWith the same chain, eight peer IDs produced 37 times the concurrent rounds and 5.8 times the memory of one. The eight-source case stayed below 2 GiB because the copied chain is static and stops offering new blocks. Adding the live public chain and its peers crossed the limit: the victim was killed by the cgroup OOM killer, the last readable sample had 3,211 accounts and 11,206 blocks, and the log shows repeated `hit maxTotalBlocks cap (10000)` immediately before the kill.\n\n## Reproduction\n\n1. Build master `978785929bc1`.\n2. Start a node with a fresh data directory:\n\n```\n./xe node --genesis-dir ./genesis/testnet-0005 --data ./data \\\n  --api-port 28100 --metrics-addr 127.0.0.1:29120 --port 29020 \\\n  --no-discovery --disable-mdns \\\n  --dial <bootstrap-ams>,<bootstrap-nyc>\n```\n\n3. Every 5 seconds record `VmRSS` and `VmHWM` from `/proc/<pid>/status`, `go_goroutines` and `process_resident_memory_bytes` from `/metrics`, and `accounts` and `block_count` from `/node`.\n4. After 120 to 150 seconds send SIGQUIT and count `requestSync` frames in the dump.\n\nObserved in both runs: 62 and 75 concurrent `requestSync`, 1.46 and 1.86 GB peak, 2.7 cores, and rounds that end with `5,343` to `5,980` blocks `could not be added after retries`.\n\n## Why the rounds overlap\n\nA round ends only after it has applied its page or exhausted the retries. In these runs the client hit the 10,000 block cap dozens of times, and 5,343 to 5,980 blocks per round failed with `signature: unopened account ... has no declared signature`. `isRetryableError` keeps those blocks pending, so the round spends all 10 passes and often the full 60 second deadline. Meanwhile `syncCooldown` has already allowed two to twelve new starts for the same peer. `shouldSync` compares frontiers, and the frontier does not move for blocks that fail, so every tick qualifies again.\n\n## Impact\n\nOnboarding: the documented bootstrap path alone reached 1.86 GB RSS and 2.7 of 4 cores on this host in 151 seconds, before the node reached the network's account count, and did not converge. Under a 2 GiB cgroup, the same public sync plus eight source peer IDs was OOM-killed at 157 seconds. The API stayed responsive in these runs, so the failure is resource saturation rather than API starvation; on a smaller host the process dies first.\n\nThe bound is per peer, not global. Six peers means 12 rounds per peer ID, and the first connection of a new peer ID always passes the cooldown. The controlled runs above show the multiplier: eight source peer IDs on one host raised the concurrent rounds from 3 to 110, and adding those eight to a victim that was also syncing the live public network took the process past the 2 GiB cgroup limit in 157 seconds. A peer that keeps advertising a frontier the node cannot apply, or several peer IDs doing so, can hold the node at this level.\n\n## Suggested severity\n\nHigh. Ordinary first sync of the public testnet reaches 1.86 GB RSS and 2.7 of 4 cores with no malicious input, and adding eight peer IDs that serve an ordinary chain OOM-kills the process under a 2 GiB cgroup in 157 seconds. This is not the single decoded page described in #44: every round here stays within the 10,000 block cap, and that cap is what multiplies across overlapping rounds.\n\n## Suggested fix\n\n- Single-flight per peer: skip the periodic start while a request for that peer is active. The tracker already stores per-peer state.\n- Add a global semaphore or byte budget for concurrent rounds and buffered blocks, not only a per-peer start cooldown.\n- Apply pages as they are decoded instead of accumulating up to 10,000 blocks.\n- Bound the buffer by bytes with `maxSyncResponseBytes`, and drop blocks that are already known to be retryable and pending.\n- Derive the request context from the node context so shutdown cancels these rounds.\n\nXE payout address: will be provided in a follow-up comment.\n","patch_refs":[{"id":164,"repo":"xeprotocol/xe","kind":"pr","ref":"146","title":"fix(net): add in-flight guard, single-flight per peer, and global concurrency cap on sync (#59)","url":"https://github.com/xeprotocol/xe/pull/146","note":"","state":"open","author":"arturbarszcz78","added_by":"desk","created_at":"2026-10-29T13:57:30.091343+00:00"},{"id":224,"repo":"xeprotocol/core","kind":"pr","ref":"1110","title":"","url":"https://github.com/xeprotocol/core/pull/1110","note":"","state":"","author":"","added_by":"token:4","created_at":"2026-10-30T14:20:23.138130+00:00"}],"thread":[{"id":15,"source":"github","source_ref":"6410749199","author":"senastor","team":false,"body":"XE payout address: 3af6fd8452352903a845a4b99a0f852a9f944bbab4afd6cfaab93cfb11510a9a","created_at":"2026-10-26T07:37:58+00:00"}]}