Skip to content

Fix DashMap read-write self-deadlock in task_cache causing hangs#92210

Merged
lukesandberg merged 1 commit into
canaryfrom
sokra/hanging
Apr 1, 2026
Merged

Fix DashMap read-write self-deadlock in task_cache causing hangs#92210
lukesandberg merged 1 commit into
canaryfrom
sokra/hanging

Conversation

@sokra
Copy link
Copy Markdown
Member

@sokra sokra commented Apr 1, 2026

What?

Fixes a deadlock in the Turbopack backend's task_cache DashMap that causes next to hang during incremental builds with persistent caching.

Why?

In get_or_create_persistent_task and get_or_create_transient_task, self.task_cache.get(&task_type) returns a dashmap::Ref that holds a read lock on a DashMap shard. Although the TaskId was copied out with let task_id = *task_id, the original Ref was not dropped until the end of the if let block — which includes the call to ConnectChildOperation::run.

ConnectChildOperation::run can trigger AggregationUpdateQueue::processfor_each_task_allprepare_tasks_with_callback, which calls task_cache.entry(task_type).or_insert(task_id) — requiring a write lock on a task_cache shard. When this targets the same shard that the current thread already holds a read lock on, the thread self-deadlocks because dashmap's RawRwLock is not reentrant.

Once one thread self-deadlocks, all other tokio worker threads needing that shard pile up behind it, eventually causing a complete system hang where every worker is stuck in dashmap::lock::RawRwLock::lock_exclusive_slow.

This was observed as a hang during the 7th incremental build with persistent caching of a large application, with a macOS sample trace showing all tokio-runtime-worker threads blocked on _pthread_cond_wait inside lock_exclusive_slow on the DashMap<Arc<CachedTaskType>, TaskId>.

How?

Use .map(|r| *r) to copy the TaskId out and drop the Ref (releasing the read lock) before calling ConnectChildOperation::run:

// Before: Ref lives until end of `if let` block
if let Some(task_id) = self.task_cache.get(&task_type) {
    let task_id = *task_id;
    ConnectChildOperation::run(...); // read lock still held!
}

// After: Ref dropped by .map(), read lock released
if let Some(task_id) = self.task_cache.get(&task_type).map(|r| *r) {
    ConnectChildOperation::run(...); // no lock held
}

Applied to both get_or_create_persistent_task and get_or_create_transient_task.

