Skip to content

Improve unsafe code safety documentation in turbo-tasks-backend#90755

Merged
sokra merged 1 commit intocanaryfrom
sokra/backend-unsafe
Mar 9, 2026
Merged

Improve unsafe code safety documentation in turbo-tasks-backend#90755
sokra merged 1 commit intocanaryfrom
sokra/backend-unsafe

Conversation

@sokra
Copy link
Member

@sokra sokra commented Mar 2, 2026

What?

Improves safety documentation for all unsafe code in the turbo-tasks-backend crate (6 files, comment-only changes).

Why?

A comprehensive audit of all 14 files containing unsafe code in turbo-tasks-backend found no soundness bugs, but identified several safety comments that were incorrect, misleading, or missing entirely. Correct safety documentation is critical for maintaining unsafe code over time — reviewers and future contributors rely on these comments to understand invariants.

How?

Fixes applied (comment-only, no behavior changes):

  1. backend/operation/mod.rs — Fixed transmute safety comment that referenced wrong lifetime names ('a/'l'e/'tx)
  2. database/read_transaction_cache.rs — Fixed reference to wrong struct name (LmdbBackingStorageReadTransactionCache); added struct-level doc explaining the field-ordering invariant required for drop safety
  3. database/startup_cache.rs — Added struct-level doc explaining the self-referential pattern and field-ordering invariant; improved inline safety comment
  4. utils/dash_map_multi.rs — Added safety justification for unsafe impl Send/Sync on RefMut explaining bucket pointer exclusivity
  5. backing_storage.rs — Added // Safety: comments to 6 unsafe blocks in the Either impl that were missing them
  6. kv_backing_storage.rs — Made the flush() safety comment more specific about what "finished processing" means

Audit summary (14 files reviewed):

Risk Category Files Verdict
HIGH Lifetime transmutation 3 Sound ✓ — comments fixed
MEDIUM DashMap raw bucket access 3 Sound ✓ — Send/Sync docs added
LOW Unsafe trait method contracts 8 Sound ✓ — missing comments added
NEGLIGIBLE Test code 1 No issues

@nextjs-bot nextjs-bot added created-by: Turbopack team PRs by the Turbopack team. Turbopack Related to Turbopack with Next.js. labels Mar 2, 2026
@codspeed-hq
Copy link

codspeed-hq bot commented Mar 2, 2026

Merging this PR will not alter performance

✅ 17 untouched benchmarks
⏩ 3 skipped benchmarks1


Comparing sokra/backend-unsafe (875b54b) with canary (1ead596)

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
Collaborator

nextjs-bot commented Mar 2, 2026

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) 438ms 438ms ▁▁▁▁▄
Cold (First Request) 1.290s 1.234s ▂▂▁▂▄
Warm (Listen) 457ms 456ms ▁▁▁▁▅
Warm (Ready in log) 442ms 443ms ▁▁▁▁▄
Warm (First Request) 348ms 351ms ▁▁▁▁▃

⚡ Production Builds

Metric Canary PR Change Trend
Fresh Build 3.771s 3.763s ▁▁▁▁▄
Cached Build 3.841s 3.792s ▁▁▁▁▄
📦 Production Builds (Webpack) (Legacy)

📦 Production Builds (Webpack)

Metric Canary PR Change Trend
node_modules Size 477 MB 477 MB █████
📦 Bundle Sizes

Bundle Sizes

⚡ Turbopack

Client

Main Bundles: **402 kB** → **402 kB** ✅ -11 B

80 files with content-based hashes (individual files not comparable between builds)

Server

Middleware
Canary PR Change
middleware-b..fest.js gzip 764 B 766 B
Total 764 B 766 B ⚠️ +2 B
Build Details
Build Manifests
Canary PR Change
_buildManifest.js gzip 451 B 453 B
Total 451 B 453 B ⚠️ +2 B

🔄 Shared (bundler-independent)

Runtimes
Canary PR Change
app-page-exp...dev.js gzip 323 kB 323 kB
app-page-exp..prod.js gzip 171 kB 171 kB
app-page-tur...dev.js gzip 322 kB 322 kB
app-page-tur..prod.js gzip 171 kB 171 kB
app-page-tur...dev.js gzip 319 kB 319 kB
app-page-tur..prod.js gzip 169 kB 169 kB
app-page.run...dev.js gzip 319 kB 319 kB
app-page.run..prod.js gzip 169 kB 169 kB
app-route-ex...dev.js gzip 70.9 kB 70.9 kB
app-route-ex..prod.js gzip 49.3 kB 49.3 kB
app-route-tu...dev.js gzip 70.9 kB 70.9 kB
app-route-tu..prod.js gzip 49.3 kB 49.3 kB
app-route-tu...dev.js gzip 70.5 kB 70.5 kB
app-route-tu..prod.js gzip 49.1 kB 49.1 kB
app-route.ru...dev.js gzip 70.5 kB 70.5 kB
app-route.ru..prod.js gzip 49 kB 49 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.3 kB 43.3 kB
pages-api-tu..prod.js gzip 32.9 kB 32.9 kB
pages-api.ru...dev.js gzip 43.2 kB 43.2 kB
pages-api.ru..prod.js gzip 32.9 kB 32.9 kB
pages-turbo....dev.js gzip 52.6 kB 52.6 kB
pages-turbo...prod.js gzip 38.5 kB 38.5 kB
pages.runtim...dev.js gzip 52.6 kB 52.6 kB
pages.runtim..prod.js gzip 38.5 kB 38.5 kB
server.runti..prod.js gzip 62 kB 62 kB
Total 2.84 MB 2.84 MB ✅ -1 B
📎 Tarball URL
https://vercel-packages.vercel.app/next/commits/875b54bcf2ee860aae067144e4100ee8d17d31bf/next

@sokra sokra requested a review from lukesandberg March 2, 2026 09:25
@sokra sokra marked this pull request as ready for review March 2, 2026 09:25
@sokra sokra requested a review from mischnic March 3, 2026 11:33
@sokra sokra force-pushed the sokra/backend-unsafe branch from fd76078 to 366c8f5 Compare March 6, 2026 08:25
sokra added a commit that referenced this pull request Mar 9, 2026
…91087)

## Summary

`getFailedJobs()` in `scripts/pr-status.js` used jq's `select(.conclusion == "failure")` filter during GitHub API pagination. When a page had 100 jobs but none were failures, jq returned empty output, and the `if (!output.trim()) break` check terminated the pagination loop early — never fetching page 2+ where actual failures existed.

For example, on PR #90755 (129 total jobs), all 100 jobs on page 1 were success/skipped, while the 2 failures (`test unit windows (22) / build` and `thank you, next`) were on page 2 (jobs 101-129). The script reported "Found 0 failed jobs" when there were actually 2.

## Fix

`getFailedJobs()` now delegates to `getAllJobs()` (which fetches all jobs without jq filtering, so pagination works correctly) and filters for failures in JavaScript afterward. This is simpler and avoids the class of bug where jq pre-filtering interacts badly with pagination.

## Test Plan

- Ran `node scripts/pr-status.js 90755` before fix: reported 0 failed jobs
- Ran `node scripts/pr-status.js 90755` after fix: correctly reported 2 failed jobs (`test unit windows (22) / build` and `thank you, next`)
Fix incorrect and missing safety comments across 6 files:

- operation/mod.rs: Fix transmute safety comment using wrong lifetime
  names ('a/'l instead of 'e/'tx)
- read_transaction_cache.rs: Fix reference to wrong struct name
  (LmdbBackingStorage → ReadTransactionCache), add struct-level safety
  invariant documenting field-ordering requirement for drop order
- startup_cache.rs: Add struct-level safety invariant documenting
  self-referential pattern and field-ordering requirement
- dash_map_multi.rs: Add safety justification for unsafe impl
  Send/Sync on RefMut explaining bucket pointer safety
- backing_storage.rs: Add safety comments to 6 unsafe blocks in
  Either impl forwarding calls
- kv_backing_storage.rs: Improve flush safety comment specificity
@sokra sokra force-pushed the sokra/backend-unsafe branch from 366c8f5 to 875b54b Compare March 9, 2026 10:09
@sokra sokra merged commit 6a73374 into canary Mar 9, 2026
159 of 162 checks passed
@sokra sokra deleted the sokra/backend-unsafe branch March 9, 2026 14:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants