Skip to content

Protect watcher from double close #49990

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

Merged
merged 1 commit into from
Jul 22, 2022
Merged

Conversation

jakebailey
Copy link
Member

Fixes #49961

@typescript-bot typescript-bot added Author: Team For Milestone Bug PRs that fix a bug with a specific milestone labels Jul 21, 2022
@jakebailey jakebailey requested a review from sheetalkamat July 21, 2022 20:28
@jakebailey jakebailey requested a review from andrewbranch July 21, 2022 20:28
Copy link
Member

@sheetalkamat sheetalkamat left a comment

Choose a reason for hiding this comment

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

I am not confident in if we should take this or not..
Normally this exception is either because the state is messed up because of other exception (in which case this fix is ok as it shuts down the noise) but there could be some legitimate issues with watcher ref counting etc that we might not be able to detect easily. So i am more on the side of not taking this fix rather than taking but not totally against.

@sheetalkamat
Copy link
Member

Eg. in the tsserver log there were some exceptions that could result in projects being in incorrect state

@jakebailey
Copy link
Member Author

Well, at least in the case of #49961, if the error in the log before this one was in fact #49310, then I at least have a fix for that already.

But, I think in the context of this code, it feels like we should at least either make the watcher potentially undefined and conditionally close it, or make it always required and remove the undefined! and allow close to proceed if it's called a second time. (Both of which are type safe.)

watchMissingFileSystemEntry() :
watchPresentFileSystemEntry();
return {
close: () => {
// Close the watcher (either existing file system entry watcher or missing file system entry watcher)
watcher.close();
Copy link
Member

Choose a reason for hiding this comment

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

What i am saying is that this should be watcher!.close() closing same watcher multiple times is potentially issue somewhere else.

Watcher is set to undefined because we want to update them if its not closed look for updateWatcher right below this

Copy link
Member Author

Choose a reason for hiding this comment

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

That's exactly what the code is today, though. We have let watcher: FileWatcher = ....

If we leave this code as it is now, it will crash if close is called, because it sets the watcher to undefined.

This PR doesn't change the fact that the watcher can be undefined, it only declares that watcher can be undefined (which makes it clear that watcher.close() is a type error), then ignores all closes but the first by skipping if it's already been set to undefined. watcher is still set to undefined so updateWatcher will behave identically and not perform any updates if the watcher has been closed.

Copy link
Member Author

Choose a reason for hiding this comment

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

But, my suggestion of

make it always required and remove the undefined! and allow close to proceed if it's called a second time

Doesn't work, of course, if we are trying to avoid updateWatcher from creating a new watcher even if it's closed.

So, we can either ignore the double close (this PR), or I guess we can do a debug assert and figure out who is closing this a second time and shouldn't be (which is the thing I was unsure was correct, given there are watchers out there that are refcounted and intend to be closed more than once).

Copy link
Member

Choose a reason for hiding this comment

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

given there are watchers out there that are refcounted and intend to be closed more than once).

This is not one of the watcher thats ref counted ones close over physical watcher and are suppose to close only once when ref count is 0

Copy link
Member Author

Choose a reason for hiding this comment

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

So, we should do what this PR does then, and ignore a second close?

Copy link
Member

Choose a reason for hiding this comment

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

I gave more thought to this.. I think Debug.assert would be ideal but given its more likely to cause it in case of other exceptions i think your fix is good. Sorry for going back and forth on this one..

Copy link
Member Author

Choose a reason for hiding this comment

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

All good, just wanted to ensure I understood the right thing here.

I did try and add some debug asserts to every close implementation I could find (considering ref counting too), but didn't actually hit any asserts either. I guess that's what happens without a repro.

@jakebailey jakebailey merged commit 4e23f51 into microsoft:main Jul 22, 2022
@jakebailey jakebailey deleted the fix-49961 branch July 22, 2022 20:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Author: Team For Milestone Bug PRs that fix a bug with a specific milestone
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Cannot read properties of undefined (reading 'close')
4 participants