Skip to content

TtlCache.invalidate() is lost if it lands while a load is in flight — stale platform summary is re-cached for a full TTL #463

Closed

Description

@chonilius

Problem

src/analytics/ttl-cache.ts backs AnalyticsService.platformSummary(), which is busted by ANALYTICS_PLATFORM_INVALIDATE_EVENT on bounty create/pay and first repository sync:

invalidate(): void {
  this.entry = null;
}

async getOrLoad(loader) {
  ...
  this.inflight = (async () => {
    try {
      const data = await loader();
      this.entry = { data, expiresAt: Date.now() + this.ttlMs };  // ← unconditional
      return data;
    } finally {
      this.inflight = null;
    }
  })();
  return this.inflight;
}

invalidate() only clears entry. It doesn't know about a load that's already running. The sequence:

  1. A request misses the cache and starts loadPlatformSummary(): three queries, counting bounties that don't yet include the new one.
  2. BountiesService.create() or markMergedAndRelease() commits and emits the invalidate event, so entry = null, a no-op.
  3. The in-flight loader resolves with the pre-change counts and writes them into entry with a fresh TTL.
  4. For the next ANALYTICS_PLATFORM_SUMMARY_TTL_MS (60s by default), every homepage request gets the stale totalBounties / totalPaidOut. The invalidation was lost. Concurrent callers joined to the stale inflight promise after the invalidate get the stale value too.

README.md documents that multi-instance deploys can serve a TTL of staleness per process. This is different: it happens within a single process, exactly in the busy periods when invalidation matters most.

Suggested fix

Version the cache so a load that started before an invalidate can't populate it:

private generation = 0;
invalidate() { this.entry = null; this.inflight = null; this.generation++; }
// in the loader:
const gen = this.generation;
const data = await loader();
if (gen === this.generation) this.entry = { data, expiresAt: Date.now() + this.ttlMs };

Also clear inflight on invalidate so callers after the invalidate start a fresh load. Add a unit test: start a load, call invalidate(), resolve the load, and the next getOrLoad calls the loader again.

Related: analytics.service.ts invalidatePlatformSummary.

Activity

  1. added
    bugSomething isn't working
    performancePerformance/optimization issue
    Stellar WaveIssues in the Stellar wave program
    on Sep 27, 2026
  2. Halimatyemisi commented on Sep 27, 2026

    @Halimatyemisi

    @Halimatyemisi has applied to work on this issue as part of the Stellar Wave Program's 9th wave.

    Hi Maintainer, I’ve reviewed the issue and understand the requirements. I’m ready to take it on and deliver a clean solution. Kindly assign it to me.

    ℹ️ Repo Maintainers: To accept this application, review their application or assign @Halimatyemisi to this issue.

  3. Proxima84-code commented on Sep 27, 2026

    @Proxima84-code

    I have submitted a clean, tested fix resolving this race condition in PR #465 (#465).

    • Versioned cache entries with a generation sequence counter.
    • Invalidations now reset both entry and inflight promises.
    • Added comprehensive unit tests in ttl-cache.spec.ts ensuring discarded stale loaders do not repopulate the cache.
  4. iamcracked-dev commented on Sep 27, 2026

    @iamcracked-dev
    Contributor

    @iamcracked-dev has applied to work on this issue as part of the Stellar Wave Program's 9th wave.

    I’d love to help out with this issue. I’ve checked the current codebase/documentation and know exactly what needs to be updated to make this clearer. I will ensure everything aligns perfectly with your existing contribution guidelines.

    ℹ️ Repo Maintainers: To accept this application, review their application or assign @iamcracked-dev to this issue.

  5. drips-wave commented on Sep 27, 2026

    @drips-wave

    Congratulations, @iamcracked-dev! 🎉 Your application was accepted by the repo's maintainers, and the issue is due on September 30, 2026.

    🧑‍💻 @iamcracked-dev: Please resolve the issue such that the repo's maintainers have enough time to review your contribution before the due date. You'll earn Points for completing the issue on-time, which will make you eligible for a share of the Stellar Wave Program's reward pool.

    Warning

    When opening a PR, please link it to this issue to ensure it gets tracked accurately. Points are awarded when this issue is marked as completed by the maintainer.

    🤠 Repo maintainers: Please keep an eye on the contributor's progress and review their work before the due date. You can manage this issue, including adjusting its complexity and points, here.

    🌊 Happy Wave 🌊

  6. drips-wave commented on Sep 27, 2026

    @drips-wave

    This issue has been completed by @iamcracked-dev as part of the Stellar Wave Program's 9th Wave 🥳

    😎 @iamcracked-dev: You earned 100 Points for completing this issue! After the current Wave ends, you'll be eligible for a percentage of the Wave's reward pool based on the percentage of total points you've earned. Learn more here. You can also Leave a review to share your experience working on this issue.

    🧑‍💻 Repo maintainers: How'd the contributor do? Leave a review to share your experience working with them.

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

Metadata

Metadata

Labels

Stellar WaveIssues in the Stellar wave programbugSomething isn't workinggood first issueGood for newcomersperformancePerformance/optimization issue

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions