Skip to content

fix: clean up hash_to_block_id mapping when deallocating blocks#153

Open
ggboooy wants to merge 2 commits into
GeeeekExplorer:mainfrom
ggboooy:fix-deallocate-block
Open

fix: clean up hash_to_block_id mapping when deallocating blocks#153
ggboooy wants to merge 2 commits into
GeeeekExplorer:mainfrom
ggboooy:fix-deallocate-block

Conversation

@ggboooy
Copy link
Copy Markdown

@ggboooy ggboooy commented Jan 6, 2026

Problem
In the BlockManager._deallocate_block method, when deallocating a block, the corresponding entry in the hash_to_block_id dictionary was not being removed. This causes the hash mapping of deallocated blocks to remain in the dictionary, leading to potential issues:
Memory leak: invalid mappings accumulate in the dictionary over time
Logic error: may incorrectly reference deallocated blocks

Solution
Add hash mapping cleanup logic in the _deallocate_block method:
Check if the block being deallocated has a valid hash value (hash != -1)
If so, remove the corresponding mapping from the hash_to_block_id dictionary

Changes
File: nanovllm/engine/block_manager.py
Method: _deallocate_block
Added: hash mapping cleanup logic using pop() with safe default

Comment thread nanovllm/engine/block_manager.py Outdated
Co-authored-by: d.transposed <damian.bogunowicz@gmail.com>
@ggboooy
Copy link
Copy Markdown
Author

ggboooy commented Jan 8, 2026

Good catch!

@pangkun248
Copy link
Copy Markdown

If the above changes take effect, can the if branch in the code be simplified directly?

if block_id in self.used_block_ids:
      block = self.blocks[block_id]
      block.ref_count += 1
else:
    block = self._allocate_block(block_id)

change to

block = self.blocks[block_id]
block.ref_count += 1

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.

3 participants