Skip to content

Don't use a streaming player after its handler destroys it - #31659

Open
oviron wants to merge 2 commits into
telegramdesktop:devfrom
oviron:tdesktop-streaming-player-destroyed
Open

oviron wants to merge 2 commits into
telegramdesktop:devfrom
oviron:tdesktop-streaming-player-destroyed

Conversation

@oviron

@oviron oviron commented Nov 19, 2026 •

Copy link
Copy Markdown

A handler of Streaming::Player::updates() can destroy the player that is sending the update, and the player then goes on reading and writing its own fields.

The automatic quality switch does it. Player::trackReceivedTill() sends PreloadedVideo through trackSendReceivedTill(). Streaming::Document::handleUpdate() answers with checkSwitchToHigherQuality(), which fires switchQualityRequests, and the owner replaces its stream right there: Gif::createStreamedPlayer(), OverlayWidget::applyVideoQuality() or Pip::applyVideoQuality(). If that owner held the last shared_ptr<Document>, the Document and its Player are destroyed before _updates.fire() returns. Back in trackReceivedTill() the player reads _pauseReading, _information, _audio, _video and _options, may set _pauseReading, and videoReceivedTill() then calls checkResumeFromWaitingForData().

How bad it is depends on the owner. Gif and Pip create the new stream before they drop the old one, so the weak_ptr in Data::Streaming still holds the old allocation: the object is destroyed, the memory is not freed yet. OverlayWidget::applyVideoQuality() calls clearStreaming() first and initStreaming() after it, and sharedDocument() erases the expired entry (PruneDestroyedAndSet) while it registers the new stream. The memory is freed, and the reads that follow are a use after free. It takes the media viewer as the only owner of the stream and the quality on Auto.

The same happens after Information: Gif::streamingReady() calls stopAnimation() when the real frame is over the inline limit, and provideStartInformation() reads _stage and _paused after the update.

The first commit checks _sessionGuard after these updates, the way trackPlayedTill(), start() and updatePausedState() already do:

  • in trackReceivedTill() after trackSendReceivedTill();
  • in audioReceivedTill() and videoReceivedTill() before checkResumeFromWaitingForData();
  • at the end of trackPlayedTill();
  • in provideStartInformation() after the update.

stop() clears the guard too. The code skipped in that case has no effect that outlives the stop: the only field it could change is _pauseReading, and the next play() resets it.

Audio track. AudioTrack::playPosition() has the same problem one level down. Its handler fires _waitingForData, the player sends WaitingForData{ true }, and Document::waitingChange() checks for a lower quality at once if the waiting indicator is still animating. When the owner replaces the stream there, Player::stop() destroys the track, and the handler then reads _stream.duration and assigns _playPosition. The track is its own allocation, so its memory is freed whichever owner did it. The second commit gives AudioTrack a base::has_weak_ptr base and returns from the handler when the track is gone.

Found by reading the code while working on #31657; I have not seen a crash from it. Our fork of AyuGram, which tracks Telegram Desktop 7.4.4, ships these commits in AyuGram 7.4.4-3 (universal macOS and Windows x64, built by CI). The macOS build starts and runs; the behaviour itself has not been checked by observation yet.

This pull request was prepared with the help of an AI assistant (Claude Code).

🤖 Generated with Claude Code

@oviron

oviron commented Nov 19, 2026

Copy link
Copy Markdown
Author

Our fork ships these two commits in AyuGram 7.4.4-3, universal macOS and Windows x64, built by CI. The macOS build starts and runs. No code change since the pull request was opened.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant