srs/trunk/doc
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
..
Architecture.md GB28181: Fix bug for parsing GB to RTC. 2022-10-07 19:47:34 +08:00
CHANGELOG.md HLS/DASH: Fix dispose() to cleanup files after unpublish (#4618) 2026-02-03 19:36:11 -05:00
Dockers.md Use new cache image name. v6.0.86 (#3815) 2023-10-08 07:43:14 -05:00
Features.md AI: Remove cygwin64, always enable WebRTC, and enforce C++98 compatibility. v7.0.60 (#4447) 2025-08-21 10:03:38 -06:00
ffmpeg-logo.png support ffmpeg filter 2013-12-01 10:54:41 +08:00
ffmpeg-min.png add drawtext filter sample 2013-12-01 12:04:00 +08:00
PERFORMANCE.md Improve README and documents with AI. v5.0.153. v6.0.43 (#3538) 2023-05-12 17:18:30 +08:00
README.md Micro changes and refines. 2022-09-30 17:57:48 +08:00
readme.txt Move doc 2020-01-26 11:14:18 +08:00
Resources.md Merge 4.0release, migrate to new website. 2022-07-31 18:34:18 +08:00
source.200kbps.768x320.flv update readme, add hls(audio-only) support to readme. update sample flv from h264+mp3 to h264+aac 2014-03-21 11:38:02 +08:00
source.flv SquashSRS4: Support RTC2RTMP. 2021-05-01 22:15:57 +08:00
srs-logo.ico Windows: Support cygwin pipline and packager. v5.0.89 (#3257) 2022-11-20 15:02:08 +08:00
srs-logo.png remove the libfreetype for it fullfill with bugs and always cause build failed. 0.9.139 2014-06-29 10:43:53 +08:00