Skip to content

fix(mcp): skip unreadable regions before splitting scans - #128

Open
StupidDev1337 wants to merge 1 commit into
JeanExtreme002:mainfrom
StupidDev1337:fix/skip-unreadable-regions-before-splitting
Open

StupidDev1337 wants to merge 1 commit into
JeanExtreme002:mainfrom
StupidDev1337:fix/skip-unreadable-regions-before-splitting

Conversation

@StupidDev1337

Copy link
Copy Markdown

Why is this PR necessary, what does it do?

Fixes #127. On 64-bit Windows targets, scan_value and scan_pattern ran out of the 30 s budget before they reached the heap.

  • The Windows region walk reports the whole address space, MEM_FREE and MEM_RESERVE ranges included.
  • _run_batched_scan cut all of that into scan_batch_bytes pieces. At the 64 MiB default that is about 2 million batches for ~128 TiB.

This PR leaves unreadable regions out before split_regions / batch_regions:

  • _run_batched_scan filters the snapshot to readable regions first. Unreadable regions can never hold a match, and every backend's scan already drops them through default_scan_filter, so the results do not change.
  • find_pointer_paths applies the same filter before cutting regions for the pointer map. scan_pointer_paths already keeps only readable regions itself, so the map does not change. It just no longer builds millions of pieces first.

list_memory_regions and the session's region snapshot are untouched.

How it was tested

  • Two regression tests in tests/mcp_server/test_toolset.py (TestHugeRegionsAreScannedInPieces) use a fake target with a 1 MiB unreadable region below its two readable ones.
    • Without the fix, both fail: the unreadable region turns into extra batches or pointer-map pieces.
    • With the fix, both pass.
  • flake8 PyMemoryEditor tests and mypy PyMemoryEditor report no issues.
  • Full suite (pytest tests) on Windows 11 x64, Python 3.13.14, with the speed extra: 1832 passed, 28 skipped, 1 failed.
    • The failure is tests/process/test_foreign_process.py, and it fails the same way on unmodified main.
    • Cause: inside a Windows venv, sys.executable is the venv launcher, which starts the real interpreter as a child, so the test opens the launcher's pid (WinError 299).
    • It passes when run with the base interpreter.
  • Live check with the reproducer from MCP scan_value on 64-bit Windows targets hits the 30 s budget before reaching the heap: free/reserved address space is split into millions of pieces #127 against this branch, without --scan-batch-bytes:
    • this branch: scan_value returns the counter with count: 1, partial: false, elapsed_seconds: 0.002;
    • 3.4.1: count: 0, partial: true, elapsed_seconds: 30.122.

Checklist (complete all items):

  • Added tests as necessary.
  • There is no breaking change for existing features.

References:

Fixes #127

Notes:

  • The change only filters on MemoryRegion.is_readable, so it is platform-independent. I could only run it on Windows; CI will cover Linux and macOS.
  • Written with help from Claude (an AI assistant). I reviewed the change and ran the checks above locally.

🤖 Generated with Claude Code

On Windows the region snapshot spans the whole address space, MEM_FREE
and MEM_RESERVE ranges included (~128 TiB on a 64-bit target).
_run_batched_scan cut all of it into scan_batch_bytes pieces, about two
million batches at the 64 MiB default, so scan_value and scan_pattern
spent their whole budget before reaching the heap.

Leave unreadable regions out before split_regions/batch_regions in
_run_batched_scan and find_pointer_paths. Every backend's scan already
drops them through default_scan_filter, and scan_pointer_paths keeps
only readable regions, so results do not change.

Fixes JeanExtreme002#127

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@github-actions github-actions Bot added lib Library changes (PyMemoryEditor/) tests Test changes (tests/) mcp MCP changes (PyMemoryEditor/mcp) labels Nov 5, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

lib Library changes (PyMemoryEditor/) mcp MCP changes (PyMemoryEditor/mcp) tests Test changes (tests/)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

MCP scan_value on 64-bit Windows targets hits the 30 s budget before reaching the heap: free/reserved address space is split into millions of pieces

1 participant