mlt icon indicating copy to clipboard operation
mlt copied to clipboard

macOS: melt segfaults mid-render in cache_object_close while another thread is in setlocale()

Open tydude001 opened this issue 2 months ago • 9 comments

On macOS, melt occasionally segfaults mid-render in cache_object_close. It happens on both Apple silicon and Intel. In every crash report, another thread is at that moment inside setlocale(), called from mlt_property_get_string_l_tf. My reading is that the per-property setlocale(LC_NUMERIC, ...) swap changes the process-wide locale while a render thread's sprintf is reading it.

Versions

  • melt 7.41.0, the unreleased master that Shotcut 26.8.1 bundles (Shotcut.app/Contents/MacOS/melt)
  • macOS 26.6.2 on arm64 (GitHub's macos-latest), and macOS 15.7.9 on x86_64 (macos-15-intel)
  • The XML root carries LC_NUMERIC="C", as Kdenlive's does. So every service gets a numeric locale, and mlt_property_get_string_l_tf takes its setlocale path.

How often

The same small project was rendered 30 times per runner. Each crashed render was re-run, and 9 runs crashed with SIGSEGV: 8 on arm64 and 1 on Intel. An earlier batch of 60 had 3 crashes. The crashes happen at different frames: one run died at frame 18 three times in a row, and another died around frame 216 of 289. Linux, with the same project, has never crashed in this way.

The faulting thread (identical in all 9 reports; EXC_BAD_ACCESS, KERN_INVALID_ADDRESS at 0x48)

libsystem_c.dylib   localeconv_l
libsystem_c.dylib   __vfprintf
libsystem_c.dylib   _vsnprintf
libsystem_c.dylib   __sprintf_chk
libmlt-7.7.dylib    cache_object_close
libmlt-7.7.dylib    mlt_cache_item_close
libmlt-7.7.dylib    mlt_property_close
libmlt-7.7.dylib    mlt_properties_close
libmlt-7.7.dylib    mlt_frame_close
  (mlt_property_close / mlt_properties_close / mlt_frame_close repeat)
libmltavformat.so   consumer_thread

Another thread, in all 9 reports

libsystem_kernel.dylib    __ulock_wait2
libsystem_platform.dylib  _os_unfair_lock_lock_slow
libsystem_c.dylib         setlocale
libmlt-7.7.dylib          mlt_property_get_string_l_tf
libmlt-7.7.dylib          mlt_properties_copy
libmlt-7.7.dylib          producer_get_frame
libmlt-7.7.dylib          mlt_service_get_frame

Where in the source (master at 0926a75)

  • src/framework/mlt_property.c, mlt_property_get_string_l_tf: it saves the locale, calls setlocale(LC_NUMERIC, localename), formats with sprintf, then restores it with setlocale(LC_NUMERIC, orig_localename). The comment says: "TODO: when glibc gets sprintf_l, start using it! For now, hack on setlocale." self->mutex guards only that one property, but setlocale changes the locale for every thread.
  • src/framework/mlt_cache.c, cache_object_close: sprintf(key, "%p", object) on the consumer thread. On macOS, sprintf reads the current locale through localeconv_l, and it faults there.

A possible fix

On glibc and Apple, list->locale is already a locale_t. The function could switch the calling thread only, as mlt_properties.c already does at about line 2210:

mlt_locale_t orig = uselocale(locale);
/* sprintf ... */
uselocale(orig);

On Apple, snprintf_l(buf, size, locale, ...) from <xlocale.h> would also avoid the switch altogether.

I haven't rebuilt MLT to test a patch. But removing LC_NUMERIC from the document root, so that no service gets a locale and setlocale is never called there, stopped the crash: the same soak then ran 60 renders with no crash and no crash report (9 crashes in 69 melt runs before). I can test a patch or a nightly Shotcut melt against the same soak if that helps. I have all nine .ips crash reports and can attach them.

This looks different from #786 (a crash at the end of the render on Windows/Linux, fixed in 7.38.0). This one is mid-render, on 7.41.0.

It may be the same race as #1049 (closed without a cause): that backtrace also dies in localeconv_l under sprintf on the consumer thread, called from mlt_events_fire.

tydude001 avatar Sep 30 '26 14:09 tydude001

Thank you for raising this; this area is a mess! Support for locale-specific numerics on the command line usage and XML once seemed like a good idea a long time ago. But now, it is considered deprecated and legacy. I have been meaning to make a build option to exclude it, but also I have considered simply removing it (without breaking ABI, behavior change to ignore it). After all, it is excluded behavior on Windows making it inconsistent. There is a new release in a couple of days that will not include removal. The "TODO... hack" comment and a vague recollection leads me to believe the intention was to use sprintf_l on Apple and possibly BSD systems. And I think setlocales can be safely changed to uselocale elsewhere. I will work on this ASAP.

ddennedy avatar Sep 30 '26 16:09 ddennedy

Thanks, that makes sense. For what it's worth, dropping locale support entirely would suit us: we already omit LC_NUMERIC from the document root and read numbers as C. When you have a patch or a nightly build, I'll run it through the same macOS soak (30 renders each on arm64 and Intel) and report back.

The nine .ips crash reports are here: https://gist.github.com/tydude001/aa8eb4fa6996ed26134c9b257471f525 (8 arm64 on macOS 26.6.2, 1 x86_64 on macOS 15.7.9; the filename prefix says which).

tydude001 avatar Sep 30 '26 17:09 tydude001

Please test branch fix-property-locale

ddennedy avatar Oct 01 '26 00:10 ddennedy

Tested fix-property-locale (99383f4) on macOS. It stops the crash on our soak.

Setup. I built libmlt (framework only) twice: from the branch, and from its merge-base on master (89fbe37). Each went into its own copy of Shotcut 26.8.1's app bundle, re-signed ad hoc the same way, so the two builds differ only by this branch. The document keeps LC_NUMERIC="C" on its root, and melt runs directly under the runner's locale (LANG/LC_ALL = en_US.UTF-8). The two builds alternate render by render.

runner master (89fbe37) branch (99383f4)
macOS 15, x86_64 (macos-15-intel) 6 of 200 SIGSEGV 0 of 200
macOS 26, arm64 (macos-latest) 1 SIGSEGV in 50, then a hang 0 of 51

The arm64 job stopped early. Master's 51st render hung for over an hour and I cancelled the job. I didn't capture a stack for the hang, so I can't say whether it's the same race.

All seven crash reports have the same fault as before: localeconv_l under sprintf in cache_object_close. In six of them the other thread is in setlocale from mlt_property_get_string_l_tf (via mlt_properties_copy, producer_get_frame). In the seventh, that thread is waiting on a lock inside localeconv_l, under vsnprintf. I can add these reports to the gist if they're useful.

One side observation: with LC_NUMERIC=C in melt's environment, so the process is already in "C", master didn't crash in 50 renders on arm64. A setlocale to the name already in effect may change nothing on macOS, which would close the race from the outside. The branch fixes it either way.

I'll rerun arm64 with a per-render timeout and a stack sample on a hang, and post here if anything changes. Thanks for the quick fix!

tydude001 avatar Oct 01 '26 11:10 tydude001

Follow-up: the arm64 rerun, 200 renders per build, with a 60 s timeout per render and a sample of any render that hangs.

runner master (89fbe37) branch (99383f4)
macOS 26.6.2, arm64 (macos-latest) 13 of 200 SIGSEGV 0 crashes in 200, 1 hang

All 13 crash reports fault in localeconv_l at 0x48 under sprintf. In 11 the caller is cache_object_close. In the other 2 it is mlt_events_fire on the avformat consumer_thread, which is the backtrace in #1049, so the branch looks like it covers that one too.

The hang came on the branch this time (last night's was on master), and I don't think it's this bug. In the sample, melt's main thread sits in main's wait loop (nanosleep, polling consumer_is_stopped). No avformat consumer_thread is left, no slice workers either, only idle h264 decoder threads, and nothing is in setlocale. melt's output stops after libx264 opens, and the footprint was 50 MB. It looks like the consumer thread is gone while the consumer never reads as stopped. That's 2 hangs in about 500 arm64 renders, one on each build. I can put the sample in the gist or open a separate issue if it's useful.

tydude001 avatar Oct 01 '26 12:10 tydude001

One more data point on the hang. I ran stock Shotcut 26.8.1 melt on arm64 with LC_NUMERIC removed from the document root, so no setlocale runs at all: 0 hangs and 0 crashes in 600 renders (300 under en_US.UTF-8, 300 with LC_NUMERIC=C in the environment). At the earlier rate of about 2 in 500 that's not conclusive, but it suggests the hang also needs the locale path rather than being a separate melt bug.

tydude001 avatar Oct 01 '26 14:10 tydude001

Create a separate issue for the deadlock corner case. I am making any day now and will include only this PR/fix and not something with a larger footprint. Thank you

ddennedy avatar Oct 01 '26 17:10 ddennedy

Opened #1333 for the hang. Thanks!

tydude001 avatar Oct 01 '26 17:10 tydude001

Re-soaked the merged commit: 97854f3 vs its parent 84da207, 200 interleaved renders per side on GitHub's Mac runners. Intel: parent 5 SIGSEGV, fix 0. Apple silicon: parent 17 SIGSEGV, fix 0. Same localeconv_l fault in all 22 reports, no hangs.

tydude001 avatar Oct 01 '26 23:10 tydude001