Skip to content

Commit 21e4411

Browse files
authored
Fix dangling promise in unstable-cache (#79248)
While investigating an issue with intermittent `unstable_cache` calls failing to set noticed we have these dangling promises which cause "racey" behavior with setting to a cache in environments where the function stops after the request is finished. These have been present since b305c2d altho since racey may or may not have been noticed. x-ref: [slack thread](https://vercel.slack.com/archives/C02K2HCH5V4/p1747322956802819?thread_ts=1746633220.634939&cid=C02K2HCH5V4)
1 parent 733e4d6 commit 21e4411

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

.changeset/loose-cows-pump.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'next': patch
3+
---
4+
5+
Fix dangling promise in unstable_cache

packages/next/src/server/web/spec-extension/unstable-cache.ts

+13-2
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,14 @@ export function unstable_cache<T extends Callback>(
275275
)
276276

277277
if (!workStore.isDraftMode) {
278-
cacheNewResult(
278+
if (!workStore.pendingRevalidates) {
279+
workStore.pendingRevalidates = {}
280+
}
281+
282+
// We need to push the cache result promise to pending
283+
// revalidates otherwise it won't be awaited and is just
284+
// dangling
285+
workStore.pendingRevalidates[invocationKey] = cacheNewResult(
279286
result,
280287
incrementalCache,
281288
cacheKey,
@@ -330,7 +337,11 @@ export function unstable_cache<T extends Callback>(
330337
cb,
331338
...args
332339
)
333-
cacheNewResult(
340+
341+
// we need to wait setting the new cache result here as
342+
// we don't have pending revalidates on workStore to
343+
// push to and we can't have a dangling promise
344+
await cacheNewResult(
334345
result,
335346
incrementalCache,
336347
cacheKey,

0 commit comments

Comments
 (0)