1
0
Fork 0
mirror of https://github.com/ossrs/srs.git synced 2025-02-13 03:41:55 +00:00
srs/trunk
Winlin 740f0d38ec
Edge: Fix flv edge crash when http unmount. v6.0.154 v7.0.13 (#4166)
Edge FLV is not working because it is stuck in an infinite loop waiting.
Previously, there was no need to wait for exit since resources were not
being cleaned up. Now, since resources need to be cleaned up, it must
wait for all active connections to exit, which causes this issue.

To reproduce the issue, start SRS edge, run the bellow command and press
`CTRL+C` to stop the request:

```bash
curl http://localhost:8080/live/livestream.flv -v >/dev/null
```

It will cause edge to fetch stream from origin, and free the consumer
when client quit. When `SrsLiveStream::do_serve_http` return, it will
free the consumer:

```cpp
srs_error_t SrsLiveStream::do_serve_http(ISrsHttpResponseWriter* w, ISrsHttpMessage* r) {
    SrsUniquePtr<SrsLiveConsumer> consumer(consumer_raw);
```

Keep in mind that in this moment, the stream is alive, because only set
to not alive after this function return:

```cpp
    alive_viewers_++;
    err = do_serve_http(w, r); // Free 'this' alive stream.
    alive_viewers_--; // Crash here, because 'this' is freed.
```

When freeing the consumer, it will cause the source to unpublish and
attempt to free the HTTP handler, which ultimately waits for the stream
not to be alive:

```cpp
SrsLiveConsumer::~SrsLiveConsumer() {
    source_->on_consumer_destroy(this);

void SrsLiveSource::on_consumer_destroy(SrsLiveConsumer* consumer) {
    if (consumers.empty()) {
        play_edge->on_all_client_stop();

void SrsLiveSource::on_unpublish() {
    handler->on_unpublish(req);

void SrsHttpStreamServer::http_unmount(SrsRequest* r) {
    if (stream->entry) stream->entry->enabled = false;

    for (; i < 1024; i++) {
        if (!cache->alive() && !stream->alive()) {
            break;
        }
        srs_usleep(100 * SRS_UTIME_MILLISECONDS);
    }
```

After 120 seconds, it will free the stream and cause SRS to crash
because the stream is still active. In order to track this potential
issue, also add an important warning log:

```cpp
srs_warn("http: try to free a alive stream, cache=%d, stream=%d", cache->alive(), stream->alive());
```

SRS may crash if got this log.

---------

Co-authored-by: Jacob Su <suzp1984@gmail.com>
2024-09-01 06:44:35 +08:00
..
3rdparty ASAN: Support coroutine context switching and stack tracing (#4153) 2024-08-22 17:12:39 +08:00
auto Fix crash when quiting. v6.0.151 v7.0.10 (#4157) 2024-08-24 22:40:39 +08:00
conf GB28181: Support external SIP server. v6.0.144 (#4101) 2024-07-27 09:06:12 +08:00
doc Edge: Fix flv edge crash when http unmount. v6.0.154 v7.0.13 (#4166) 2024-09-01 06:44:35 +08:00
etc/init.d Squash: Fix bugs 2022-01-13 18:26:28 +08:00
gdb The identifier "ShowCouroutines" needs to be modified to "ShowCoroutines" in order to rectify the typographical error. v6.0.63 (#3703) 2023-07-27 08:12:39 +08:00
ide/srs_clion UniquePtr: Support SrsUniquePtr to replace SrsAutoFree. v6.0.136 (#4109) 2024-07-09 10:29:36 +08:00
modules Moduels: Refine the description 2021-04-21 11:18:35 +08:00
packaging Windows: Refine cygwin pipeline (#3260) 2022-11-21 14:32:26 +08:00
research ASAN: Support coroutine context switching and stack tracing (#4153) 2024-08-22 17:12:39 +08:00
scripts UniquePtr: Support SrsUniquePtr to replace SrsAutoFree. v6.0.136 (#4109) 2024-07-09 10:29:36 +08:00
src Edge: Fix flv edge crash when http unmount. v6.0.154 v7.0.13 (#4166) 2024-09-01 06:44:35 +08:00
usr/lib/systemd/system Squash: Support RPM. Fix bugs. 2021-11-04 07:37:17 +08:00
.gitignore For #2136: API: Cleanup no active streams for statistics. v5.0.42 2022-08-19 19:45:19 +08:00
AUTHORS.md Update contributors. 2023-09-28 11:03:34 +08:00
AUTHORS.txt Update AUTHORS 2021-08-25 07:43:35 +08:00
configure ASAN: Support coroutine context switching and stack tracing (#4153) 2024-08-22 17:12:39 +08:00
Dockerfile.builds Use new cache image name. v6.0.86 (#3815) 2023-10-08 07:43:14 -05:00
Dockerfile.cov Asan: Refine asan warning message for macOS. 2022-12-31 21:20:51 +08:00
Dockerfile.pkg Merge branch v5.0.103 into develop 2022-12-03 21:15:22 +08:00
Dockerfile.test Fix opus delay options, use ffmpeg-opus in docker test. v6.0.102 (#3883) 2023-11-22 14:58:30 +08:00