Skip to content

Commit dfc5331

Browse files
authored
[Turbopack] add more tracing to dirty flagging (vercel#71482)
### What? trace make task dirty add tracing why tasks are marked as dirty report reason when a page is reloaded
1 parent 24da437 commit dfc5331

File tree

11 files changed

+187
-44
lines changed

11 files changed

+187
-44
lines changed

packages/next/src/server/dev/hot-reloader-turbopack.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -484,6 +484,7 @@ export async function createHotReloaderTurbopack(
484484
// reload, only this client is out of date.
485485
const reloadAction: ReloadPageAction = {
486486
action: HMR_ACTIONS_SENT_TO_BROWSER.RELOAD_PAGE,
487+
data: `error in HMR event subscription for ${id}: ${e}`,
487488
}
488489
sendToClient(client, reloadAction)
489490
client.close()

packages/next/src/server/dev/hot-reloader-types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ interface RemovedPageAction {
7676

7777
export interface ReloadPageAction {
7878
action: HMR_ACTIONS_SENT_TO_BROWSER.RELOAD_PAGE
79+
data: string
7980
}
8081

8182
interface ServerComponentChangesAction {

packages/next/src/server/dev/hot-reloader-webpack.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -408,7 +408,10 @@ export default class HotReloaderWebpack implements NextJsHotReloaderInterface {
408408
public clearHmrServerError(): void {
409409
if (this.hmrServerError) {
410410
this.setHmrServerError(null)
411-
this.send({ action: HMR_ACTIONS_SENT_TO_BROWSER.RELOAD_PAGE })
411+
this.send({
412+
action: HMR_ACTIONS_SENT_TO_BROWSER.RELOAD_PAGE,
413+
data: 'clear hmr server error',
414+
})
412415
}
413416
}
414417

@@ -1359,7 +1362,10 @@ export default class HotReloaderWebpack implements NextJsHotReloaderInterface {
13591362
this.serverPrevDocumentHash = documentChunk.hash || null
13601363

13611364
// Notify reload to reload the page, as _document.js was changed (different hash)
1362-
this.send({ action: HMR_ACTIONS_SENT_TO_BROWSER.RELOAD_PAGE })
1365+
this.send({
1366+
action: HMR_ACTIONS_SENT_TO_BROWSER.RELOAD_PAGE,
1367+
data: '_document has changed',
1368+
})
13631369
}
13641370
)
13651371

packages/next/src/server/dev/turbopack-utils.ts

Lines changed: 34 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -479,8 +479,11 @@ export async function handleRouteType({
479479
pages: [page],
480480
}
481481
},
482-
() => {
483-
return { action: HMR_ACTIONS_SENT_TO_BROWSER.RELOAD_PAGE }
482+
(e) => {
483+
return {
484+
action: HMR_ACTIONS_SENT_TO_BROWSER.RELOAD_PAGE,
485+
data: `error in ${page} data subscription: ${e}`,
486+
}
484487
}
485488
)
486489
hooks?.subscribeToChanges(
@@ -492,8 +495,11 @@ export async function handleRouteType({
492495
event: HMR_ACTIONS_SENT_TO_BROWSER.CLIENT_CHANGES,
493496
}
494497
},
495-
() => {
496-
return { action: HMR_ACTIONS_SENT_TO_BROWSER.RELOAD_PAGE }
498+
(e) => {
499+
return {
500+
action: HMR_ACTIONS_SENT_TO_BROWSER.RELOAD_PAGE,
501+
data: `error in ${page} html subscription: ${e}`,
502+
}
497503
}
498504
)
499505
if (entrypoints.global.document) {
@@ -502,10 +508,16 @@ export async function handleRouteType({
502508
false,
503509
entrypoints.global.document,
504510
() => {
505-
return { action: HMR_ACTIONS_SENT_TO_BROWSER.RELOAD_PAGE }
511+
return {
512+
action: HMR_ACTIONS_SENT_TO_BROWSER.RELOAD_PAGE,
513+
data: '_document has changed (page route)',
514+
}
506515
},
507-
() => {
508-
return { action: HMR_ACTIONS_SENT_TO_BROWSER.RELOAD_PAGE }
516+
(e) => {
517+
return {
518+
action: HMR_ACTIONS_SENT_TO_BROWSER.RELOAD_PAGE,
519+
data: `error in _document subscription (page route): ${e}`,
520+
}
509521
}
510522
)
511523
}
@@ -1075,6 +1087,7 @@ export async function handlePagesErrorRoute({
10751087
() => {
10761088
return {
10771089
action: HMR_ACTIONS_SENT_TO_BROWSER.RELOAD_PAGE,
1090+
data: '_app has changed (error route)',
10781091
}
10791092
}
10801093
)
@@ -1096,10 +1109,16 @@ export async function handlePagesErrorRoute({
10961109
false,
10971110
entrypoints.global.document,
10981111
() => {
1099-
return { action: HMR_ACTIONS_SENT_TO_BROWSER.RELOAD_PAGE }
1112+
return {
1113+
action: HMR_ACTIONS_SENT_TO_BROWSER.RELOAD_PAGE,
1114+
data: '_document has changed (error route)',
1115+
}
11001116
},
1101-
() => {
1102-
return { action: HMR_ACTIONS_SENT_TO_BROWSER.RELOAD_PAGE }
1117+
(e) => {
1118+
return {
1119+
action: HMR_ACTIONS_SENT_TO_BROWSER.RELOAD_PAGE,
1120+
data: `error in _document subscription (error route): ${e}`,
1121+
}
11031122
}
11041123
)
11051124
}
@@ -1122,8 +1141,11 @@ export async function handlePagesErrorRoute({
11221141
// https://github.com/vercel/next.js/blob/08d7a7e5189a835f5dcb82af026174e587575c0e/packages/next/src/client/page-bootstrap.ts#L69-L71
11231142
return { event: HMR_ACTIONS_SENT_TO_BROWSER.CLIENT_CHANGES }
11241143
},
1125-
() => {
1126-
return { action: HMR_ACTIONS_SENT_TO_BROWSER.RELOAD_PAGE }
1144+
(e) => {
1145+
return {
1146+
action: HMR_ACTIONS_SENT_TO_BROWSER.RELOAD_PAGE,
1147+
data: `error in _error subscription: ${e}`,
1148+
}
11271149
}
11281150
)
11291151
}

turbopack/crates/turbo-tasks-backend/src/backend/mod.rs

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ use crate::{
4141
operation::{
4242
get_aggregation_number, is_root_node, AggregatedDataUpdate, AggregationUpdateJob,
4343
AggregationUpdateQueue, CleanupOldEdgesOperation, ConnectChildOperation,
44-
ExecuteContext, ExecuteContextImpl, Operation, OutdatedEdge, TaskGuard,
44+
ExecuteContext, ExecuteContextImpl, Operation, OutdatedEdge, TaskDirtyCause, TaskGuard,
4545
},
4646
storage::{get, get_many, get_mut, iter_many, remove, Storage},
4747
},
@@ -779,7 +779,11 @@ impl<B: BackingStorage> TurboTasksBackendInner<B> {
779779
task_id: TaskId,
780780
turbo_tasks: &dyn TurboTasksBackendApi<TurboTasksBackend<B>>,
781781
) {
782-
operation::InvalidateOperation::run(smallvec![task_id], self.execute_context(turbo_tasks));
782+
operation::InvalidateOperation::run(
783+
smallvec![task_id],
784+
TaskDirtyCause::Unknown,
785+
self.execute_context(turbo_tasks),
786+
);
783787
}
784788

785789
fn invalidate_tasks(
@@ -789,6 +793,7 @@ impl<B: BackingStorage> TurboTasksBackendInner<B> {
789793
) {
790794
operation::InvalidateOperation::run(
791795
tasks.iter().copied().collect(),
796+
TaskDirtyCause::Unknown,
792797
self.execute_context(turbo_tasks),
793798
);
794799
}
@@ -800,6 +805,7 @@ impl<B: BackingStorage> TurboTasksBackendInner<B> {
800805
) {
801806
operation::InvalidateOperation::run(
802807
tasks.iter().copied().collect(),
808+
TaskDirtyCause::Unknown,
803809
self.execute_context(turbo_tasks),
804810
);
805811
}
@@ -817,9 +823,11 @@ impl<B: BackingStorage> TurboTasksBackendInner<B> {
817823
task.invalidate_serialization();
818824
}
819825

820-
fn get_task_description(&self, task: TaskId) -> std::string::String {
821-
let task_type = self.lookup_task_type(task).expect("Task not found");
822-
task_type.to_string()
826+
fn get_task_description(&self, task_id: TaskId) -> std::string::String {
827+
self.lookup_task_type(task_id).map_or_else(
828+
|| format!("{task_id:?} transient"),
829+
|task_type| task_type.to_string(),
830+
)
823831
}
824832

825833
fn try_get_function_id(&self, task_id: TaskId) -> Option<FunctionId> {
@@ -1163,7 +1171,7 @@ impl<B: BackingStorage> TurboTasksBackendInner<B> {
11631171
.get(&cell.type_id)
11641172
.map_or(false, |range| range.contains(&cell.index)) =>
11651173
{
1166-
Some(OutdatedEdge::RemovedCellDependent(task))
1174+
Some(OutdatedEdge::RemovedCellDependent(task, cell.type_id))
11671175
}
11681176
_ => None,
11691177
},
@@ -1194,7 +1202,7 @@ impl<B: BackingStorage> TurboTasksBackendInner<B> {
11941202
.get(&cell.type_id)
11951203
.map_or(false, |range| range.contains(&cell.index)) =>
11961204
{
1197-
Some(OutdatedEdge::RemovedCellDependent(task))
1205+
Some(OutdatedEdge::RemovedCellDependent(task, cell.type_id))
11981206
}
11991207
_ => None,
12001208
})

turbopack/crates/turbo-tasks-backend/src/backend/operation/aggregation_update.rs

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,14 @@ use indexmap::map::Entry;
88
use rustc_hash::{FxHashMap, FxHashSet};
99
use serde::{Deserialize, Serialize};
1010
use smallvec::SmallVec;
11-
use turbo_tasks::{FxIndexMap, FxIndexSet, SessionId, TaskId};
11+
use turbo_tasks::{FxIndexMap, FxIndexSet, SessionId, TaskId, TraitTypeId};
1212

1313
use crate::{
1414
backend::{
15-
operation::{invalidate::make_task_dirty, ExecuteContext, Operation, TaskGuard},
15+
operation::{
16+
invalidate::{make_task_dirty, TaskDirtyCause},
17+
ExecuteContext, Operation, TaskGuard,
18+
},
1619
storage::{get, get_many, iter_many, remove, update, update_count},
1720
TaskDataCategory,
1821
},
@@ -101,8 +104,9 @@ pub enum AggregationUpdateJob {
101104
upper_ids: Vec<TaskId>,
102105
update: AggregatedDataUpdate,
103106
},
104-
Invalidate {
107+
InvalidateDueToCollectiblesChange {
105108
task_ids: SmallVec<[TaskId; 4]>,
109+
collectible_type: TraitTypeId,
106110
},
107111
BalanceEdge {
108112
upper_id: TaskId,
@@ -284,8 +288,9 @@ impl AggregatedDataUpdate {
284288
}
285289
);
286290
if !dependent.is_empty() {
287-
queue.push(AggregationUpdateJob::Invalidate {
291+
queue.push(AggregationUpdateJob::InvalidateDueToCollectiblesChange {
288292
task_ids: dependent,
293+
collectible_type: ty,
289294
})
290295
}
291296
}
@@ -556,9 +561,17 @@ impl AggregationUpdateQueue {
556561
AggregationUpdateJob::AggregatedDataUpdate { upper_ids, update } => {
557562
self.aggregated_data_update(upper_ids, ctx, update);
558563
}
559-
AggregationUpdateJob::Invalidate { task_ids } => {
564+
AggregationUpdateJob::InvalidateDueToCollectiblesChange {
565+
task_ids,
566+
collectible_type,
567+
} => {
560568
for task_id in task_ids {
561-
make_task_dirty(task_id, self, ctx);
569+
make_task_dirty(
570+
task_id,
571+
TaskDirtyCause::CollectiblesChange { collectible_type },
572+
self,
573+
ctx,
574+
);
562575
}
563576
}
564577
}

turbopack/crates/turbo-tasks-backend/src/backend/operation/cleanup_old_edges.rs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use std::mem::take;
22

33
use serde::{Deserialize, Serialize};
4-
use turbo_tasks::TaskId;
4+
use turbo_tasks::{TaskId, ValueTypeId};
55

66
use crate::{
77
backend::{
@@ -10,7 +10,7 @@ use crate::{
1010
get_aggregation_number, get_uppers, is_aggregating_node, AggregationUpdateJob,
1111
AggregationUpdateQueue,
1212
},
13-
invalidate::make_task_dirty,
13+
invalidate::{make_task_dirty, TaskDirtyCause},
1414
AggregatedDataUpdate, ExecuteContext, Operation, TaskGuard,
1515
},
1616
storage::{update, update_count},
@@ -41,7 +41,7 @@ pub enum OutdatedEdge {
4141
CellDependency(CellRef),
4242
OutputDependency(TaskId),
4343
CollectiblesDependency(CollectiblesRef),
44-
RemovedCellDependent(TaskId),
44+
RemovedCellDependent(TaskId, ValueTypeId),
4545
}
4646

4747
impl CleanupOldEdgesOperation {
@@ -177,8 +177,13 @@ impl Operation for CleanupOldEdgesOperation {
177177
});
178178
}
179179
}
180-
OutdatedEdge::RemovedCellDependent(task_id) => {
181-
make_task_dirty(task_id, queue, ctx);
180+
OutdatedEdge::RemovedCellDependent(task_id, value_type) => {
181+
make_task_dirty(
182+
task_id,
183+
TaskDirtyCause::CellRemoved { value_type },
184+
queue,
185+
ctx,
186+
);
182187
}
183188
}
184189
}

0 commit comments

Comments
 (0)