[Bug] MLC-LLM CLI Crashes with malloc Error on macOS after Source Build
Bug
Cannot execute python -m mlc_llm serve -h or any other MLC-LLM CLI commands after building from source on macOS with Metal support. The process crashes with a memory allocation error during import.
To Reproduce
Steps to reproduce the behavior:
Build MLC-LLM from source on macOS with Metal support:
# Configure build cd mlc-llm mkdir -p build && cd build python ../cmake/gen_cmake_config.py # Select Metal, disable CUDA/ROCm/Vulkan/OpenCL cmake .. make -j 10Set up environment variables:
export MLC_LLM_SOURCE_DIR=$PWD export PYTHONPATH=$MLC_LLM_SOURCE_DIR/python:$PYTHONPATH export PYTHONPATH=$PWD/3rdparty/tvm/python:$PYTHONPATH export DYLD_LIBRARY_PATH="$PWD/build/lib:$DYLD_LIBRARY_PATH"Install required packages:
pip install apache-tvm-ffi numpyAttempt to run any MLC-LLM command:
python -m mlc_llm -h python -m mlc_llm serve -hObserve the crash with malloc error:
python(xxxxx,0x20bb3a240) malloc: *** error for object 0x600001720008: pointer being freed was not allocated python(xxxxx,0x20bb3a240) malloc: *** set a breakpoint in malloc_error_break to debug zsh: abort python -m mlc_llm -h
Expected behavior
MLC-LLM CLI should display help information or start properly without crashing. The import of the mlc_llm module should complete successfully, and the command-line interface should be functional.
Environment
- Platform: Metal (Apple M4)
- Operating system: macOS 15.7.4
- Device: Macbook Air M4
- MLC-LLM Installation: Source (branch: main, v0.20.0.dev0)
- Python Installation: miniconda (conda environment: mlc-build)
- Python version: 3.13.14
- TVM Installation: Source (
mlc-llm/3rdparty/tvm, commit hash: 837cb9de1127b48ce48e4cefe09e83215b9d4ba7) - TVM-FFI: apache-tvm-ffi 0.1.12 (installed via pip)
- Compiler: AppleClang 17.0.0.17000013
- CMake: Build configured with
USE_METAL ON, other GPU backends disabled
Additional context
Build Output Summary
- Successfully built with
make -j 10 - Produced libraries:
libtvm_runtime.dylib,libtvm_ffi.dylib,libmlc_llm.dylib - Build warnings:
Cannot find libflash_attn- Version mismatch warnings for tokenizers_c.a (built for macOS 15.5 vs linked for 15.0)
- Unused private field warning in
paged_kv_cache.cc
Diagnostic Steps Performed
TVM import test initially failed with missing
tvm_ffimodule (resolved bypip install apache-tvm-ffi)TVM import test then failed with missing
numpy(resolved bypip install numpy)TVM import test succeeded after all dependencies installed:
python -c "import tvm; print(tvm.support.libinfo())"Output shows TVM detected the environment and libraries correctly.
MLC-LLM import fails with malloc corruption during module initialization.
Potential Root Causes
ABI/Symbol mismatch: The MLC-LLM build process may be producing binaries with different compilation flags than the Python bindings expect, or there may be version mismatches between the compiled libraries and the Python wrapper.
Double-free or memory corruption: The
pointer being freed was not allocatederror suggests that the C++ runtime is attempting to free memory that was either:- Already freed
- Not allocated by malloc
- Corrupted heap
Library loading order: Multiple dynamic libraries may be loading conflicting versions of symbols, especially given the libbacktrace dependency and the various
tvm_fficomponents.Missing
tvm_ffilibrary path: The build produceslibtvm_ffi.dylibinbuild/lib, but this directory may not be properly registered with the runtime loader despiteDYLD_LIBRARY_PATHbeing set. Thetvm_ffiPython package from PyPI might conflict with the built version.Python/C++ interface mismatch: Python 3.13.14 might have ABI differences that cause memory management issues with the compiled C++ code.
Additional Notes
- The error occurs only when importing the full
mlc_llmpackage, not when importingtvmalone - The crash happens consistently for all
mlc_llmsubcommands - The
mallocerror suggests the issue is in the C++ layer rather than pure Python
Workarounds Attempted
- Installed
apache-tvm-ffi(resolved initial import issues) - Installed
numpy(resolved TVM import issues) - Set
DYLD_LIBRARY_PATH(resolved library discovery) - Still crashes on MLC-LLM import
Source: mlc-ai/mlc-llm