fix(cache): leave no empty cache file after a failed cached call

cache_in_file created the cache file before it called the function. When
the call raised, e.g. a provider request answered with HTTP 500, the
empty file stayed in the store until it expired, and the next call logged
"Read failed: Ran out of input" before it called the function again. The
function is now called first; a failed call leaves the store unchanged.

Refs #1571

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Assisted-by: Claude Code (claude-opus-5-5)
This commit is contained in:
Christin
2026-11-20 18:50:25 +01:00
committed by Bobby Noelte
co-authored by Claude Opus 5.5
parent c8f793c203
commit 14acf759a7
2 changed files with 24 additions and 2 deletions
+3 -2
View File
@@ -1096,7 +1096,9 @@ def cache_in_file(
# Fail gracefully - recreate the cache file in this call
call_force_update = True
if call_force_update or cache_file is None:
# Otherwise, call the function and save its result to the cache
# Otherwise, call the function and save its result to the cache.
# Call first: a call that raises leaves no empty cache file behind.
result = func(*args, **kwargs)
logger.debug("Created cache file for function: " + func.__name__)
cache_file = CacheFileStore().create(
key,
@@ -1107,7 +1109,6 @@ def cache_in_file(
until_date=expiry["until_date"],
with_ttl=expiry["with_ttl"],
)
result = func(*args, **kwargs)
try:
# Assure we have an empty file
cache_file.truncate(0)