The `task_cache.get()` call returns a `dashmap::Ref` that holds a read
lock on a DashMap shard. The `Ref` was kept alive until the end of the
`if let` block, which includes `ConnectChildOperation::run`. That
operation can re-enter `task_cache` with a write lock via
`prepare_tasks_with_callback` -> `task_cache.entry()`. When the write
targets the same shard that's already read-locked by the current thread,
the thread self-deadlocks (dashmap's RwLock is not reentrant). Once one
thread deadlocks, all other threads needing that shard block too, causing
a full system hang during incremental builds with persistent caching.

Fix: use `.map(|r| *r)` to copy the TaskId and drop the Ref (releasing
the read lock) before calling ConnectChildOperation::run.

Co-Authored-By: Claude <noreply@anthropic.com>
@nextjs-bot nextjs-bot added created-by: Turbopack team PRs by the Turbopack team. Turbopack Related to Turbopack with Next.js. labels Apr 1, 2026
@codspeed-hq
Copy link
Copy Markdown

codspeed-hq Bot commented Apr 1, 2026

Merging this PR will improve performance by 3.19%

⚡ 1 improved benchmark
✅ 16 untouched benchmarks
⏩ 3 skipped benchmarks1

Performance Changes

Mode Benchmark BASE HEAD Efficiency
Simulation packages-bundle.js[full] 1,019.4 ms 987.9 ms +3.19%

Comparing sokra/hanging (36f2ee1) with canary (a0ee1b8)

Open in CodSpeed

Footnotes

  1. 3 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports.

@nextjs-bot
Copy link
Copy Markdown
Contributor

nextjs-bot commented Apr 1, 2026

Tests Passed

@nextjs-bot
Copy link
Copy Markdown
Contributor

Stats from current PR

✅ No significant changes detected

📊 All Metrics
📖 Metrics Glossary

Dev Server Metrics:

  • Listen = TCP port starts accepting connections
  • First Request = HTTP server returns successful response
  • Cold = Fresh build (no cache)
  • Warm = With cached build artifacts

Build Metrics:

  • Fresh = Clean build (no .next directory)
  • Cached = With existing .next directory

Change Thresholds:

  • Time: Changes < 50ms AND < 10%, OR < 2% are insignificant
  • Size: Changes < 1KB AND < 1% are insignificant
  • All other changes are flagged to catch regressions

⚡ Dev Server

Metric Canary PR Change Trend
Cold (Listen) 456ms 455ms ▁█▇▁▁
Cold (Ready in log) 444ms 443ms ▁█▇▁▁
Cold (First Request) 832ms 825ms ▁█▆▁▁
Warm (Listen) 456ms 456ms ▁█▇▁▁
Warm (Ready in log) 443ms 444ms ▁█▇▁▁
Warm (First Request) 340ms 344ms ▁█▆▁▁
📦 Dev Server (Webpack) (Legacy)

📦 Dev Server (Webpack)

Metric Canary PR Change Trend
Cold (Listen) 456ms 456ms ▁▁█▁█
Cold (Ready in log) 445ms 448ms ▂▁█▁▆
Cold (First Request) 1.918s 1.901s ▃▂█▁▆
Warm (Listen) 457ms 457ms ▁▁█▁█
Warm (Ready in log) 447ms 450ms ▁▁█▁▆
Warm (First Request) 1.950s 1.931s ▂▂█▁▆

⚡ Production Builds

Metric Canary PR Change Trend
Fresh Build 3.846s 3.914s ▁██▁▁
Cached Build 3.912s 3.892s ▁█▇▁▁
📦 Production Builds (Webpack) (Legacy)

📦 Production Builds (Webpack)

Metric Canary PR Change Trend
Fresh Build 14.937s 15.007s ▁▁█▁▇
Cached Build 15.110s 15.069s ▂▁█▁▇
node_modules Size 485 MB 485 MB ▁▁███
📦 Bundle Sizes

Bundle Sizes

⚡ Turbopack

Client

Main Bundles
Canary PR Change
00iotnnw2_xfy.js gzip 70.8 kB N/A -
02fkg8wfh0iju.js gzip 9.19 kB N/A -
050zwt5xh_0tx.js gzip 10.4 kB N/A -
06rvbj82bhyo0.js gzip 13 kB N/A -
087fzjd-gvlzv.js gzip 450 B N/A -
0cz1d0mv5g_q7.js gzip 39.4 kB 39.4 kB
0ppxcl_z43mad.js gzip 8.52 kB N/A -
0z_g5xb06rni8.js gzip 155 B N/A -
19oha6-znmkcv.js gzip 8.55 kB N/A -
1elt1qium-r2m.css gzip 115 B 115 B
1r404v4-3v4_u.js gzip 168 B N/A -
1tfdnbt58l2-x.js gzip 159 B N/A -
1veummv7rqv69.js gzip 153 B N/A -
2_5rjb7lqxntf.js gzip 221 B 221 B
219prxwxgaalc.js gzip 7.61 kB N/A -
26elcgxnn9zjd.js gzip 8.52 kB N/A -
2900hudr6gvm0.js gzip 2.28 kB N/A -
2jo4n-moj718i.js gzip 157 B N/A -
2lv2js3kmdeho.js gzip 8.48 kB N/A -
2rehygrd36hqv.js gzip 8.58 kB N/A -
2rvyazlx9_6iq.js gzip 157 B N/A -
2srwswih0m9_h.js gzip 13.3 kB N/A -
2vzdej-0msygw.js gzip 155 B N/A -
2x28o5srzht32.js gzip 157 B N/A -
3-p9p9mheqhzx.js gzip 8.55 kB N/A -
31030bryqpolg.js gzip 8.53 kB N/A -
31dx5nmrzzuy7.js gzip 225 B N/A -
3925v09gtu-5k.js gzip 49 kB N/A -
39x4zj5mjb4d_.js gzip 9.77 kB N/A -
3fc08f71xefg1.js gzip 155 B N/A -
3jic6i9bseep6.js gzip 65.7 kB N/A -
3k-48b78ys_vy.js gzip 10.1 kB N/A -
3lnfio3cnq7f0.js gzip 156 B N/A -
3m7-5rfj0avoz.js gzip 12.9 kB N/A -
3u6fn2mw1di--.js gzip 155 B N/A -
3uqce_6sa526g.js gzip 8.47 kB N/A -
3yurjqk-sjs3y.js gzip 1.46 kB N/A -
40ybjx9c192n0.js gzip 13.8 kB N/A -
41-_01ln_2zkh.js gzip 155 B N/A -
421vzwdt9j1b_.js gzip 5.62 kB N/A -
4438caaqile4_.js gzip 161 B N/A -
turbopack-03..n4nb.js gzip 4.18 kB N/A -
turbopack-04..ikcf.js gzip 4.15 kB N/A -
turbopack-04..alx1.js gzip 4.18 kB N/A -
turbopack-0j..dga0.js gzip 4.18 kB N/A -
turbopack-0l.._n9x.js gzip 4.18 kB N/A -
turbopack-0o..wy29.js gzip 4.18 kB N/A -
turbopack-12..pglf.js gzip 4.17 kB N/A -
turbopack-1j..o_c0.js gzip 4.19 kB N/A -
turbopack-1z..q6p8.js gzip 4.17 kB N/A -
turbopack-2-..qsnb.js gzip 4.18 kB N/A -
turbopack-26..mq9v.js gzip 4.17 kB N/A -
turbopack-2g..ahbp.js gzip 4.18 kB N/A -
turbopack-2w..j_8n.js gzip 4.18 kB N/A -
turbopack-44..dyyi.js gzip 4.17 kB N/A -
03dgzoo-qf3sm.js gzip N/A 9.19 kB -
05tx5f25dlivn.js gzip N/A 8.53 kB -
076qdhguprj3g.js gzip N/A 157 B -
07x7y9orroch_.js gzip N/A 154 B -
0c7ez6p2qc57f.js gzip N/A 5.62 kB -
0duvj3qk5pvgn.js gzip N/A 13.8 kB -
0m-34rm9w_wpm.js gzip N/A 7.6 kB -
0qnwuk92m8i7o.js gzip N/A 10.4 kB -
0r4wrn6n0ue2m.js gzip N/A 8.55 kB -
0rp0fodtbt_6m.js gzip N/A 8.52 kB -
0sfck-km4dl1k.js gzip N/A 8.47 kB -
0x0xuhmxzwkp8.js gzip N/A 8.47 kB -
1-wdvgxnzicj7.js gzip N/A 1.46 kB -
11u6nxujb2eg4.js gzip N/A 450 B -
1jv-o1_s-zmua.js gzip N/A 49 kB -
1qr4jxytqrquk.js gzip N/A 167 B -
1tawq-_7w5wnt.js gzip N/A 156 B -
1vhsa1istl8vk.js gzip N/A 155 B -
1vtzm80ig7ons.js gzip N/A 154 B -
25m7mqgt86ixn.js gzip N/A 160 B -
2c_83vh6ra6-m.js gzip N/A 152 B -
2e4t24z72pt2f.js gzip N/A 70.8 kB -
2hgmo8vho4vf0.js gzip N/A 152 B -
2k9ax08cjl2id.js gzip N/A 12.9 kB -
2lms6k76q5-6m.js gzip N/A 13.3 kB -
2luekerf2irq-.js gzip N/A 65.7 kB -
2qx4twi9i3xus.js gzip N/A 2.28 kB -
2rpa9-yryz91a.js gzip N/A 155 B -
2srnqic6tvxxd.js gzip N/A 8.52 kB -
30l7m4nayp73a.js gzip N/A 8.55 kB -
38rr7d3kfutni.js gzip N/A 13 kB -
3cka21fecurt7.js gzip N/A 157 B -
3h_ecpiaatwgc.js gzip N/A 10.1 kB -
3huq6k2x99obf.js gzip N/A 154 B -
3ity0aahajapd.js gzip N/A 225 B -
3wrhpuc-j1aw9.js gzip N/A 9.77 kB -
42q80l5qx8xoe.js gzip N/A 155 B -
43mlw9dy_8f02.js gzip N/A 8.58 kB -
turbopack-0j..6ao-.js gzip N/A 4.17 kB -
turbopack-19..y-ui.js gzip N/A 4.16 kB -
turbopack-1g..8mcq.js gzip N/A 4.18 kB -
turbopack-1g..srop.js gzip N/A 4.17 kB -
turbopack-1l..ffik.js gzip N/A 4.17 kB -
turbopack-2e..h56q.js gzip N/A 4.17 kB -
turbopack-2i..x_ek.js gzip N/A 4.19 kB -
turbopack-35..wsrp.js gzip N/A 4.17 kB -
turbopack-37..qdma.js gzip N/A 4.18 kB -
turbopack-39..9fws.js gzip N/A 4.18 kB -
turbopack-3a..z3tb.js gzip N/A 4.17 kB -
turbopack-3c..fl01.js gzip N/A 4.17 kB -
turbopack-3g..off8.js gzip N/A 4.18 kB -
turbopack-3q..1pmk.js gzip N/A 4.17 kB -
Total 464 kB 464 kB ✅ -49 B

Server

Middleware
Canary PR Change
middleware-b..fest.js gzip 715 B 712 B
Total 715 B 712 B ✅ -3 B
Build Details
Build Manifests
Canary PR Change
_buildManifest.js gzip 436 B 430 B 🟢 6 B (-1%)
Total 436 B 430 B ✅ -6 B

📦 Webpack

Client

Main Bundles
Canary PR Change
5528-HASH.js gzip 5.54 kB N/A -
6280-HASH.js gzip 60.7 kB N/A -
6335.HASH.js gzip 169 B N/A -
912-HASH.js gzip 4.59 kB N/A -
e8aec2e4-HASH.js gzip 62.8 kB N/A -
framework-HASH.js gzip 59.7 kB 59.7 kB
main-app-HASH.js gzip 256 B 253 B 🟢 3 B (-1%)
main-HASH.js gzip 39.3 kB 39.2 kB
webpack-HASH.js gzip 1.68 kB 1.68 kB
262-HASH.js gzip N/A 4.59 kB -
2889.HASH.js gzip N/A 169 B -
5602-HASH.js gzip N/A 5.55 kB -
6948ada0-HASH.js gzip N/A 62.8 kB -
9544-HASH.js gzip N/A 61.4 kB -
Total 235 kB 235 kB ⚠️ +647 B
Polyfills
Canary PR Change
polyfills-HASH.js gzip 39.4 kB 39.4 kB
Total 39.4 kB 39.4 kB
Pages
Canary PR Change
_app-HASH.js gzip 194 B 194 B
_error-HASH.js gzip 183 B 180 B 🟢 3 B (-2%)
css-HASH.js gzip 331 B 330 B
dynamic-HASH.js gzip 1.81 kB 1.81 kB
edge-ssr-HASH.js gzip 256 B 256 B
head-HASH.js gzip 351 B 352 B
hooks-HASH.js gzip 384 B 383 B
image-HASH.js gzip 580 B 581 B
index-HASH.js gzip 260 B 260 B
link-HASH.js gzip 2.51 kB 2.51 kB
routerDirect..HASH.js gzip 320 B 319 B
script-HASH.js gzip 386 B 386 B
withRouter-HASH.js gzip 315 B 315 B
1afbb74e6ecf..834.css gzip 106 B 106 B
Total 7.98 kB 7.98 kB ✅ -1 B

Server

Edge SSR
Canary PR Change
edge-ssr.js gzip 125 kB 125 kB
page.js gzip 270 kB 270 kB
Total 395 kB 396 kB ⚠️ +172 B
Middleware
Canary PR Change
middleware-b..fest.js gzip 618 B 618 B
middleware-r..fest.js gzip 156 B 155 B
middleware.js gzip 43.6 kB 43.9 kB
edge-runtime..pack.js gzip 842 B 842 B
Total 45.3 kB 45.5 kB ⚠️ +217 B
Build Details
Build Manifests
Canary PR Change
_buildManifest.js gzip 715 B 718 B
Total 715 B 718 B ⚠️ +3 B
Build Cache
Canary PR Change
0.pack gzip 4.35 MB 4.34 MB
index.pack gzip 110 kB 110 kB
index.pack.old gzip 110 kB 110 kB
Total 4.57 MB 4.56 MB ✅ -4.77 kB

🔄 Shared (bundler-independent)

Runtimes
Canary PR Change
app-page-exp...dev.js gzip 336 kB 336 kB
app-page-exp..prod.js gzip 186 kB 186 kB
app-page-tur...dev.js gzip 335 kB 335 kB
app-page-tur..prod.js gzip 185 kB 185 kB
app-page-tur...dev.js gzip 332 kB 332 kB
app-page-tur..prod.js gzip 183 kB 183 kB
app-page.run...dev.js gzip 332 kB 332 kB
app-page.run..prod.js gzip 184 kB 184 kB
app-route-ex...dev.js gzip 76.3 kB 76.3 kB
app-route-ex..prod.js gzip 51.9 kB 51.9 kB
app-route-tu...dev.js gzip 76.3 kB 76.3 kB
app-route-tu..prod.js gzip 51.9 kB 51.9 kB
app-route-tu...dev.js gzip 75.9 kB 75.9 kB
app-route-tu..prod.js gzip 51.7 kB 51.7 kB
app-route.ru...dev.js gzip 75.9 kB 75.9 kB
app-route.ru..prod.js gzip 51.7 kB 51.7 kB
dist_client_...dev.js gzip 324 B 324 B
dist_client_...dev.js gzip 326 B 326 B
dist_client_...dev.js gzip 318 B 318 B
dist_client_...dev.js gzip 317 B 317 B
pages-api-tu...dev.js gzip 43.5 kB 43.5 kB
pages-api-tu..prod.js gzip 33.1 kB 33.1 kB
pages-api.ru...dev.js gzip 43.5 kB 43.5 kB
pages-api.ru..prod.js gzip 33.1 kB 33.1 kB
pages-turbo....dev.js gzip 52.9 kB 52.9 kB
pages-turbo...prod.js gzip 38.7 kB 38.7 kB
pages.runtim...dev.js gzip 52.9 kB 52.9 kB
pages.runtim..prod.js gzip 38.7 kB 38.7 kB
server.runti..prod.js gzip 62.5 kB 62.5 kB
Total 2.99 MB 2.99 MB ✅ -4 B
📎 Tarball URL
https://vercel-packages.vercel.app/next/commits/76915a45adcebad7142a2f1266be0e98b696a731/next

@sokra sokra changed the title Fix DashMap read-write self-deadlock in task_cache causing dev server hangs Fix DashMap read-write self-deadlock in task_cache causing hangs Apr 1, 2026
@sokra sokra requested a review from lukesandberg April 1, 2026 19:22
@sokra sokra marked this pull request as ready for review April 1, 2026 19:22
Copy link
Copy Markdown
Contributor

@lukesandberg lukesandberg left a comment

Choose a reason for hiding this comment

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

😭

@lukesandberg lukesandberg merged commit 9e15093 into canary Apr 1, 2026
286 of 292 checks passed
@lukesandberg lukesandberg deleted the sokra/hanging branch April 1, 2026 20:25
@github-actions github-actions Bot locked as resolved and limited conversation to collaborators Apr 16, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

created-by: Turbopack team PRs by the Turbopack team. locked Turbopack Related to Turbopack with Next.js.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants