Skip to content

fix: bound block-search volume so a large !searchForBlock can't stall the connection#795

Open
atiweb wants to merge 1 commit into
mindcraft-bots:developfrom
atiweb:fix/bound-block-search-volume
Open

fix: bound block-search volume so a large !searchForBlock can't stall the connection#795
atiweb wants to merge 1 commit into
mindcraft-bots:developfrom
atiweb:fix/bound-block-search-volume

Conversation

@atiweb

@atiweb atiweb commented Jun 17, 2026

Copy link
Copy Markdown
Contributor

Problem

getNearestBlocksWhere (and getNearestBlocks, which wraps it) calls bot.findBlocks synchronously. findBlocks scans a volume that grows with the cube of maxDistance, and with the default count of 10000 it does not early-exit when the target block is rare or absent — it walks the entire volume on the main thread.

The !searchForBlock command exposes a search_range of up to 512 to the model. When the model asks for a large radius while looking for something that isn't nearby (diamonds, a specific log type, an ore vein), that synchronous scan blocks the Node event loop long enough that mineflayer can't answer keep-alive packets — and the server drops the bot mid-task. From the outside it looks like the bot randomly freezes or "crashes" during normal play.

Fix

Cap the search inside getNearestBlocksWhere:

  • maxDistance128 — a search past the loaded view distance returns nothing useful anyway, so this doesn't drop real results.
  • count4000 — a lower cap only makes findBlocks early-exit sooner; it can never return fewer results than a caller actually consumes.

…and lower !searchForBlock's search_range domain max from 512 to 128 to match, so the model can't request a radius the engine would only clamp.

Why it's safe

  • findBlocks already only returns blocks within loaded chunks; beyond the view distance there is nothing to find, so capping the radius at 128 does not change results in practice.
  • Capping count downward is strictly less work (it's an upper bound on matches collected). 4000 hits within a 128-block radius is far more than any current caller uses.

Scope

Two files, +14 / −3. No new dependencies; no behavior change other than the bound.

bot.findBlocks is synchronous and scans a volume that grows with the cube of
maxDistance. With the default count (10000) a search for a rare or absent block
never early-exits, so a large radius walks a huge volume on the main thread and
blocks the Node event loop long enough for mineflayer to miss keep-alive packets
-- the server then drops the bot mid-task. !searchForBlock exposes a radius up to
512 to the model, so this is reachable in normal play.

Cap the search radius at 128 (a search beyond the loaded view distance returns
nothing useful anyway) and the match count at 4000 in getNearestBlocksWhere, and
lower !searchForBlock's max range to 128 to match. A single search can no longer
stall the connection.
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