hyunwoo jo 6e2392f366 HLS/DASH: Fix dispose() to cleanup files after unpublish (#4618)
# HLS/DASH: Fix dispose() to cleanup files after unpublish

## Summary
Fixes a bug where HLS/DASH files are not deleted after the configured
`hls_dispose`/`dash_dispose` timeout.

## Problem
When a stream is unpublished:
1. `on_unpublish()` is called and sets `enabled_ = false`
2. After the dispose timeout, `cycle()` calls `dispose()`
3. `dispose()` immediately returns due to `if (!enabled_)` check at line
2722 (HLS) and line 891 (DASH)
4. `controller_->dispose()` is never called
5. Files remain on disk indefinitely

**Observed behavior**:
- Stream stopped at 11:32:42
- `dispose()` called at 11:33:14 (after 30s timeout)
- Log shows "hls cycle to dispose hls" but no "gracefully dispose hls"
message
- Files remain on disk

## Root Cause
Commit 550760f2d introduced an early return in `dispose()` when
`!enabled_`, which prevents file cleanup after `on_unpublish()` has
already been called and set `enabled_` to false.

## Solution
Reorder the logic in `dispose()` to:
1. Check if dispose is enabled (hls_dispose/dash_dispose > 0) first
2. Call `on_unpublish()` only if `enabled_` is still true (prevents
duplicate calls)
3. Always call `controller_->dispose()` to cleanup files when dispose
timeout occurs

This ensures files are properly cleaned up while still preventing
duplicate `on_unpublish()` calls.

## Changes Made
- **trunk/src/app/srs_app_hls.cpp** (lines 2718-2734): Reordered
dispose() logic
- **trunk/src/app/srs_app_dash.cpp** (lines 887-902): Reordered
dispose() logic
- **trunk/doc/CHANGELOG.md**: Added v7.0.137 entry

## Testing Recommendation

To verify the fix:

1. Start RTMP stream to `/live/test`:
   ```bash
   ffmpeg -re -i test.mp4 -c copy -f flv rtmp://localhost:1935/live/test
   ```

2. Wait for HLS segments to be created:
   ```bash
   ls -la /path/to/hls/live/test/
   ```

3. Stop the stream (Ctrl+C)

4. Wait for `hls_dispose` timeout (default 120s, or 30s with your
config):
   ```bash
# Watch logs for "hls cycle to dispose hls" and "gracefully dispose hls"
   tail -f srs.log
   ```

5. Verify files are deleted:
   ```bash
   ls -la /path/to/hls/live/test/
   # Should be empty or directory removed
   ```

**Expected results**:
- Before fix: Files remain on disk
- After fix: Files are deleted, logs show "gracefully dispose hls"

## Impact
- **Risk**: Low - minimal logic change, only reordering of checks
- **Breaking changes**: None
- **Performance**: No impact
- **Compatibility**: Fixes existing bug, improves expected behavior

## Checklist
- [x] Code follows project style
- [x] Both HLS and DASH are fixed
- [x] CHANGELOG updated
- [x] Tested locally (recommended before merge)
- [x] No breaking changes

## Related Issues
- Regression introduced in: 550760f2d
- Related to: #865 (hls_dispose feature)

---------

Co-authored-by: Jacob Su <suzp1984@gmail.com>
2026-02-03 19:36:11 -05:00
2025-10-28 10:04:53 -04:00
2025-12-13 08:24:40 -05:00
2025-12-03 10:41:42 -05:00
2023-01-30 14:20:37 +08:00

SRS(Simple Realtime Server)

SRS/7.0 (Kai) is a simple, high-efficiency, and real-time video server, supporting RTMP/WebRTC/HLS/HTTP-FLV/SRT/MPEG-DASH/GB28181, Linux/macOS, X86_64/ARMv7/AARCH64/M1/RISCV/LOONGARCH/MIPS, with codec support for H.264, H.265, AV1, VP9, AAC, Opus, and G.711, and essential features.

SRS Overview

Note: For more details on the single-node architecture for SRS, please visit the following link.

SRS is licenced under MIT, and some third-party libraries are distributed under their licenses.

Usage

Please check the Getting Started guide in English or Chinese. We highly recommend using SRS with docker:

docker run --rm -it -p 1935:1935 -p 1985:1985 -p 8080:8080 \
    -p 8000:8000/udp -p 10080:10080/udp ossrs/srs:6

Tips: If you're in China, use this image registry.cn-hangzhou.aliyuncs.com/ossrs/srs:6 for faster speed.

Open http://localhost:8080/ to verify, and then stream using the following FFmpeg command:

ffmpeg -re -i ./doc/source.flv -c copy -f flv -y rtmp://localhost/live/livestream

Alternatively, stream by OBS using the following configuration:

  • Service: Custom
  • Server: rtmp://localhost/live
  • Stream Key: livestream

Play the following streams using media players:

If you'd like to use WebRTC, convert RTMP to WebRTC, or convert WebRTC to RTMP, please check out the wiki documentation in either English or Chinese.

To learn more about RTMP, HLS, HTTP-FLV, SRT, MPEG-DASH, WebRTC protocols, clustering, HTTP API, DVR, and transcoding, please check the documents in English or Chinese.

If you want to use an IDE, VSCode is recommended. VSCode supports macOS, and Linux platforms. The settings are ready. All you need to do is open the folder with VSCode and enjoy the efficiency brought by the IDE. See VSCode README for details.

Sponsor

Would you like additional assistance from us? By becoming a sponsor or backer of SRS, we can provide you with the support you need:

  • Backer: $5 per month, online text chat support through Discord.
  • Sponsor: $100 per month, online text chat plus online meeting support.

Please visit OpenCollective to become a backer or sponsor, and send us a direct message on Discord. We are currently providing support to the developers listed below:

At SRS, our goal is to create a free, open-source community that helps developers all over the world build high-quality streaming and RTC platforms for their businesses.

Contributing

The maintainers, and contributors are listed here. The maintainers who made significant contributions and maintained parts of SRS are listed below, ranked by the number of commits:

  • Winlin: Founder of the project, focusing on ST and Issues/PR. Responsible for architecture and maintenance.
  • XiaoZhihong: Concentrates on WebRTC/QUIC and SRT, with expertise in network QoS. Contributed to ARM on ST and was the original contributor for WebRTC.
  • ChenHaibo: Specializes in GB28181 and HTTP API, contributing to patches for FFmpeg with WHIP.
  • ZhangJunqin: Focused on H.265, Prometheus Exporter, and API module.
  • XiaLixin: Specializes in GB28181, with expertise in live streaming and WebRTC.
  • Jacob Su: Jacob Su has contributed to various modules of SRS.
  • ShiWei: Specializes in SRT and H.265, maintaining SRT and FLV patches for FFmpeg. An expert in codecs and FFmpeg.
  • ChenGuanghua: Focused on WebRTC/QoS and introduced the Asan toolchain to SRS.
  • LiPeng: Concentrates on WebRTC and contributes to memory management and smart pointers.
  • ZhaoWenjie: One of the earliest contributors, focusing on HDS. Has expertise in client technology.
  • WuPengqiang: Focused on H.265, initially contributed to the FFmpeg module in SRS for transcoding AAC with OPUS for WebRTC.

A huge THANK YOU goes out to:

We're really thankful to everyone in the community for helping us find bugs and improve the project. To stay in touch and keep helping our community, please check out this guide.

LICENSE

SRS is licenced under MIT, and some third-party libraries are distributed under their licenses.

Releases

Features

Please read FEATURES.

Changelog

Please read CHANGELOG.

Performance

Please read PERFORMANCE.

Architecture

Please read ARCHITECTURE.

Ports

Please read PORTS.

APIs

Please read APIS.

Mirrors

Please read MIRRORS.

Dockers

Please read DOCKERS.

Beijing, 2013.10
Winlin

Description
SRS is a simple, high-efficiency, real-time video server supporting RTMP, WebRTC, HLS, HTTP-FLV, SRT, MPEG-DASH, and GB28181.
Readme 389 MiB
Languages
C++ 87.5%
JavaScript 5.7%
HTML 3.4%
Shell 2.3%
Go 0.6%
Other 0.2%