Engineering note
Making a video service fail properly: timeouts, a blocked event loop, and a TypeError
CloakMotion's video work happens in a separate Python service (FastAPI) that calls GPT, text-to-speech, an image generator and a chain of ffmpeg commands. When I audited it, nothing was broken on the happy path. The problems were about what happens when something goes slow or wrong. There were three.
1. Nothing had a timeout
An outbound HTTP call with no timeout waits as long as the other side lets it. If GPT, the voice service or a file download hangs, so does the request, and it holds a worker while it waits. Every ffmpeg command had the same problem: a wedged process could block forever.
I added a limit to every outbound call, a 10-second connect timeout and a 120-second read timeout, and a ceiling for each ffmpeg or ffprobe command:
FFMPEG_TIMEOUT_SECONDS = 600 # a single invocation, for 30-90 second videos
subprocess.run(command, stdout=subprocess.DEVNULL, stderr=subprocess.PIPE,
check=True, timeout=FFMPEG_TIMEOUT_SECONDS)
When a command times out, the code logs which step timed out and raises, so the job fails visibly instead of hanging.
2. Four routes blocked the whole service
Four routes (script generation, image generation, the ElevenLabs character check and the render) were declared async def but called slow blocking code inside. In FastAPI, an async def route runs on the event loop, so a blocking call inside it freezes every other request on that worker until it finishes. The fix was one word per route:
-async def render_video(request: GeneratePromptRequest):
+def render_video(request: GeneratePromptRequest):
A plain def route runs in a thread pool, so a slow render no longer stops the rest of the service from answering.
3. A failed GPT call crashed with the wrong error
The GPT helper returned None when the API call failed. The calling code then checked "error" in result, which on None raises TypeError: argument of type 'NoneType' is not iterable. A temporary API failure turned into a confusing crash in the middle of a render. The code now checks for None first and logs "no response", so the failure is reported as what it is and the job can degrade instead of crashing.
What this doesn't cover
- These are fixes found by reading the code in an audit. CloakMotion has no automated test suite, so no tests prove them.
- The 600-second ceiling and the 120-second read timeout are limits I chose for 30 to 90 second videos. They'd need tuning for longer ones.
- A timeout makes a job fail cleanly. It doesn't retry it.
What I'd tell a client
For a service that depends on other services, the question to ask early isn't "does it work?" but "what happens when one of them stalls?" Check that every external call has a time limit, that blocking work doesn't sit on the event loop, and that an empty response is handled before it's used. Each fix is small, and together they decide whether a bad hour at a vendor becomes your outage.
The code from this post is public: https://github.com/hassan91199/cloakmotion-highlights/tree/main/video-service.
If you have a Python service that hangs or crashes under slow dependencies, book a call with me on Upwork.