fix: bound block-search volume so a large !searchForBlock can't stall the connection#795
Open
atiweb wants to merge 1 commit into
Open
fix: bound block-search volume so a large !searchForBlock can't stall the connection#795atiweb wants to merge 1 commit into
atiweb wants to merge 1 commit into
Conversation
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
getNearestBlocksWhere(andgetNearestBlocks, which wraps it) callsbot.findBlockssynchronously.findBlocksscans a volume that grows with the cube ofmaxDistance, and with the defaultcountof10000it does not early-exit when the target block is rare or absent — it walks the entire volume on the main thread.The
!searchForBlockcommand exposes asearch_rangeof 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:maxDistance→ 128 — a search past the loaded view distance returns nothing useful anyway, so this doesn't drop real results.count→ 4000 — a lower cap only makesfindBlocksearly-exit sooner; it can never return fewer results than a caller actually consumes.…and lower
!searchForBlock'ssearch_rangedomain max from512to128to match, so the model can't request a radius the engine would only clamp.Why it's safe
findBlocksalready 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.countdownward 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.