mirror of
https://github.com/ossrs/srs.git
synced 2026-02-08 01:22:53 +08:00
# 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 Commit550760f2dintroduced 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>