Skip to content
This repository has been archived by the owner on Feb 12, 2022. It is now read-only.

Show callstack in browser upon REPL crashes #2021

Closed
wants to merge 6 commits into from
Closed

Conversation

caiismyname
Copy link
Contributor

Fixing some incompatibility between heapGraph and __optimize().
Essence of the issue was an implicit assumption that only one visitor would visit certain entries.

Original Issue: #1732

@@ -253,10 +253,11 @@ class ModifiedBindingEntry extends GeneratorEntry {
containingGenerator === this.containingGenerator,
"This entry requires effects to be applied and may not be moved"
);
invariant(this.modifiedBinding.value === this.newValue);
invariant(this.modifiedBinding.value === this.newValue, "ModifiedBinding's value has been improperly modified.");
Copy link
Contributor

Choose a reason for hiding this comment

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

This is a good example of an utterly useless comment. Much better (and much harder) is to write a comment that explains to a reader of the code why this invariant is expected to hold. In terms of debug logging, for those who care about such things, it would be better to include information about the two values.

Copy link
Contributor

@jeffreytan81 jeffreytan81 left a comment

Choose a reason for hiding this comment

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

Great job!

@@ -286,6 +286,7 @@ function runTest(name, code, options: PrepackOptions, args) {
options.invariantLevel = code.includes("// omit invariants") || args.verbose ? 0 : 99;
if (code.includes("// emit concrete model")) options.emitConcreteModel = true;
if (code.includes("// exceeds stack limit")) options.maxStackDepth = 10;
if (code.includes("// heapGraphFilePath")) options.heapGraphFormat = "VISJS";
Copy link
Contributor

Choose a reason for hiding this comment

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

It is not a file path, you are using VISJS format here. I would call "// heap graph" instead of heapGraphFilePath

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Will do. I put it like that originally to avoid confusion with the actual flag (the flag itself is --heapGraphFilePath so I kept it the same)

Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah, heapGraphFilePath is a command line level option not actually the engine/serializer level option, which is heapGraphFormat. But either way, we are just trying to enable heap graph and do not care about the output in this test.

@@ -182,6 +182,9 @@ export class Serializer {
additionalFunctionValuesAndEffects,
referentializer
);
// Copying dERB map so cache lookup returns the same object, and the subsequent
Copy link
Contributor

Choose a reason for hiding this comment

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

I would write the comment like: ModifiedBindingEntry caches the state from declarativeEnvironmentRecordsBindings during visitor so we have to pass the same cache to future visitors.

this.residualFunctionBinding = residualBinding;
this.modifiedBinding.value = newValue;
Copy link
Contributor

Choose a reason for hiding this comment

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

Is this change really needed? Looks a bit scary to me. I'd rather do less state mutation on the side than more.

Copy link
Contributor Author

@caiismyname caiismyname May 24, 2018

Choose a reason for hiding this comment

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

Without it, we run into the following problem:

In the provided example

(function () {
  let a = __abstract("number", "a");
  let b, c;
  global.f = function() {
    b = a + 42;
    c = a + 42;
  }
  __optimize(f);
})();

when the ModifiedBindingEntry does the .visitModifiedBinding, there is a .visitEquivalentValue(newValue) call on the new value, and the returned newValue may not match the original (it's replaced by the "equivalent" value if one is found). This happens to us in this test case because of the

b = a + 42;
c = a + 42;

since the a + 42 look the same. The assignment realigns the state to what is expected by the invariant assertion.

This problem actually seems to be at least somewhat independent of the problem caused by the cache/ResidualFunctionBinding, but it was in the same code area and also causes the code to crash.

It seems that the more fundamental issue is that we're re-traversing a the heap, which has some sort of assumption that it won't be re-traversed.

Copy link
Contributor

@NTillmann NTillmann May 25, 2018

Choose a reason for hiding this comment

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

which has some sort of assumption that it won't be re-traversed.

Yes, that's the underlying problem here...

While the introduced assignment makes the invariant violation go away, it changes what happens during the next visiting, which is bad.

The right thing to do would be to stop mutating state on the side in these GeneratorEntry's, they should be just values. Instead, build up new data structure in the visitor, a mapping of GeneratorEntry to something --- whatever mutable state there was, and pass that on to the next phase that really needs it, and just throw it away after the ResidualHeapGraphVisitor.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@yinghuitan was saying then that it might be easier to detect and reject calls to __optimize() when the --heapGraphFilePath flag is given, since the two haven't been made to work together yet. Would that be a more relevant solution?

if (additionalFunctionValuesAndEffects.size > 0) {
let diagnostic = new CompilerDiagnostic(
"Using __optimize with --heapGraphFilePath flag is not currently supported",
undefined,
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Wasn't sure what to give these fields since they're not really applicable. Suggestions?

Copy link
Contributor

@NTillmann NTillmann left a comment

Choose a reason for hiding this comment

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

If the new approach is now to fail with an error, then this would make our website REPL unusable as soon as __optimize is used.

Some heuristics could be used in the website code to disable the heap graph if the text contains the word __optimize. I am not a fan of that complicated working around the actual issue, but still better than crashing.

… callstacks along with error message (for all errors) in repl
@caiismyname
Copy link
Contributor Author

caiismyname commented May 31, 2018

Screenshot of new behavior on the code snippet that previously caused the REPL to crash
screen shot 2018-05-31 at 11 48 14 am

Copy link
Contributor

@jeffreytan81 jeffreytan81 left a comment

Choose a reason for hiding this comment

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

Awesome! Much better REPL diagnostics experience.
You may want to retitle the PR since we are not fixing crash anymore.

@@ -286,6 +286,7 @@ function runTest(name, code, options: PrepackOptions, args) {
options.invariantLevel = code.includes("// omit invariants") || args.verbose ? 0 : 99;
if (code.includes("// emit concrete model")) options.emitConcreteModel = true;
if (code.includes("// exceeds stack limit")) options.maxStackDepth = 10;
if (code.includes("// heapGraph")) options.heapGraphFormat = "VISJS";
Copy link
Contributor

Choose a reason for hiding this comment

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

I do not think we need this change anymore.

@caiismyname caiismyname changed the title Fix issue #1732 heapGraph crashes Show callstack in browser upon REPL crashes Jun 1, 2018
Copy link

@facebook-github-bot facebook-github-bot left a comment

Choose a reason for hiding this comment

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

@trueadm is landing this pull request. If you are a Facebook employee, you can view this diff on Phabricator.

@hermanventer hermanventer deleted the bug1732 branch June 5, 2018 20:06
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants