Skip to content

hi-mlx expert_pool: close fd leaks, fix mmap read overflow - #145

Closed
davidrhodus wants to merge 1 commit into
mainfrom
hi-mlx-expert-pool-fixes
Closed

hi-mlx expert_pool: close fd leaks, fix mmap read overflow#145
davidrhodus wants to merge 1 commit into
mainfrom
hi-mlx-expert-pool-fixes

Conversation

@davidrhodus

Copy link
Copy Markdown
Contributor

Summary

Two soundness/resource fixes in crates/hi-mlx/src/expert_pool.rs (MmapShard):

  1. File-descriptor leak. MmapShard::open leaked both fds via std::mem::forget — the File backing the mmap and the F_NOCACHE direct fd — and Drop never closed the mmap fd. Each opened shard permanently consumed two descriptors, a slow leak across repeated model loads. Both fds are now owned via into_raw_fd and closed exactly once in Drop after munmap (no leak, no double-close), and the confused stale comment is removed.

  2. Out-of-bounds read via integer overflow. MmapShard::read computed end = start + len without a checked add, so a huge offset/length pair could wrap past the end > self.len bounds check and read out of bounds. Now uses checked_add and rejects overflow.

Also prefixes the now-unused pad_attention_bias offset param in models.rs (the cache bound is correctly derived from kv_len - l per the in-loop rationale) to silence the warning.

Verification

  • cargo test -p hi-mlx --features mlx --lib expert_pool: 9/9 pass.
  • The full hi-mlx GPU suite is environmentally blocked in this sandbox (Metal toolchain can't build mlx.metallib) and fails identically on the base — no regressions from these changes.
  • cargo check --workspace: clean.

Note: these changes are already on main (committed as 1513d46 during the review sweep); this branch carries the same fix isolated for review.

- MmapShard leaked both file descriptors (std::mem::forget on the File
  and the F_NOCACHE direct fd) and never closed the mmap fd on Drop, so
  each opened shard permanently consumed two fds — a slow leak across
  repeated model loads. Both fds are now owned via into_raw_fd and closed
  exactly once in Drop after munmap, removing the leak and the confused
  stale comment.
- MmapShard::read computed end = start + len without a checked_add, so a
  huge offset/len pair could wrap and pass the bounds check, reading out
  of bounds. Now uses checked_add and rejects overflow.
- models.rs: prefix the now-unused pad_attention_bias  param
  (cache bound is derived from kv_len - l instead) to silence the warning.

expert_pool 9/9 tests pass with --features mlx; GPU backend tests are
environmentally blocked here (sandbox denies mlx.metallib caching) and
fail identically on HEAD — no regressions. cargo check --workspace clean
@davidrhodus

Copy link
Copy Markdown
Contributor Author

These changes are already on main (committed as 1513d46 during the review sweep), so this isolated review branch is redundant. Closing without merging.

@davidrhodus davidrhodus closed this Aug 7, 2026
@davidrhodus
davidrhodus deleted the hi-mlx-expert-pool-fixes branch August 7, 2026 00:51
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