Skip to content

Android: creating a task with a relative modelAssetPath truncates the cached model file that live tasks have mmap'ed, causing SIGBUS #6369

Open

Description

@Wancky

Summary

On Android, creating a task with a relative asset path (BaseOptions.setModelAssetPath("face_landmarker.task")) rewrites the cached copy of the model at <cacheDir>/drishti_asset_cache/face_landmarker.task every time, truncating it first. Every live task instance has that same file mmaped with MAP_SHARED, and TFLite reads constant tensors from the mapping on every Invoke. So if a second task is created while a first one is running inference, the first one can read pages beyond the (temporarily truncated) end of file and crash with SIGBUS (BUS_ADRERR).

Environment

  • com.google.mediapipe:tasks-vision:1.0.0 / tasks-core:1.0.0 (libmediapipe_tasks_jni.so, BuildId 773ea834cf94bf838f4c7c9d0405590f)
  • Pixel 6, Android 16 (CP1A.260405.005), arm64-v8a, release build
  • Face Landmarker, CPU delegate. Seen in LIVE_STREAM mode; the same overlap exists in VIDEO mode

Steps to reproduce

  1. Create Face Landmarker A with setModelAssetPath("face_landmarker.task") and keep running inference on camera frames.
  2. While A is still running inference, create Face Landmarker B with the same asset path (in our app: when the user changes numFaces, we create a new instance and close the old one once its in-flight inference finishes).
  3. Occasionally (about 1 in 15 re-creations for us), A's inference thread crashes.

Crash

F/libc: Fatal signal 7 (SIGBUS), code 2 (BUS_ADRERR), fault addr 0x7a0b463990 in tid 28865 (Thread-27)
signal 7 (SIGBUS), code 2 (BUS_ADRERR), fault addr 0x0000007a0b463990 (read)
memory near x23 (/data/data/<package>/cache/drishti_asset_cache/face_landmarker.task):

All 12 app frames are in libmediapipe_tasks_jni.so (no symbols). Matching each frame's function against the assert / source-file strings it references gives:
ThreadPool worker → SchedulerQueue::RunNextTask → CalculatorNode::ProcessNode → InferenceCalculator → inference_interpreter_delegate_runner.cc → tflite::Interpreter::Invoke → Subgraph::Invoke → StridedSlice Eval, which faults while reading its constant begin tensor. The fault address is inside the mapping of the cached model file. When debuggerd dumped memory ~0.7 s later the same address was readable again, because the rewrite had finished by then.

Cause

  1. ExternalFileHandler::MapExternalFile resolves file_name via PathToResourceAsFile.
  2. On Android, PathToResourceAsFile sends relative paths to AssetManager::CachedFileFromAsset (mediapipe/util/resource_util_android.cc).
  3. CachedFileFromAsset writes the asset with std::ofstream output_file(file_path);, which truncates the file and then writes it. The comment says it overwrites the cache file unconditionally (mediapipe/util/android/asset_manager_util.cc).
  4. ExternalFileHandler then open(O_RDONLY) + mmap(PROT_READ, MAP_SHARED)s that file. The .task entries are stored uncompressed, so TFLite uses the model in place and constant tensors point into the mapping.
  5. Each TaskRunner has its own ModelResourcesCache, so every new task repeats steps 1–4 on the same path while older tasks are still reading their mapping. Truncating a file removes its pages from every mapping, and a read past the current EOF raises SIGBUS.

Suggested fix

Write the cache file to a temporary file in the same directory and rename() it over the destination. Existing mappings keep the old inode, so nobody reads a truncated file. Alternatively, skip the rewrite when the cached file already has the same size and contents.

Workaround

Load the asset once into a direct ByteBuffer and pass it with BaseOptions.setModelAssetBuffer(...), keeping the buffer alive for the life of the process. MediaPipe then passes the pointer through FilePointerMeta and never touches the cache file. This fixed the crash for us.

Possibly related

#1817 (SIGBUS BUS_ADRERR on startup with two graphs, closed without a root cause) may have the same cause: two graphs starting at the same time, each rewriting the cached assets while the other is reading them.

Activity

  1. HarishPermal commented on Sep 28, 2026

    @HarishPermal

    hi @Wancky

    Thanks for raising this issue, and also the suggested fix im currently testing it and soon will update with the status here

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

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions