-
Notifications
You must be signed in to change notification settings - Fork 0
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
Is this proposal strict enough for its use cases? #1
Comments
At first I was trying to think through some approach wherein some clever indirection could add some stronger guarantees to these Resources. I was thinking, "what if I couldn't think of many precedents in terms of APIs that strictly required runtime support like this. The obvious one that did come to mind though, was |
Our position is that if you are explicitly calling
That is explicitly not a feature of this proposal and I am not making any such guarantees.
Sure, but if you've chosen to explicitly call |
I think the name |
Not arguing against that, but we are saying that it's not a strict enough guarantee to solve
It's not just manual calls to class Foo {
private stack = new DisposableStack();
example() {
const scope = new AsyncContext.Scope(someSnapshot);
disposable.use(scope);
// Do stuff…
// didn't dispose here!
}
[Symbol.dispose]() {
this.stack[Symbol.dispose]();
}
}
const v = new AsyncContext.Variable();
using f = new Foo();
v.run(1 , () => {
assert.equal(v.get(), 1);
// f is properly using a DispoableStack, but that's not enough
// to prevent a leak.
f.example();
// f's context has leaked out of it's method call.
assert.equal(v.get(), undefined);
}); Ideally, we'd like a way to detect syntatic |
This seems problematic w.r.t. syntax transformation. This sort of feature would (by definition) be untranspilable, making it useful (at best) only in the very-narrow case of transpiling later language features that would depend on it. |
Such a mechanism would have to also support eg |
Someone mentioned a
During the |
Right - which is why "enforcing syntactic usage" won't work, because that would give you a false negative. |
Not quite. It would have to be reset for further nested stacks. |
Not from my point of view. I specifically do not want it to pass, so it's giving a true negative. It is not acceptable to use
Ah, yes, it should reset to the previous value. But still easily implementable with a transform. |
Why isn't that acceptable? It's going to be properly disposed. This really feels like it should just be a linter rule, like no-floating-promises - especially given that there's differing opinions about what level of enforcement is desired. |
@jridgewell and I have been discussing this on Matrix. The kinds of guarantees that using x = new Resource(); to const getResource = () => new Resource();
using x = getResource(); will break their code. The intended use of I have also proposed an alternative approach, where each successive |
This proposal's name is "strict enforcement of
using
", but while it solves the footgun of forgettingusing
, it is not strict against unintended usage of the disposable API.You might expect that a strict enforcement of an RAII-like pattern would guarantee not only that the
[Symbol.dispose]
method is always called, but that it is called before the current function returns, and that the ordering of[Symbol.enter]
and[Symbol.dispose]
calls across multiple disposables is properly stacked. Since this proposal allows JS code to call the[Symbol.enter]()
and[Symbol.dispose]()
methods, it guarantees none of these properties.As an example use case where these properties matter, in the AsyncContext proposal we were discussing the possibility of a disposable API (see tc39/proposal-async-context#60 and nodejs/node#52065), that would switch the current context when entered and restore the previous one when disposed. However, since the AsyncContext machinery is unaware of function boundaries, the possibility of an entered but non-disposed scope would pollute the caller's context:
Now, it is possible that this is a one-off, or a special case, and that most disposables would benefit from fixing the footgun without needing this level of strictness. But that would need to be looked into.
The text was updated successfully, but these errors were encountered: