-
Notifications
You must be signed in to change notification settings - Fork 30.7k
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
Review places where we use IDisposable[]
in the code
#74250
Comments
Part of microsoft#74250 - Extend `Disposable` in classes where it makes sense - Use `DisposableStore` for lists of disposables - Make `combinedDisposable` take arguments instead of an array (so that you can't pass in an array and then modify the array after the fact)
@mjbvz on some objects I need to track vscode/src/vs/workbench/contrib/terminal/node/terminalProcess.ts Lines 199 to 211 in 8a6ddac
This would either throw an exception (maybe native and crash) if the process has been disposed. |
Yes I'm ok to exposing this on |
🛑 Can we please make this a team effort instead of @mjbvz making all changes across ownership. Apart from the merge conflict potential and introducing bugs, there is a lot of personal taste in this. A change like this really makes me sad: 6b7b5c0#diff-2754a48a0975eb32f8b8ff03d9d67478R16. I generally favour composition over inheritance, esp. when it comes to Don't get me wrong, I am not against the "stricter disposable"-effort, even though I haven't seem much evidence or leaks because of register-after-dispose cases. What I am against is mass changes across ownership boundaries without notice - we don't accept PRs that do this and we should respect that rule ourself. |
cc @microsoft/vscode |
+1000. That
Leaks like this happen due to poor code reasoning, they are not inherent to this code pattern, imo. |
@jrieken I apologize if it felt like I was stepping on your toes here. For one, the size of the push to master got away from me. These changes should have gone in more incrementally. Let me provide some additional context on why I think this work is important and why the changes were made in the way there were made. I know that I've written incorrect code that uses Changing our internal APIs to make unsafe patterns like this more difficult to write is the right approach in my opinion, even if we are not aware of incorrect usage causing any issues at the moment. That's why I have been looking into disposables for debt weak and that's where the introduction of a While adding class Foo {
private _disposables: IDisposable[] = [];
...
dispose() {
this._disposables = dispose(_disposables);
}
} The repeated code is what made refactoring to use a I don't think inheritance is a great solution, but it's the only approach that I've found so far that works well with TS. If anyone has another idea on how on how we can better share code for disposable objects, let me know Ultimately I think we should work towards standardizing on common, safe patterns across all of our codebase. What these patterns should be is something we should discuss together. In this case, I did not alert enough team members to the disposable work I was interested in, or provide enough time for feedback or discussion on the proposed changes. I will try to do better at this in the future |
Bottom line is that everyone agrees that |
I have pushed a change that makes events accept a disposable store (in addition to IDisposable[], which is API (!)), e.g. |
Closing as I don't think we can/want to replace all instances. Almost all new code should be using |
Problem
Our code commonly uses
IDisposable[]
to track multiple disposables. This is not safe and may leak disposables. Consider:This results in
DisposableThing2
being leaked. This sequence of calls can easily happen in async code or in complex sync codeProposed Fix
Review places where we use
IDisposable[]
. In almost all cases, we should either:For classes, extend
Disposable
instead of creating adisposables
private property. TheDisposable
class correctly handles the above case.In place of a
IDisposable[]
, useDisposableStore
which was added by Add DisposableStore for use in VS Code #74242 for this very reason.The text was updated successfully, but these errors were encountered: