I added a test that asserts malformed profiling operations do not throw and that errors/warnings are logged#452
I added a test that asserts malformed profiling operations do not throw and that errors/warnings are logged#452everettbu wants to merge 4 commits into
Conversation
…ow and that errors/warnings are logged
Greptile OverviewGreptile SummaryThis PR converts error-throwing behavior to graceful error handling in Critical Issues:
What Changed:
These bugs must be fixed before the error handling can work correctly. The previous thread comments correctly identified these exact issues. Confidence Score: 0/5
Important Files Changed
|
| if (nodes.has(id)) { | ||
| throw new Error( | ||
| `Commit tree already contains fiber "${id}". This is a bug in React DevTools.`, | ||
| // Duplicate add detected; log and skip instead of throwing to avoid crashing the UI. | ||
| console.error( | ||
| `Commit tree already contains fiber "${id}". Skipping duplicate add.`, | ||
| ); | ||
| break; |
There was a problem hiding this comment.
using break here without advancing i past all ADD operation parameters will cause subsequent parameters to be misinterpreted as operation codes. For non-root nodes, there are still 5+ parameters to skip (parentID, ownerID, displayNameStringID, keyStringID, name prop). This will corrupt the operation stream parsing.
need to advance i to skip remaining parameters before breaking, similar to how the normal ADD case increments i throughout lines 234-248
Prompt To Fix With AI
This is a comment left during a code review.
Path: packages/react-devtools-shared/src/devtools/views/Profiler/CommitTreeBuilder.js
Line: 202:207
Comment:
using `break` here without advancing `i` past all ADD operation parameters will cause subsequent parameters to be misinterpreted as operation codes. For non-root nodes, there are still 5+ parameters to skip (parentID, ownerID, displayNameStringID, keyStringID, name prop). This will corrupt the operation stream parsing.
need to advance `i` to skip remaining parameters before breaking, similar to how the normal ADD case increments `i` throughout lines 234-248
How can I resolve this? If you propose a fix, please make it concise.| console.error( | ||
| `Commit tree already contains fiber "${id}". Skipping duplicate add.`, | ||
| ); | ||
| break; |
There was a problem hiding this comment.
The break statement here exits without consuming all ADD operation parameters, corrupting the operation stream. When a duplicate is detected, i has only advanced by 3 positions (operation + id + type). For non-root nodes, 5 additional parameters remain (parentID, ownerID, displayNameStringID, keyStringID, name prop). These will be misinterpreted as operation codes on the next loop iteration.
Need to advance i past all parameters before breaking:
| break; | |
| // Duplicate add detected; log and skip instead of throwing to avoid crashing the UI. | |
| console.error( | |
| `Commit tree already contains fiber "${id}". Skipping duplicate add.`, | |
| ); | |
| // Skip remaining ADD parameters to avoid corrupting the operation stream | |
| if (type === ElementTypeRoot) { | |
| i += 4; // isStrictModeCompliant, Profiling flag, supportsStrictMode, hasOwnerMetadata | |
| } else { | |
| i += 5; // parentID, ownerID, displayNameStringID, keyStringID, name prop | |
| } | |
| break; |
Prompt To Fix With AI
This is a comment left during a code review.
Path: packages/react-devtools-shared/src/devtools/views/Profiler/CommitTreeBuilder.js
Line: 207:207
Comment:
The `break` statement here exits without consuming all ADD operation parameters, corrupting the operation stream. When a duplicate is detected, `i` has only advanced by 3 positions (operation + id + type). For non-root nodes, 5 additional parameters remain (parentID, ownerID, displayNameStringID, keyStringID, name prop). These will be misinterpreted as operation codes on the next loop iteration.
Need to advance `i` past all parameters before breaking:
```suggestion
// Duplicate add detected; log and skip instead of throwing to avoid crashing the UI.
console.error(
`Commit tree already contains fiber "${id}". Skipping duplicate add.`,
);
// Skip remaining ADD parameters to avoid corrupting the operation stream
if (type === ElementTypeRoot) {
i += 4; // isStrictModeCompliant, Profiling flag, supportsStrictMode, hasOwnerMetadata
} else {
i += 5; // parentID, ownerID, displayNameStringID, keyStringID, name prop
}
break;
```
How can I resolve this? If you propose a fix, please make it concise.| case TREE_OPERATION_REMOVE_ROOT: { | ||
| throw Error('Operation REMOVE_ROOT is not supported while profiling.'); | ||
| // Removing roots is not supported while profiling; log and ignore. | ||
| console.warn('Operation REMOVE_ROOT is not supported while profiling. Ignoring.'); | ||
| break; |
There was a problem hiding this comment.
REMOVE_ROOT operation has 1 parameter (root ID) that needs to be consumed. Breaking without advancing i will cause the root ID to be misinterpreted as an operation code.
| case TREE_OPERATION_REMOVE_ROOT: { | |
| throw Error('Operation REMOVE_ROOT is not supported while profiling.'); | |
| // Removing roots is not supported while profiling; log and ignore. | |
| console.warn('Operation REMOVE_ROOT is not supported while profiling. Ignoring.'); | |
| break; | |
| case TREE_OPERATION_REMOVE_ROOT: { | |
| i++; // Skip root ID parameter | |
| // Removing roots is not supported while profiling; log and ignore. | |
| console.warn('Operation REMOVE_ROOT is not supported while profiling. Ignoring.'); | |
| break; | |
| } |
Prompt To Fix With AI
This is a comment left during a code review.
Path: packages/react-devtools-shared/src/devtools/views/Profiler/CommitTreeBuilder.js
Line: 317:320
Comment:
REMOVE_ROOT operation has 1 parameter (root ID) that needs to be consumed. Breaking without advancing `i` will cause the root ID to be misinterpreted as an operation code.
```suggestion
case TREE_OPERATION_REMOVE_ROOT: {
i++; // Skip root ID parameter
// Removing roots is not supported while profiling; log and ignore.
console.warn('Operation REMOVE_ROOT is not supported while profiling. Ignoring.');
break;
}
```
How can I resolve this? If you propose a fix, please make it concise.| const ops0 = [0, rootID, 0]; | ||
| const ops1 = [ | ||
| 0, rootID, 0, | ||
| 1, 2, ElementTypeFunction, rootID, 0, 0, 0, | ||
| 2, 1, 999, | ||
| 6, | ||
| 99, | ||
| ]; |
There was a problem hiding this comment.
The test doesn't actually validate the malformed operations it intends to test. Operation 0 in both ops0 and at the start of ops1 hits the default case (line 499-503 in CommitTreeBuilder.js), which sets i = operations.length and stops processing immediately. The subsequent malformed operations (duplicate ADD at position 1, REMOVE of missing node at position 2, REMOVE_ROOT at position 6, and unsupported op 99) are never executed.
To properly test these scenarios, remove the leading 0 operations or use valid initial operations like TREE_OPERATION_UPDATE_TREE_BASE_DURATION.
Prompt To Fix With AI
This is a comment left during a code review.
Path: packages/react-devtools-shared/src/__tests__/profilingCommitTreeBuilder-test.js
Line: 369:376
Comment:
The test doesn't actually validate the malformed operations it intends to test. Operation `0` in both `ops0` and at the start of `ops1` hits the default case (line 499-503 in CommitTreeBuilder.js), which sets `i = operations.length` and stops processing immediately. The subsequent malformed operations (duplicate ADD at position 1, REMOVE of missing node at position 2, REMOVE_ROOT at position 6, and unsupported op 99) are never executed.
To properly test these scenarios, remove the leading `0` operations or use valid initial operations like `TREE_OPERATION_UPDATE_TREE_BASE_DURATION`.
How can I resolve this? If you propose a fix, please make it concise.| if (nodes.has(id)) { | ||
| throw new Error( | ||
| `Commit tree already contains fiber "${id}". This is a bug in React DevTools.`, | ||
| // Duplicate add detected; log and skip instead of throwing to avoid crashing the UI. | ||
| console.error( | ||
| `Commit tree already contains fiber "${id}". Skipping duplicate add.`, | ||
| ); | ||
| break; |
There was a problem hiding this comment.
using break without consuming all ADD parameters corrupts the operation stream. At this point, i has only advanced by 3 (operation + id + type). For non-root nodes, 5 more parameters remain: parentID, ownerID, displayNameStringID, keyStringID, and name prop (lines 234-248). These unread parameters will be misinterpreted as operation codes on the next iteration.
need to advance i past all remaining parameters before breaking:
| if (nodes.has(id)) { | |
| throw new Error( | |
| `Commit tree already contains fiber "${id}". This is a bug in React DevTools.`, | |
| // Duplicate add detected; log and skip instead of throwing to avoid crashing the UI. | |
| console.error( | |
| `Commit tree already contains fiber "${id}". Skipping duplicate add.`, | |
| ); | |
| break; | |
| if (nodes.has(id)) { | |
| // Duplicate add detected; log and skip instead of throwing to avoid crashing the UI. | |
| console.error( | |
| `Commit tree already contains fiber "${id}". Skipping duplicate add.`, | |
| ); | |
| // Skip remaining parameters for non-root nodes | |
| if (type !== ElementTypeRoot) { | |
| i += 5; // parentID, ownerID, displayNameStringID, keyStringID, name prop | |
| } else { | |
| i += 4; // isStrictModeCompliant, Profiling flag, supportsStrictMode flag, hasOwnerMetadata flag | |
| } | |
| break; | |
| } |
Prompt To Fix With AI
This is a comment left during a code review.
Path: packages/react-devtools-shared/src/devtools/views/Profiler/CommitTreeBuilder.js
Line: 202:207
Comment:
using `break` without consuming all ADD parameters corrupts the operation stream. At this point, `i` has only advanced by 3 (operation + id + type). For non-root nodes, 5 more parameters remain: parentID, ownerID, displayNameStringID, keyStringID, and name prop (lines 234-248). These unread parameters will be misinterpreted as operation codes on the next iteration.
need to advance `i` past all remaining parameters before breaking:
```suggestion
if (nodes.has(id)) {
// Duplicate add detected; log and skip instead of throwing to avoid crashing the UI.
console.error(
`Commit tree already contains fiber "${id}". Skipping duplicate add.`,
);
// Skip remaining parameters for non-root nodes
if (type !== ElementTypeRoot) {
i += 5; // parentID, ownerID, displayNameStringID, keyStringID, name prop
} else {
i += 4; // isStrictModeCompliant, Profiling flag, supportsStrictMode flag, hasOwnerMetadata flag
}
break;
}
```
How can I resolve this? If you propose a fix, please make it concise.| case TREE_OPERATION_REMOVE_ROOT: { | ||
| throw Error('Operation REMOVE_ROOT is not supported while profiling.'); | ||
| // Removing roots is not supported while profiling; log and ignore. | ||
| console.warn('Operation REMOVE_ROOT is not supported while profiling. Ignoring.'); | ||
| break; |
There was a problem hiding this comment.
REMOVE_ROOT operation has 1 parameter (root ID) that must be consumed. Breaking without advancing i causes the root ID to be misinterpreted as an operation code on the next iteration.
| case TREE_OPERATION_REMOVE_ROOT: { | |
| throw Error('Operation REMOVE_ROOT is not supported while profiling.'); | |
| // Removing roots is not supported while profiling; log and ignore. | |
| console.warn('Operation REMOVE_ROOT is not supported while profiling. Ignoring.'); | |
| break; | |
| case TREE_OPERATION_REMOVE_ROOT: { | |
| // Removing roots is not supported while profiling; log and ignore. | |
| console.warn('Operation REMOVE_ROOT is not supported while profiling. Ignoring.'); | |
| i++; // Skip root ID parameter | |
| break; | |
| } |
Prompt To Fix With AI
This is a comment left during a code review.
Path: packages/react-devtools-shared/src/devtools/views/Profiler/CommitTreeBuilder.js
Line: 317:320
Comment:
REMOVE_ROOT operation has 1 parameter (root ID) that must be consumed. Breaking without advancing `i` causes the root ID to be misinterpreted as an operation code on the next iteration.
```suggestion
case TREE_OPERATION_REMOVE_ROOT: {
// Removing roots is not supported while profiling; log and ignore.
console.warn('Operation REMOVE_ROOT is not supported while profiling. Ignoring.');
i++; // Skip root ID parameter
break;
}
```
How can I resolve this? If you propose a fix, please make it concise.| const ops0 = [0, rootID, 0]; | ||
| const ops1 = [ | ||
| 0, rootID, 0, | ||
| 1, 2, ElementTypeFunction, rootID, 0, 0, 0, | ||
| 2, 1, 999, | ||
| 6, | ||
| 99, | ||
| ]; |
There was a problem hiding this comment.
the test doesn't actually validate the malformed operations it intends to test. Operation 0 is not a valid operation code (valid codes are 1-13 per constants.js:20-32). When operation 0 is encountered in both ops0 and at the start of ops1, it hits the default case (CommitTreeBuilder.js:499-503), which sets i = operations.length and immediately stops processing. The subsequent malformed operations in ops1 (duplicate ADD at position 1, REMOVE of missing node at position 2, REMOVE_ROOT at position 6, unsupported op 99) are never reached.
to properly test error handling, use valid initial operations:
| const ops0 = [0, rootID, 0]; | |
| const ops1 = [ | |
| 0, rootID, 0, | |
| 1, 2, ElementTypeFunction, rootID, 0, 0, 0, | |
| 2, 1, 999, | |
| 6, | |
| 99, | |
| ]; | |
| const ops0 = [4, rootID, 0]; // TREE_OPERATION_UPDATE_TREE_BASE_DURATION | |
| const ops1 = [ | |
| 4, rootID, 0, // TREE_OPERATION_UPDATE_TREE_BASE_DURATION | |
| 1, 2, ElementTypeFunction, rootID, 0, 0, 0, // duplicate ADD (node 2 exists in snapshot) | |
| 2, 1, 999, // REMOVE missing node 999 | |
| 6, // REMOVE_ROOT | |
| 99, // unsupported operation | |
| ]; |
Prompt To Fix With AI
This is a comment left during a code review.
Path: packages/react-devtools-shared/src/__tests__/profilingCommitTreeBuilder-test.js
Line: 369:376
Comment:
the test doesn't actually validate the malformed operations it intends to test. Operation `0` is not a valid operation code (valid codes are 1-13 per constants.js:20-32). When operation `0` is encountered in both `ops0` and at the start of `ops1`, it hits the default case (CommitTreeBuilder.js:499-503), which sets `i = operations.length` and immediately stops processing. The subsequent malformed operations in `ops1` (duplicate ADD at position 1, REMOVE of missing node at position 2, REMOVE_ROOT at position 6, unsupported op 99) are never reached.
to properly test error handling, use valid initial operations:
```suggestion
const ops0 = [4, rootID, 0]; // TREE_OPERATION_UPDATE_TREE_BASE_DURATION
const ops1 = [
4, rootID, 0, // TREE_OPERATION_UPDATE_TREE_BASE_DURATION
1, 2, ElementTypeFunction, rootID, 0, 0, 0, // duplicate ADD (node 2 exists in snapshot)
2, 1, 999, // REMOVE missing node 999
6, // REMOVE_ROOT
99, // unsupported operation
];
```
How can I resolve this? If you propose a fix, please make it concise.|
Upstream PR was closed or merged. Code is synced via branch mirror. |
Mirror of facebook/react#35674
Original author: apoorvgarewal07