Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix context stack misalignment caused by error replay #12508

Merged
merged 5 commits into from
Apr 2, 2018
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 22 additions & 5 deletions packages/react-reconciler/src/ReactFiberScheduler.js
Original file line number Diff line number Diff line change
Expand Up @@ -263,9 +263,16 @@ export default function<T, P, I, TI, HI, PI, C, CC, CX, PL>(

let stashedWorkInProgressProperties;
let replayUnitOfWork;
let isReplayingFailedUnitOfWork;
let originalReplayError;
if (__DEV__ && replayFailedUnitOfWorkWithInvokeGuardedCallback) {
stashedWorkInProgressProperties = null;
replayUnitOfWork = (failedUnitOfWork: Fiber, isAsync: boolean) => {
isReplayingFailedUnitOfWork = false;
replayUnitOfWork = (
failedUnitOfWork: Fiber,
error: mixed,
isAsync: boolean,
) => {
// Retore the original state of the work-in-progress
assignFiberPropertiesInDEV(
failedUnitOfWork,
Expand All @@ -290,12 +297,16 @@ export default function<T, P, I, TI, HI, PI, C, CC, CX, PL>(
break;
}
// Replay the begin phase.
isReplayingFailedUnitOfWork = true;
originalReplayError = error;
invokeGuardedCallback(null, workLoop, null, isAsync);
isReplayingFailedUnitOfWork = false;
if (hasCaughtError()) {
clearCaughtError();
} else {
// This should be unreachable because the render phase is
// idempotent
// If the begin phase did not fail the second time, set this pointer
// back to the original value.
nextUnitOfWork = failedUnitOfWork;
}
};
}
Expand Down Expand Up @@ -855,9 +866,15 @@ export default function<T, P, I, TI, HI, PI, C, CC, CX, PL>(
);
}
let next = beginWork(current, workInProgress, nextRenderExpirationTime);

if (__DEV__) {
ReactDebugCurrentFiber.resetCurrentFiber();
if (isReplayingFailedUnitOfWork) {
// Currently replaying a failed unit of work. This should be unreachable,
// because the render phase is meant to be idempotent, and it should
// have thrown again. Since it didn't, rethrow the original error, so
// React's internal stack is not misaligned.
throw originalReplayError;
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Should we maybe clear the variable just before throwing so we don't hold onto it until next replay?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Yeah good call

}
}
if (__DEV__ && ReactFiberInstrumentation.debugTool) {
ReactFiberInstrumentation.debugTool.onBeginWork(workInProgress);
Expand Down Expand Up @@ -935,7 +952,7 @@ export default function<T, P, I, TI, HI, PI, C, CC, CX, PL>(

if (__DEV__ && replayFailedUnitOfWorkWithInvokeGuardedCallback) {
const failedUnitOfWork = nextUnitOfWork;
replayUnitOfWork(failedUnitOfWork, isAsync);
replayUnitOfWork(failedUnitOfWork, thrownValue, isAsync);
}

const sourceFiber: Fiber = nextUnitOfWork;
Expand Down