Crash: unguarded lazy init in loader, service cache and avformat when producers open on several threads
Maintainers usually reply within 1 day
Assessment
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Newbie friendliness
- 55/100
- Issue type
- Bug
- Clarity
- Mostly clear
- Activity status
- Active
- Tech stack
- c
- Domain
- audio-video-rtc
Research direction
Start by running the linked lazyinit reproduction after reading producer_loader.c, mlt_service.c, and avformat/factory.c at the lazy initialization sites named in the report. Trace the concurrent initialization and cleanup paths for dictionary, normalizers, the service cache, and avformat state. Done means the reproduction remains stable across repeated threaded runs without requiring the warm-up workaround.
Written by the indexing model from the issue text.
Description
Environment: MLT 7.40.0 (Fedora 44 package, x86_64), FFmpeg 8.1.2. The code involved is unchanged on master (0926a75).
Found while building a GTK4 video editor on MLT; the repro below uses only melt/libmlt.
What happens
Several pieces of process-wide MLT state are initialised lazily, on first use, without a lock or a once-guard:
producer_loader.c: the staticdictionary(create_producer(),if (dictionary == NULL)at line 87) andnormalizers(attach_normalizers());mlt_service.c:get_cache()creating the global"caches"properties (line 934);avformat/factory.c:avformat_initialised(line 61).
So the first producers opened on several threads at once can crash: two threads each build the dictionary, one's is freed while the other iterates it, and the same for the others.
Repro
Four threads each open a clip through the default loader and read one frame, straight after mlt_factory_init() (C, 50 lines): https://github.com/iDoMeteor/u-studio-video-editor/tree/70841c3/tools/upstream-repros/mlt/lazy-init-race
ffmpeg -f lavfi -i testsrc2=s=640x360:r=25:d=2 -pix_fmt yuv420p clip.mp4
cc -g lazyinit.c -o lazyinit -pthread $(pkg-config --cflags --libs mlt-framework-7)
for i in $(seq 200); do ./lazyinit clip.mp4 || echo crash; done | grep -c crash # 31
for i in $(seq 200); do ./lazyinit clip.mp4 warm || echo crash; done | grep -c crash # 0
31 of 200 runs segfaulted (16 cores). Where, from the core dumps:
| Crashing frames | Runs |
|---|---|
mlt_properties_count ← create_producer ← producer_loader_init (dictionary) |
14 |
mlt_properties_count ← attach_normalizers ← producer_loader_init |
11 |
mlt_properties_get_data/set_data ← get_cache ← mlt_service_cache_put ← producer_avformat_init |
4 |
mlt_frame_close (double free) and mlt_cache_item_close |
2 |
With warm (one open and decode on the main thread before the threads start): 0 of 200.
Expected: opening producers on several threads is safe. Actual: about one run in seven crashes.
Notes
pthread_once (or a mutex) around each of these would fix it. We work around it by opening one producer on the main thread right after mlt_factory_init(). The worker threads in apps (thumbnails, waveforms, probing) are the usual way to hit this.
- Dominant language
- C
- Stars
- 1.9k
- Forks
- 391
- Avg merge
- 1d 17h
- Merged PRs (30d)
- 12
Getting set up
Starts the project's dev container in your browser, under your own GitHub account.
- Ships a Dockerfile or Docker Compose file
- No pull request template
- No contributing guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from mltframework/mlt
-
Difficulty 2/5 1-3 hours Newbie friendliness 75/100
mltframework/mlt#1335 ·
Maintainers usually reply within 1 day
-
Difficulty 4/5 3-5 days Newbie friendliness 42/100
mltframework/mlt#1333 ·
Maintainers usually reply within 1 day
-
_unique_id is assigned with a non-atomic increment, so concurrent services can share an idPossibly taken @iDoMeteor claimed this 7 days ago. Open
Difficulty 4/5 3-5 days Newbie friendliness 72/100
mltframework/mlt#1319 · 2 comments ·
Maintainers usually reply within 1 day
-
Crash: avformat decoder cache (size 4) evicts state another thread is still decoding withPossibly taken @iDoMeteor claimed this 7 days ago. Open
Difficulty 4/5 3-5 days Newbie friendliness 52/100
mltframework/mlt#1318 · 1 comment ·
Maintainers usually reply within 1 day
-
mix transition allocates 9.2 MB per instancePossibly taken @iDoMeteor claimed this 7 days ago. Open
Difficulty 3/5 1-2 days Newbie friendliness 72/100
mltframework/mlt#1316 · 5 comments ·
Maintainers usually reply within 1 day
All issues in mltframework/mlt
Similar issues
-
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
-
backend
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
BasedHardware/omi#20940 ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
kovidgoyal/kitty#10625 ·
Maintainers usually reply within 1 day
-
Feature Status: Needs Triage
Difficulty 2/5 1-3 hours Newbie friendliness 73/100
Maintainers usually reply within 1 day