Spaces:
Runtime error
Runtime error
Commit ·
399ad59
1
Parent(s): f1803be
fix(hls): snapshot read playlist/segment to avoid Content-Length race
Browse filesFileResponse stat'd then streamed the file while ffmpeg was rolling
playlist.m3u8 / .ts segments concurrently, so the response body grew
past the precomputed Content-Length and uvicorn raised
"Response content longer than Content-Length". Read whole bytes into
memory and return Response so Content-Length matches what we send.
- app/api/routes_hls.py +28 -11
app/api/routes_hls.py
CHANGED
|
@@ -12,7 +12,7 @@ import logging
|
|
| 12 |
from pathlib import Path
|
| 13 |
|
| 14 |
from fastapi import APIRouter, Depends
|
| 15 |
-
from fastapi.responses import
|
| 16 |
|
| 17 |
from app.api.deps import get_worker
|
| 18 |
from app.core.errors import COMMON_ERROR_RESPONSES, ApiException
|
|
@@ -79,11 +79,21 @@ async def hls_playlist(
|
|
| 79 |
message="HLS playlist warming up; retry shortly",
|
| 80 |
details={"camera_id": camera_id},
|
| 81 |
)
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 87 |
|
| 88 |
|
| 89 |
@router.get(
|
|
@@ -114,8 +124,15 @@ async def hls_segment(
|
|
| 114 |
message="segment not found (rolled off the playlist)",
|
| 115 |
details={"camera_id": camera_id, "segment": name},
|
| 116 |
)
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
from pathlib import Path
|
| 13 |
|
| 14 |
from fastapi import APIRouter, Depends
|
| 15 |
+
from fastapi.responses import Response
|
| 16 |
|
| 17 |
from app.api.deps import get_worker
|
| 18 |
from app.core.errors import COMMON_ERROR_RESPONSES, ApiException
|
|
|
|
| 79 |
message="HLS playlist warming up; retry shortly",
|
| 80 |
details={"camera_id": camera_id},
|
| 81 |
)
|
| 82 |
+
# Snapshot read instead of FileResponse. ffmpeg rewrites playlist.m3u8 every
|
| 83 |
+
# ~segment_seconds via atomic rename, but FileResponse computes Content-Length
|
| 84 |
+
# from os.stat() then streams the file — if ffmpeg replaces the file mid-send,
|
| 85 |
+
# uvicorn raises "Response content longer than Content-Length". Reading the
|
| 86 |
+
# whole bytes upfront pegs Content-Length to exactly what we send.
|
| 87 |
+
try:
|
| 88 |
+
body = pl.read_bytes()
|
| 89 |
+
except (FileNotFoundError, OSError):
|
| 90 |
+
raise ApiException(
|
| 91 |
+
status_code=503,
|
| 92 |
+
code=ErrorCode.service_unavailable,
|
| 93 |
+
message="HLS playlist rotating; retry shortly",
|
| 94 |
+
details={"camera_id": camera_id},
|
| 95 |
+
)
|
| 96 |
+
return Response(content=body, media_type=_HLS_PLAYLIST_TYPE, headers=_HLS_HEADERS)
|
| 97 |
|
| 98 |
|
| 99 |
@router.get(
|
|
|
|
| 124 |
message="segment not found (rolled off the playlist)",
|
| 125 |
details={"camera_id": camera_id, "segment": name},
|
| 126 |
)
|
| 127 |
+
# Snapshot read — see playlist handler for rationale (avoid race with ffmpeg
|
| 128 |
+
# rewriting/rolling segments).
|
| 129 |
+
try:
|
| 130 |
+
body = seg_path.read_bytes()
|
| 131 |
+
except (FileNotFoundError, OSError):
|
| 132 |
+
raise ApiException(
|
| 133 |
+
status_code=404,
|
| 134 |
+
code=ErrorCode.not_found,
|
| 135 |
+
message="segment rolled off during read",
|
| 136 |
+
details={"camera_id": camera_id, "segment": name},
|
| 137 |
+
)
|
| 138 |
+
return Response(content=body, media_type=_HLS_SEGMENT_TYPE, headers=_HLS_HEADERS)
|