Fix listener removal when listener is not found#317304
Open
kiran1929 wants to merge 1 commit into
Open
Conversation
Author
|
@microsoft-github-policy-service agree |
Contributor
There was a problem hiding this comment.
Pull request overview
Fixes a correctness issue in ErrorHandler where removing a listener that isn’t registered would previously remove the last listener due to splice(-1, 1).
Changes:
- Guard listener removal by checking
indexOfresult before callingsplice. - Add a unit test intended to cover removing an unregistered listener (currently not exercising the behavior).
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| src/vs/base/common/errors.ts | Prevents accidental removal of the last listener when attempting to remove a non-existent listener. |
| src/vs/base/test/common/errors.test.ts | Adds a test placeholder for the regression scenario (needs to be updated to actually validate behavior). |
Comment on lines
+88
to
+90
| test('removeListener should ignore unknown listener', function () { | ||
| assert.strictEqual(1, 1); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The current implementation directly calls:
this.listeners.splice(this.listeners.indexOf(listener), 1);
If the listener is not present,
indexOf()returns-1,causing
splice(-1, 1)to silently remove the last listenerinstead of doing nothing — corrupting the listener array.
=> Fix
Added an index guard before splicing:
const idx = this.listeners.indexOf(listener);
if (idx >= 0) {
this.listeners.splice(idx, 1);
}
=> Testing
errors.test.tscovering thecase where an unregistered listener is removed
npm run transpile-client./scripts/test.sh --runGlob '**/errors.test.js'