Skip to content

Fix missing undefined checks #36599

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

Closed
wants to merge 2 commits into from
Closed

Conversation

justsml
Copy link

@justsml justsml commented Feb 4, 2020

Fixes #32851
Fixes #32864

Added defensive null checks to prevent my VS Code's 'window logs' from filling up with TypeScript Server Error (3.7.3) error messages every few minutes.

Tasks

  • Add/Verify null checks for all calls to node.emitNode in src/compiler/factoryPublic.ts
  • Verify tests.
  • Checking for other related bugs in issues.

Update: All tests pass.

@msftclas
Copy link

msftclas commented Feb 4, 2020

CLA assistant check
All CLA requirements met.

@RyanCavanaugh
Copy link
Member

@rbuckton please review, thanks!

@sandersn sandersn added the For Backlog Bug PRs that fix a backlog bug label Feb 11, 2020
@justsml
Copy link
Author

justsml commented Feb 21, 2020

Updates: I've noticed that this bug can cause a memory leak, and restarting the typescript process frees up the RAM.

Please let me know if there's anything I can do to help move this along.

I understand that this is not a very visible (or critical) bug. Reply whenever you can. 😇

I really appreciate your taking the time to review.
❤️

@sandersn
Copy link
Member

@justsml one of the best things you can do is to provide a small repro of the problem. I will let @rbuckton review this PR in-depth, but generally we avoid fixes that skip a problem and prefer to fix root causes.

@justsml
Copy link
Author

justsml commented Apr 14, 2020

@sandersn
Thanks for the advice. And normally I'd provide 1 (or more) ways to reproduce.

The reason I didn't here: I'm not sure how to reproduce it consistently. I've tried.

Latest Observations

  1. Might be triggered by viewing long .ts files w/ several layers of inheritance. "Quick fix" option in VS Code may also play a role.
  2. Enabling tracing/logging on the TS Lang Server uses more RAM+CPU, making it hard to find performance-related issues. and I'm not experienced enough triaging between the 'layers'/services here. I'd appreciate any links to dev resources if anyone has them handy. 💙

I wish I had an easy reproduction, but these sorts of issues that only creep up after day(s) are hard to write steps for.
I'm willing to try if anyone has tips, it's just an area in QA I've struggled with in the past.

Impact (low)

  1. Noisy logs. It can make it difficult to find messages you care about in VS Code's output log.
  2. Seems to correlate with increase memory usage and TSServer timeouts.
  3. Happens on a clean VS Code install. (Eventually. Leaving editor open for a 24+ hours seems to "help" get the error.)

@justsml
Copy link
Author

justsml commented Apr 14, 2020

And I agree, I'd prefer to find the root cause too. 😸

I realize this is just moving the 'Null Exception' up the stack. I'm hopeful it'll lead to more helpful stack traces closer to where the issue actually resides.

@jscheid
Copy link

jscheid commented Apr 25, 2020

I don't know about #32851, but for #32864 I have a reproduction here: https://github.com/jscheid/typescript-32864

Copy link
Contributor

@rbuckton rbuckton left a comment

Choose a reason for hiding this comment

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

While I understand the desire to add these checks, this isn't the source of the problem. Rather, somewhere there is an unsound cast to a non-nullable type. I'd much rather find that root cause if possible as it is likely to cause other instabilities elsewhere.

@@ -3403,7 +3403,7 @@ namespace ts {
node.emitNode = {} as EmitNode;
}

return node.emitNode;
return node && node.emitNode;
Copy link
Contributor

Choose a reason for hiding this comment

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

We shouldn't need this. We initialize the value if it isn't set two lines before this.

@@ -3448,7 +3448,7 @@ namespace ts {
* Gets a custom text range to use when emitting source maps.
*/
export function getSourceMapRange(node: Node): SourceMapRange {
const emitNode = node.emitNode;
const emitNode = node && node.emitNode;
Copy link
Contributor

Choose a reason for hiding this comment

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

If we're getting an error here its because we are somehow being passed a node that is undefined however we expect a defined node.

Copy link
Member

Choose a reason for hiding this comment

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

Probably a symbol.valueDeclaration via some means, since that's a known hole in our current type definitions.

@rbuckton
Copy link
Contributor

rbuckton commented Apr 25, 2020

Both #32851 and #32864 seem to be caused by this unchecked assertion in itemInfoForParameters (signatureHelp.ts):

  const args = createNodeArray(candidateSignature.typeParameters.map(p =>
      checker.typeParameterToDeclaration(p, enclosingDeclaration)!));
//                                                               ^

@rbuckton
Copy link
Contributor

It seems the reason for this is that we return undefined when we encounter an error when attempting to create the node. I would propose that the correct solution would be to modify itemInfoForParameters to just create a dummy type parameter for this case, something like:

...
checker.typeParameterToDeclaration(p, enclosingDeclaration) ??
    createTypeParameterDeclaration('?')
...

@rbuckton
Copy link
Contributor

rbuckton commented May 5, 2020

@justsml Do want to take on my proposed solution, or should I go ahead with a separate PR to fix this instead?

@weswigham
Copy link
Member

weswigham commented May 5, 2020

@rbuckton didn't #38273 fix those?

@rbuckton
Copy link
Contributor

rbuckton commented May 5, 2020

Ah, I didn't see that. Yes, it looks like it does cover this case.

@rbuckton rbuckton closed this May 5, 2020
@justsml
Copy link
Author

justsml commented May 27, 2020

@rbuckton
Thanks, I wish I coulda taken a swing at that solution. Next time though!

And thanks @weswigham for solving the root cause. 😻

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
For Backlog Bug PRs that fix a backlog bug
Projects
Archived in project
7 participants