Skip to content

fix(prompts): protect cache from stale background refreshes - #2106

Open
Aafimalek wants to merge 2 commits into
langfuse:mainfrom
Aafimalek:fix/prompt-refresh-race
Open

Aafimalek wants to merge 2 commits into
langfuse:mainfrom
Aafimalek:fix/prompt-refresh-race

Conversation

@Aafimalek

@Aafimalek Aafimalek commented Nov 6, 2026 •

Copy link
Copy Markdown

What does this PR do?

An expired prompt can start a background refresh while another operation invalidates or replaces its cache entry. If the old request finishes afterward, it can restore stale content or delete the newer entry after a 404.

Update or delete the cache entry only when it is still the same entry that scheduled the refresh. Foreground fetches retain their existing behavior.

Type of change

  • Bug fix

Verification

uv run --frozen pytest -q tests/unit/test_prompt.py
uv run --frozen pytest -n 4 --dist worksteal tests/unit
uv run --frozen ruff check .
uv run --frozen ruff format --check .
uv run --frozen mypy langfuse --no-error-summary

The regression exercises a delayed successful response and a delayed 404, both after invalidation and after replacement with a newer entry. Before the fix, the successful response overwrote the newer prompt and the 404 deleted it. The full unit suite passed with 1,019 passed and 2 skipped, using local test credentials.

Checklist

  • Diff reviewed against code_review.md.
  • Regression tests added for both outcomes.
  • Generated files and credentials excluded from the diff.

RetriggerView in GreptileConfidence Score: 5/5

The PR appears safe to merge.

Summary

Background refreshes now change the cache only when the entry that started the refresh is still current. Foreground fetches keep their existing behavior.

  • Background refreshes only change the cache entry that started them.

Reviews (2) · Last reviewed commit: "test(prompts): verify background refresh..." · Reviewed by Greptile

@claude claude Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Claude Code Review

This pull request is from a fork — automated review is disabled. A repository maintainer can comment @claude review to run a one-time review.

Comment thread tests/unit/test_prompt.py Resolved

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant