Add check for audio file existence#8529
Conversation
Handle case where audio file may not exist yet.
There was a problem hiding this comment.
Code Review
This pull request adds a check to ensure_wav in astrbot/core/utils/media_utils.py to return the audio_path as-is if the file does not exist yet, handling potential race conditions. However, the review comment points out that this check will incorrectly trigger for remote URLs (e.g., starting with http), bypassing their download and conversion. A suggestion is provided to only apply this check if the path is not a remote URL.
Important
The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.
| if not os.path.exists(audio_path): | ||
| # File not available yet (e.g. napcat race condition); | ||
| # return the path as-is so upstream retry logic can handle it later. | ||
| return audio_path |
There was a problem hiding this comment.
If audio_path is a remote URL (e.g., starting with http), os.path.exists(audio_path) will return False. This causes ensure_wav to return the URL as-is, bypassing the conversion to a local WAV file. This breaks the expected behavior where remote audio URLs are downloaded and converted to local WAV files.
We should only return early if the path is a local path and does not exist.
| if not os.path.exists(audio_path): | |
| # File not available yet (e.g. napcat race condition); | |
| # return the path as-is so upstream retry logic can handle it later. | |
| return audio_path | |
| if not audio_path.startswith("http") and not os.path.exists(audio_path): | |
| # File not available yet (e.g. napcat race condition); | |
| # return the path as-is so upstream retry logic can handle it later. | |
| return audio_path |
There was a problem hiding this comment.
Hey - I've left some high level feedback:
- Consider logging or otherwise surfacing when the audio file path does not yet exist so that diagnosing frequent race conditions or misconfigurations is easier than silently returning the path.
- If this utility might receive
Pathobjects in addition to strings, it may be safer to normalizeaudio_pathtostror usePaththroughout before callingos.path.exists.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- Consider logging or otherwise surfacing when the audio file path does not yet exist so that diagnosing frequent race conditions or misconfigurations is easier than silently returning the path.
- If this utility might receive `Path` objects in addition to strings, it may be safer to normalize `audio_path` to `str` or use `Path` throughout before calling `os.path.exists`.Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
Handle case where audio file may not exist yet.
Modifications / 改动点
Screenshots or Test Results / 运行截图或测试结果
Checklist / 检查清单
😊 If there are new features added in the PR, I have discussed it with the authors through issues/emails, etc.
/ 如果 PR 中有新加入的功能,已经通过 Issue / 邮件等方式和作者讨论过。
👀 My changes have been well-tested, and "Verification Steps" and "Screenshots" have been provided above.
/ 我的更改经过了良好的测试,并已在上方提供了“验证步骤”和“运行截图”。
🤓 I have ensured that no new dependencies are introduced, OR if new dependencies are introduced, they have been added to the appropriate locations in
requirements.txtandpyproject.toml./ 我确保没有引入新依赖库,或者引入了新依赖库的同时将其添加到
requirements.txt和pyproject.toml文件相应位置。😮 My changes do not introduce malicious code.
/ 我的更改没有引入恶意代码。
Summary by Sourcery
Bug Fixes: