bluenviron / mediamtx

Ready-to-use SRT / WebRTC / RTSP / RTMP / LL-HLS media server and media proxy that allows to read, publish, proxy, record and playback video and audio streams.
MIT License
12.2k stars 1.53k forks source link

Goroutines of RTCPSender are never stopped #1206

Closed gabridego closed 2 years ago

gabridego commented 2 years ago

Which version are you using?

v0.20.1

Which operating system are you using?

Describe the issue

When a FFmpeg process publishing to the server is stopped and its clients are disconnected, the goroutines associated to the RTCPSender object keep running, leading to possible goroutine leaks:

goroutine 26 [select]:
github.com/aler9/gortsplib/pkg/rtcpsender.(*RTCPSender).run(0xc0000849c0)
    /home/user/go/pkg/mod/github.com/aler9/gortsplib@v0.0.0-20221021150213-7d8e3cf5bc03/pkg/rtcpsender/rtcpsender.go:64 +0x132
created by github.com/aler9/gortsplib/pkg/rtcpsender.New
    /home/user/go/pkg/mod/github.com/aler9/gortsplib@v0.0.0-20221021150213-7d8e3cf5bc03/pkg/rtcpsender/rtcpsender.go:45 +0x10a

To better identify the problem, we modified the run() function in our local installation of gortsplib/pkg/rtcpsender/rtcpsender.go like this:

func (rs *RTCPSender) run() {
    defer close(rs.done)

    t := time.NewTicker(rs.period)
    t_new := time.NewTicker(10 * time.Second)
    defer t.Stop()
    defer t_new.Stop()

    for {
        select {
        case <-t.C:
            report := rs.report(now())
            if report != nil {
                rs.writePacketRTCP(report)
            }

        case <-t_new.C:
            log.Println("===================", rs.packetCount, time.Now())

        case <-rs.terminate:
            log.Println("=================== TERMINATE", time.Now())
            return
        }
    }
}

Even after both publisher and client processes are killed, the first message is printed forever and the loop never reaches its termination.

Describe how to replicate the issue

  1. start the server
  2. publish any stream through FFmpeg: ffmpeg -re -stream_loop -1 -i out.mp4 -c copy -f rtsp rtsp://localhost:8554/mystream
  3. read with any player, such as FFplay: ffplay rtsp://localhost:8554/mystream
  4. kill player and publisher processes

Did you attach the server logs?

yes

2022/10/27 14:38:43 INF [RTSP] [conn 127.0.0.1:48020] opened
2022/10/27 14:38:43 INF [RTSP] [session 433090564] created by 127.0.0.1:48020
2022/10/27 14:38:43 INF [RTSP] [session 433090564] is publishing to path 'mystream', with UDP, 1 track (H264)
2022/10/27 14:38:53 INF [RTSP] [conn 127.0.0.1:41048] opened
2022/10/27 14:38:53 INF [RTSP] [session 676059982] created by 127.0.0.1:41048
2022/10/27 14:38:53 INF [RTSP] [session 676059982] is reading from path 'mystream', with UDP, 1 track (H264)
2022/10/27 14:39:03 =================== 358 2022-10-27 14:39:03.81869608 +0200 CEST m=+22.248182167
2022/10/27 14:39:13 =================== 716 2022-10-27 14:39:13.818089331 +0200 CEST m=+32.247575401
2022/10/27 14:39:21 INF [RTSP] [conn 127.0.0.1:41048] closed (EOF)
2022/10/27 14:39:23 =================== 1070 2022-10-27 14:39:23.817989394 +0200 CEST m=+42.247475480
2022/10/27 14:39:33 =================== 1428 2022-10-27 14:39:33.818080162 +0200 CEST m=+52.247566239
2022/10/27 14:39:36 INF [RTSP] [session 433090564] destroyed (teared down by 127.0.0.1:48020)
2022/10/27 14:39:36 INF [RTSP] [session 676059982] destroyed (terminated)
2022/10/27 14:39:36 INF [RTSP] [conn 127.0.0.1:48020] closed (EOF)
2022/10/27 14:39:43 =================== 1525 2022-10-27 14:39:43.823728934 +0200 CEST m=+62.253214995
2022/10/27 14:39:53 =================== 1525 2022-10-27 14:39:53.817958749 +0200 CEST m=+72.247444823
2022/10/27 14:40:03 =================== 1525 2022-10-27 14:40:03.821344118 +0200 CEST m=+82.250830192
2022/10/27 14:40:13 =================== 1525 2022-10-27 14:40:13.827152066 +0200 CEST m=+92.256638157
2022/10/27 14:40:23 =================== 1525 2022-10-27 14:40:23.823803003 +0200 CEST m=+102.253289080
2022/10/27 14:40:33 =================== 1525 2022-10-27 14:40:33.823804391 +0200 CEST m=+112.253290461

Last messages are repeated every 10 seconds.

Did you attach a network dump?

no

aler9 commented 1 year ago

Thanks for reporting the bug, it is fixed in v0.20.2.

github-actions[bot] commented 1 year ago

This issue is being locked automatically because it has been closed for more than 6 months. Please open a new issue in case you encounter a similar problem.