AI: HTTP-FLV: Fix heap-use-after-free crash during stream unmount. v6.0.171 v7.0.50#4432
Merged
winlinvip merged 3 commits intoossrs:developfrom Aug 11, 2025
Merged
AI: HTTP-FLV: Fix heap-use-after-free crash during stream unmount. v6.0.171 v7.0.50#4432winlinvip merged 3 commits intoossrs:developfrom
winlinvip merged 3 commits intoossrs:developfrom
Conversation
winlinvip
added a commit
that referenced
this pull request
Aug 11, 2025
….0.171 (#4432) Fixes a critical heap-use-after-free crash in HTTP-FLV streaming that occurs when a client requests a stream while it's being unmounted asynchronously. - **Issue**: #4429 - Heap-use-after-free crash in `SrsLiveStream::serve_http()` - **Root Cause**: Race condition between coroutines in single-threaded SRS server: 1. **Coroutine A**: HTTP client requests FLV stream → `serve_http()` starts 2. **Coroutine B**: RTMP publisher disconnects → triggers async stream destruction 3. **Async Worker**: Destroys `SrsLiveStream` object while Coroutine A is yielded 4. **Coroutine A**: Resumes and accesses freed memory → **CRASH** 1. **Early viewer registration**: Add HTTP connection to `viewers_` list immediately in `serve_http()` before any I/O operations that could yield 2. **Lifecycle protection**: Split `serve_http()` into wrapper and implementation to ensure proper viewer management 3. **Stream availability checks**: Add fast checks for stream disposal state before critical operations 4. **Improved error handling**: Convert warnings to fatal errors when trying to free alive streams - **`SrsLiveStream::serve_http()`**: Now immediately registers viewer and delegates to `serve_http_impl()` - **`SrsLiveStream::serve_http_impl()`**: Contains the actual HTTP serving logic - **`SrsHttpStreamDestroy::call()`**: Enhanced error handling and longer wait timeout - **Stream state validation**: Added checks for `entry->enabled` before proceeding with stream operations Fixes #4429
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes a critical heap-use-after-free crash in HTTP-FLV streaming that occurs when a client requests a stream while it's being unmounted asynchronously.
Problem
SrsLiveStream::serve_http()serve_http()startsSrsLiveStreamobject while Coroutine A is yieldedSolution
viewers_list immediately inserve_http()before any I/O operations that could yieldserve_http()into wrapper and implementation to ensure proper viewer managementKey Changes
SrsLiveStream::serve_http(): Now immediately registers viewer and delegates toserve_http_impl()SrsLiveStream::serve_http_impl(): Contains the actual HTTP serving logicSrsHttpStreamDestroy::call(): Enhanced error handling and longer wait timeoutentry->enabledbefore proceeding with stream operationsFixes #4429