BLI_mmap not thread-safe on Linux/macOS (unfreed memory generating thumbnails) #91212

Open
opened 2021-09-06 12:20:59 +02:00 by Campbell Barton · 2 comments

Recently I noticed generating blend file thumbnails sometimes leaks memory, this is caused by 0f2ae614a1, which uses a linked list for error_handler_data.open_mmaps, threads are used to open blend file thumbnails which add/remove items without any locking.

Testing should be straightforward.

  • Remove thumbnail cache, (e.g. rm -rf ~/.cache/thumbnails)
  • Open a directory containing many blend files.
  • Wait for the thumbnails to load.
  • Close blender.

On exit ASAN reports this error (below).

Checking BLI_mmap it looks like all list access would need to locking, including handling errors.


Direct leak of 32 byte(s) in 1 object(s) allocated from:
    - 0 0x7f7ed1d97459 in __interceptor_calloc /build/gcc/src/gcc/libsanitizer/asan/asan_malloc_linux.cpp:154
    - 1 0x55cd8a26f9ac in MEM_lockfree_callocN /src/blender/intern/guardedalloc/intern/mallocn_lockfree_impl.c:236
    - 2 0x55cd8a00563a in BLI_genericNodeN /src/blender/source/blender/blenlib/intern/listbase.c:932
    - 3 0x55cd89eded19 in sigbus_handler_add /src/blender/source/blender/blenlib/intern/BLI_mmap.c:136
    - 4 0x55cd89edef8c in BLI_mmap_open /src/blender/source/blender/blenlib/intern/BLI_mmap.c:188
    - 5 0x55cd89fa181d in BLI_filereader_new_mmap /src/blender/source/blender/blenlib/intern/filereader_memory.c:130
    - 6 0x55cd83c2f97d in blo_filedata_from_file_descriptor /src/blender/source/blender/blenloader/intern/readfile.c:1223
    - 7 0x55cd83c2fec8 in blo_filedata_from_file_open /src/blender/source/blender/blenloader/intern/readfile.c:1270
    - 8 0x55cd83c30137 in blo_filedata_from_file_minimal /src/blender/source/blender/blenloader/intern/readfile.c:1293
    - 9 0x55cd83c31e31 in BLO_thumbnail_from_file /src/blender/source/blender/blenloader/intern/readfile.c:1513
    - 10 0x55cd84e68067 in imb_thumb_load_from_blendfile /src/blender/source/blender/imbuf/intern/thumbs_blend.c:72
    - 11 0x55cd84e6810b in IMB_thumb_load_blend /src/blender/source/blender/imbuf/intern/thumbs_blend.c:86
    - 12 0x55cd84e6540c in thumb_create_ex /src/blender/source/blender/imbuf/intern/thumbs.c:390
    - 13 0x55cd84e6676c in thumb_create_or_fail /src/blender/source/blender/imbuf/intern/thumbs.c:495
    - 14 0x55cd84e677e9 in IMB_thumb_manage /src/blender/source/blender/imbuf/intern/thumbs.c:656
    - 15 0x55cd893c5c44 in filelist_cache_preview_runf /src/blender/source/blender/editors/space_file/filelist.c:1472
    - 16 0x55cd8a24591e in Task::operator()() const /src/blender/source/blender/blenlib/intern/task_pool.cc:178
    - 17 0x55cd8a24a288 in tbb::internal::function_task<Task>::execute() /usr/include/tbb/task.h:1059
    #18 0x55cd83a89424 in tbb::internal::custom_scheduler<tbb::internal::IntelSchedulerTraits>::process_bypass_loop(tbb::internal::context_guard_helper<false>&, tbb::task*, long) (/src/cmake_debug/bin/blender+0x12db9424)
Recently I noticed generating blend file thumbnails sometimes leaks memory, this is caused by 0f2ae614a1, which uses a linked list for `error_handler_data.open_mmaps`, threads are used to open blend file thumbnails which add/remove items without any locking. Testing should be straightforward. - Remove thumbnail cache, (e.g. `rm -rf ~/.cache/thumbnails`) - Open a directory containing many blend files. - Wait for the thumbnails to load. - Close blender. On exit ASAN reports this error (below). Checking `BLI_mmap` it looks like all list access would need to locking, including handling errors. ---- ``` Direct leak of 32 byte(s) in 1 object(s) allocated from: - 0 0x7f7ed1d97459 in __interceptor_calloc /build/gcc/src/gcc/libsanitizer/asan/asan_malloc_linux.cpp:154 - 1 0x55cd8a26f9ac in MEM_lockfree_callocN /src/blender/intern/guardedalloc/intern/mallocn_lockfree_impl.c:236 - 2 0x55cd8a00563a in BLI_genericNodeN /src/blender/source/blender/blenlib/intern/listbase.c:932 - 3 0x55cd89eded19 in sigbus_handler_add /src/blender/source/blender/blenlib/intern/BLI_mmap.c:136 - 4 0x55cd89edef8c in BLI_mmap_open /src/blender/source/blender/blenlib/intern/BLI_mmap.c:188 - 5 0x55cd89fa181d in BLI_filereader_new_mmap /src/blender/source/blender/blenlib/intern/filereader_memory.c:130 - 6 0x55cd83c2f97d in blo_filedata_from_file_descriptor /src/blender/source/blender/blenloader/intern/readfile.c:1223 - 7 0x55cd83c2fec8 in blo_filedata_from_file_open /src/blender/source/blender/blenloader/intern/readfile.c:1270 - 8 0x55cd83c30137 in blo_filedata_from_file_minimal /src/blender/source/blender/blenloader/intern/readfile.c:1293 - 9 0x55cd83c31e31 in BLO_thumbnail_from_file /src/blender/source/blender/blenloader/intern/readfile.c:1513 - 10 0x55cd84e68067 in imb_thumb_load_from_blendfile /src/blender/source/blender/imbuf/intern/thumbs_blend.c:72 - 11 0x55cd84e6810b in IMB_thumb_load_blend /src/blender/source/blender/imbuf/intern/thumbs_blend.c:86 - 12 0x55cd84e6540c in thumb_create_ex /src/blender/source/blender/imbuf/intern/thumbs.c:390 - 13 0x55cd84e6676c in thumb_create_or_fail /src/blender/source/blender/imbuf/intern/thumbs.c:495 - 14 0x55cd84e677e9 in IMB_thumb_manage /src/blender/source/blender/imbuf/intern/thumbs.c:656 - 15 0x55cd893c5c44 in filelist_cache_preview_runf /src/blender/source/blender/editors/space_file/filelist.c:1472 - 16 0x55cd8a24591e in Task::operator()() const /src/blender/source/blender/blenlib/intern/task_pool.cc:178 - 17 0x55cd8a24a288 in tbb::internal::function_task<Task>::execute() /usr/include/tbb/task.h:1059 #18 0x55cd83a89424 in tbb::internal::custom_scheduler<tbb::internal::IntelSchedulerTraits>::process_bypass_loop(tbb::internal::context_guard_helper<false>&, tbb::task*, long) (/src/cmake_debug/bin/blender+0x12db9424) ```
Lukas Stockner was assigned by Campbell Barton 2021-09-06 12:20:59 +02:00
Author
Owner

Added subscribers: @ideasman42, @mont29, @brecht

Added subscribers: @ideasman42, @mont29, @brecht
Campbell Barton changed title from BLI_mmap not thread-safe (unfreed memory generating thumbnails) to BLI_mmap not thread-safe on Linux/macOS (unfreed memory generating thumbnails) 2021-09-06 12:21:21 +02:00

Changed status from 'Needs Triage' to: 'Confirmed'

Changed status from 'Needs Triage' to: 'Confirmed'
Philipp Oeser removed the
Interest
Core
label 2023-02-09 14:43:10 +01:00
Sign in to join this conversation.
No Label
Interest
Alembic
Interest
Animation & Rigging
Interest
Asset Browser
Interest
Asset Browser Project Overview
Interest
Audio
Interest
Automated Testing
Interest
Blender Asset Bundle
Interest
BlendFile
Interest
Collada
Interest
Compatibility
Interest
Compositing
Interest
Core
Interest
Cycles
Interest
Dependency Graph
Interest
Development Management
Interest
EEVEE
Interest
EEVEE & Viewport
Interest
Freestyle
Interest
Geometry Nodes
Interest
Grease Pencil
Interest
ID Management
Interest
Images & Movies
Interest
Import Export
Interest
Line Art
Interest
Masking
Interest
Metal
Interest
Modeling
Interest
Modifiers
Interest
Motion Tracking
Interest
Nodes & Physics
Interest
OpenGL
Interest
Overlay
Interest
Overrides
Interest
Performance
Interest
Physics
Interest
Pipeline, Assets & IO
Interest
Platforms, Builds & Tests
Interest
Python API
Interest
Render & Cycles
Interest
Render Pipeline
Interest
Sculpt, Paint & Texture
Interest
Text Editor
Interest
Translations
Interest
Triaging
Interest
Undo
Interest
USD
Interest
User Interface
Interest
UV Editing
Interest
VFX & Video
Interest
Video Sequencer
Interest
Virtual Reality
Interest
Vulkan
Interest
Wayland
Interest
Workbench
Interest: X11
Legacy
Blender 2.8 Project
Legacy
Milestone 1: Basic, Local Asset Browser
Legacy
OpenGL Error
Meta
Good First Issue
Meta
Papercut
Meta
Retrospective
Meta
Security
Module
Animation & Rigging
Module
Core
Module
Development Management
Module
EEVEE & Viewport
Module
Grease Pencil
Module
Modeling
Module
Nodes & Physics
Module
Pipeline, Assets & IO
Module
Platforms, Builds & Tests
Module
Python API
Module
Render & Cycles
Module
Sculpt, Paint & Texture
Module
Triaging
Module
User Interface
Module
VFX & Video
Platform
FreeBSD
Platform
Linux
Platform
macOS
Platform
Windows
Priority
High
Priority
Low
Priority
Normal
Priority
Unbreak Now!
Status
Archived
Status
Confirmed
Status
Duplicate
Status
Needs Info from Developers
Status
Needs Information from User
Status
Needs Triage
Status
Resolved
Type
Bug
Type
Design
Type
Known Issue
Type
Patch
Type
Report
Type
To Do
No Milestone
No project
No Assignees
2 Participants
Notifications
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Dependencies

No dependencies set.

Reference: blender/blender#91212
No description provided.