Repository navigation
Description
Problem
On a device where a gstreamer seek operation leads to a pipeline async state change, we may enter a flow of internal calls that lead to a crash. This was detected with Spotify when performing a seek to the asset end (duration) right after playback has started from the beginning (position zero).
The crash is consequence of "m_seekTargetPromise" in "MediaSource" being invalid (nullptr) when used. This occurs because a second seek is executed before the first seek call has returned (1 level recursive call). This issue does not happen if the gstreamer seek operation does not cause a async pipeline state change.
Reproduction
This issue can be reproduced with the the test contained in the attached package:
mse-seek-end-spotify-crash.zip
This needs to be run on a device where the seek operation triggers an async state change.
Analysis
When we perform the first seek (to end of the asset), "MediaPlayerPrivateGStreamerMSE::doSeek" is called which will lead to "m_seekTargetPromise" to be set in "MediaSource::waitForTarget" (MediaSource.cpp#L341). As there is no buffered data for that position, it goes through the "No data at seeked time" path calling "m_private->setMediaPlayerReadyState" (MediaSource.cpp#L351). However, before returning from this call, the chain of methods called and considering the current internal states (where pipeline is still in the async state change waiting for preroll), the "MediaPlayerPrivateGStreamerMSE::doSeek" is called again but for position "zero". It will call "MediaSource::waitForTarget" again but as we have data buffered now for position zero (issue is reproduced by starting playing the asset from start and immediately seeking to the end position to keep a buffered range for position zero), the "MediaSource::completeSeek" is called (MediaSource.cpp#L363), which will reset the "m_seekTargetPromise" to nullptr (MediaSource.cpp#L410).
Now, all the call chain unwinds and we return from the "m_private->setMediaPlayerReadyState" call we did for the first seek. As the code did not expect changes to the "m_seekTargetPromise", it will just use it right after without any validation (MediaSource.cpp#L358).
Adding a validation there to avoid the crash is not correct, because we got there through a path that should not really be valid (recursive seek).
The detailed call sequence that leads to the crash is the following (some intermediate calls removed for clarity):
- HTMLMediaElement::seek(E7B0CF2B) {"value":210.926439}
- MediaPlayerPrivateGStreamerMSE::doSeek
- MediaSource::waitForTarget - KEY POINT 1
- MediaSourcePrivateGStreamer::setMediaPlayerReadyState
- MediaPlayerPrivateGStreamerMSE::propagateReadyStateToPlayer
- MediaPlayer::readyStateChanged
- HTMLMediaElement::mediaPlayerReadyStateChanged
- HTMLMediaElement::setReadyState
- HTMLMediaElement::updatePlayState - shouldBePlaying = false, playerPaused = false
- HTMLMediaElement::pausePlayer
- MediaPlayerPrivateGStreamerMSE::pause
- MediaPlayer::playbackStateChanged
- HTMLMediaElement::mediaPlayerPlaybackStateChanged - playerPaused: false, shouldBePaused: true - KEY POINT 2
-HTMLMediaElement::playInternal - endedPlayback() returns true
...
- HTMLMediaElement::seekInternal - seek to zero position
...
- MediaPlayerPrivateGStreamerMSE::doSeek
- MediaSource::waitForTarget - KEY POINT 3
- MediaSource::completeSeek
- m_seekTargetPromise.reset() - KEY POINT 4
...
- Return from MediaSourcePrivateGStreamer::setMediaPlayerReadyState call - KEY POINT 5
- m_seekTargetPromise is dereferenced, but is nullptr because it was reset
- KEY POINT 1: first MediaSource::waitForTarget call, first seek - the "m_seekTargetPromise" variable is set with a valid promise. We go through the path of "no data available" at the target position (as expected)
- KEY POINT 2: The media element believes the player is not paused, because "m_player->paused()" (MediaPlayerPrivateGStreamer) returned false. This happens because the pipeline is still in an async state change waiting for the preroll to complete. This will cause "HTMLMediaElement::playInternal " to be called which detects that we reached the end and triggers a seek to the asset start
- KEY POINT 3: second MediaSource::waitForTarget call, second seek - the existing promise tracked by "m_seekTargetPromise" is rejected and a new one is set. Now we go through the path of "data is available", consequently calling "completeSeek"
- KEY POINT 4: The "m_seekTargetPromise" is reset, as the second seek is considered complete.
- KEY POINT 5: We return to continue the execution of the first "MediaSource::waitForTarget" call which assumed "m_seekTargetPromise" is valid, but it was already reset, causing the de-reference of the nullptr.
Note: As a consequence of the first seek, the "isWaitingPreroll" will always be true in "MediaPlayerPrivateGStreamerMSE::updateStates" (MediaPlayerPrivateGStreamerMSE.cpp#L460), because the async state change is still in progress and will remain in progress till we get data, which won't happen since we reached the end. The EOS was not signaled yet by the app when the second seek is performed.
Possible fix
The main reason for this appears to be related with the fact that the HTMLMediaElement requested a switch to a paused state, but that did not complete due the ongoing async state change. Consequently, when checking if the media player is in the paused state, it will believe it is not. Properly fixing this seems a bit tricky. However, ensuring that "MediaPlayerPrivateGStreamer::paused()" returns true in this case, avoids the HTMLMediaElement being misguided to trigger another seek.
This can be achieved by adding the following snippet to the the "MediaPlayerPrivateGStreamerMSE::updateStates()" checks (MediaPlayerPrivateGStreamerMSE.cpp#L485):
} else if (isWaitingPreroll && m_mediaTimeDuration.isValid() && (m_seekTarget.time >= m_mediaTimeDuration)) {
GST_DEBUG_OBJECT(pipeline(), "EOS during preroll with no buffers");
m_isEndReached = true;
}
As there might be corner cases not yet identified, it is not fully clear yet if this is valid or adequate (it was not tested yet with the non-async pipeline state change scenario).
Activity
filipe-norte commentedon Mar 5, 2026
As a clarification, the main issue is the second seek operation that should not occur while the first is in progress. The second issue, consequence of the first, is the crash due "m_seekTargetPromise" being invalid.
Also tried an alternative (suggestion from Eugene) that seems to fix the issue as well, although no further testing was done yet to check for side-effects. Essentially, allow switching to paused state while still in preroll state. This is achieved by changing:
MediaPlayerPrivateGStreamer::changePipelineState - MediaPlayerPrivateGStreamer.cpp#L1099 from
to
and
MediaPlayerPrivateGStreamerMSE::updateStates - MediaPlayerPrivateGStreamerMSE.cpp#L474 from
to
eocanha commentedon Mar 18, 2026
We've identified that upstream commit WebKit/WebKit@7df9b18 solves the crash (but not the cause of having two nested seeks). It's been backported to wpe-2.46 as 4a53149.
filipe-norte commentedon Mar 31, 2026
Thanks @eocanha . I also confirm this solves the crash, although with the side-effect that we jump to the asset start again (and remain there in the paused state) instead of going through the end of stream process to allow the app to jump to the next asset.
Nevertheless, as that will be addressed by the new #1645 issue (split from this ticket by @modeveci), as it is related with the recursive seek, I'm happy to close this ticket and track the root cause in the new one.