spatie / laravel-medialibrary

Associate files with Eloquent models
https://spatie.be/docs/laravel-medialibrary
MIT License
5.77k stars 1.07k forks source link

Segmentation fault while loading media #3225

Closed martynaskre closed 1 year ago

martynaskre commented 1 year ago

After updating composer packages and installing version 10.7.14, media library causes SIGSEGV - core dumped error.

Copy from php8.1-error.log:

[29-Mar-2023 13:55:16] NOTICE: [pool valet] child 59740 started
[29-Mar-2023 13:55:16] WARNING: [pool valet] child 59607 exited on signal 11 (SIGSEGV - core dumped) after 98.153450 seconds from start

It is worth mentioning that this error only occurs in specific circumstances. We are using Livewire Powergrid package for building datatables. This code snippet below adds column to datatable and causes a segmentation fault:

->addColumn('preview',
    fn(Layout $model) => view('livewire.powergrid.image-column', [
        'src' => $model->getFirstMediaUrl(Layout::LAYOUT_COLLECTION),
        'alt' => $model->name,
        'isImage' => $model->getFirstMedia(Layout::LAYOUT_COLLECTION)->getTypeFromMime()
    ])
)

If we modify this function like shown below, then we dont get any errors and standard dd() output is displayed with correct media url.

->addColumn('preview',
    fn(Layout $model) => dd($model->getFirstMediaUrl(Layout::LAYOUT_COLLECTION)))
)

I think it might be related to #3219

freekmurze commented 1 year ago

It's indeed related. It's fixed in the latest release.

More info here: https://github.com/spatie/laravel-medialibrary/issues/3224#issuecomment-1488528110

cosmastech commented 1 year ago

@martynaskre curious: how many media are being loaded?

I'm curious if the segfault is caused by having a massive number of media for a model or something else.

@freekmurze thoughts on adding a static property to the InteractsWithMedia trait like:


public static bool $setRelationForModels = false;

public function loadMedia(...)
{
    if (static::$setRelationForModels)
    {
        $collection->each->setRelation('model', $this);
    }
}

I know that laravel core does this frequently, and then will do something like:

public static function withoutSettingRelation(callable $fn) {
    $before = static::$setRelationForModels;
    static::$setRelationForModels = false;
    $fn();
    static::$setRelationForModels = $before;
}