Commit c87e487c authored by Alexandre Janniaux's avatar Alexandre Janniaux
Browse files

lib: media: wait preparse end before release

This is a quick fix for #25296 actually reverting the commit
a708c988, replacing the Hold/Release
lifecycle used there by an ad-hoc Hold/Release which actually defer the
actual release by blocking instead of releasing in the preparser thread.

Indeed, libvlc_media is holding the instance used to create it, though
it only needs it for preparsing. If the last reference of the media is
hold by the preparser, libvlc_media will be released by the preparser
thread and the libvlc_instance potentially released from there too.
Since killing the libvlc_instance will join the preparser thread, it
cannot be done from the preparser thread, or would lead to the failure
detailed in #25296. Written differently, only the final client should
make use of Hold/Release, directly through the adequate function or
indirectly through the creation of new objects.

In addition, a708c988 cannot just be
reverted as it was fixing cases where the libvlc_media_t object were
released before preparsing is finished, and the libvlc application
cannot just wait for the preparsing event to arrive since it must still
typically unlock the event manager and manage subitems.

Ideally, the libvlc_media would just not have a strong reference to the
libvlc instance, which would allow detaching the libvlc state from the
libvlccore state, but it cannot be done just yet, since the preparsing
API must be splitted from libvlc_media first.

Fix #25296
parent ddbc42d3
Pipeline #66689 failed with stage
in 70 minutes and 50 seconds
......@@ -432,7 +432,12 @@ static void input_item_preparse_ended(input_item_t *item,
return;
}
send_parsed_changed( p_md, new_status );
libvlc_media_release( p_md );
vlc_mutex_lock(&p_md->idle_lock);
assert(p_md->worker_count > 0);
p_md->worker_count--;
vlc_mutex_unlock(&p_md->idle_lock);
vlc_cond_signal(&p_md->idle_cond);
}
/**
......@@ -508,6 +513,9 @@ libvlc_media_t * libvlc_media_new_from_input_item(
vlc_mutex_init(&p_md->parsed_lock);
vlc_mutex_init(&p_md->subitems_lock);
vlc_cond_init(&p_md->idle_cond);
p_md->worker_count = 0;
p_md->state = libvlc_NothingSpecial;
/* A media descriptor can be a playlist. When you open a playlist
......@@ -652,11 +660,17 @@ void libvlc_media_release( libvlc_media_t *p_md )
/* Cancel asynchronous parsing (if any) */
libvlc_MetadataCancel( p_md->p_libvlc_instance->p_libvlc_int, p_md );
if( p_md->p_subitems )
libvlc_media_list_release( p_md->p_subitems );
/* Wait for all async tasks to stop. */
vlc_mutex_lock( &p_md->parsed_lock );
while( p_md->worker_count > 0 )
vlc_cond_wait( &p_md->idle_cond, &p_md->parsed_lock );
vlc_mutex_unlock( &p_md->parsed_lock );
input_item_Release( p_md->p_input_item );
if( p_md->p_subitems )
libvlc_media_list_release( p_md->p_subitems );
libvlc_event_manager_destroy( &p_md->event_manager );
libvlc_release( p_md->p_libvlc_instance );
free( p_md );
......@@ -862,13 +876,22 @@ static int media_parse(libvlc_media_t *media, bool b_async,
if (parse_flag & libvlc_media_do_interact)
parse_scope |= META_REQUEST_OPTION_DO_INTERACT;
libvlc_media_retain(media);
/* Note: we cannot keep parsed_lock when calling libvlc_MetadataRequest
* because it might also be used to submit the state synchronously
* which would result in recursive lock. */
vlc_mutex_lock(&media->parsed_lock);
media->worker_count++;
vlc_mutex_unlock(&media->parsed_lock);
ret = libvlc_MetadataRequest(libvlc, item, parse_scope,
&input_preparser_callbacks, media,
timeout, media);
if (ret != VLC_SUCCESS)
{
libvlc_media_release(media);
vlc_mutex_lock(&media->parsed_lock);
media->worker_count--;
vlc_mutex_unlock(&media->parsed_lock);
vlc_cond_signal(&media->idle_cond);
return ret;
}
}
......
......@@ -48,6 +48,13 @@ struct libvlc_media_t
vlc_mutex_t parsed_lock;
vlc_mutex_t subitems_lock;
/* Idle protection to prevent the media from being released during
* preparsing. The preparse will be cancelled but the release will
* be blocking until no async code is using the media anymore. */
vlc_cond_t idle_cond;
vlc_mutex_t idle_lock;
size_t worker_count;
libvlc_media_parsed_status_t parsed_status;
bool is_parsed;
bool has_asked_preparse;
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment