Skip to content
This repository has been archived by the owner on Jan 25, 2022. It is now read-only.

Interesting impact on other specifications #31

Open
domenic opened this issue Jun 18, 2018 · 5 comments · Fixed by #120
Open

Interesting impact on other specifications #31

domenic opened this issue Jun 18, 2018 · 5 comments · Fixed by #120

Comments

@domenic
Copy link
Member

domenic commented Jun 18, 2018

whatwg/streams#932 by @ricea brings up an interesting situation. I'm curious what the champions have to say about this, although I don't anticipate any changes to the weak refs proposal because of it.

The summary is:

  • A spec is written so that spec-created object y holds on to a user-provided object x indefinitely
  • However, after calling y.close(), y will never use x.
  • Thus, it is currently unobservable whether or not an implementation allows x to be GCed before y.

With weak refs, this of course becomes observable. Thus once weak refs are introduced, the spec as-written mandates that x never be GCed before y.

In reaction to this, the spec author should probably change y.close() to null out the internal reference to x explicitly. Then browsers will again be allowed to collect x before y and free up some memory, while still conforming to the spec.

Generalizing, the tricky parts of the situation are:

  • Spec authors have to be more aggressive about nulling out internal references than they were before, when such references were unobservable. (Assuming they want to avoid mandating unnecessary memory usage.)
  • Implementers now have to be exact in following specs' recommendations on nulling out (or not) an internal reference, since doing so is now observable.
  • There are probably many divergences today in memory management strategies between browsers on various APIs of this sort, which may need to be audited now that the strategy is becoming observable.
@erights
Copy link
Contributor

erights commented Jun 19, 2018

Specifying the semantics of gc is hard. At http://web.archive.org/web/20160318124045/http://wiki.ecmascript.org/doku.php?id=strawman:gc_semantics I suggest an upper bound on what may be collected (i.e., a lower bound on what must be retained):

Our safety requirements allow some reachable objects to be collected as well, so long as the garbage collector can ascertain that they will never be reached.

See the three papers linked at the bottom of that page. They are good but I no longer remember them in any detail.

@jorendorff
Copy link

Hmm. I doubt this is really a problem. The web platform doesn't specify a GC algorithm that has a mark phase and unconditionally visits every internal field. x can be collected, even though y is specified to have a field that points to it, because x is unreachable in the plain English sense—it can't be reached. The field is dead.

@littledan
Copy link
Member

Interestingly, this specification itself faces these liveness issues, in #66. Overall, though, I don't think we should take any action here on the specification side, since spec datastructure reachability already isn't the definition of reachability by garbage collectors.

For example, the following code sample shouldn't hold up bigData's collection for the whole activation frame to go away:

(function() { 
  const bigData = { /* lots of stuff */ };
  const group = new FinalizationGroup(iterator => {
    for (const item of iterator) console.log(`collected ${item}`);
  });
  group.register(bigData, "data");
  setTimeout(() => console.log("timeout"), 1000);
})();

This should be allowed to print out:

collected data
timeout

However, in terms of spec data structures, the environment record which points to bigData is still referenced from the function, and the environment record is referenced when looking up console.


There's still a practical issue about potential compatibility issues where one engine has a more precise definition of liveness than another. But JavaScript contexts are already a very present case of this, where developers already report out-of-memory issues that exist on some browsers and not others, which can be traced back to diverging issues of liveness. The details of streams' liveness will be much smaller in magnitude than closures, but at the same time, there's no clear path to compatibility in the closure case.

@littledan
Copy link
Member

Anyway, maybe we should mention this to the TAG and get their take as well. We should document clear guidance for specification authors about what all this means.

littledan added a commit to littledan/proposal-weakrefs that referenced this issue May 28, 2019
This attempts to address various issues with how reachability
is defined:
- If one WeakRef object points to an object, which points to
  another one, the whole subgraph may be collected, as raised in
  https://gist.github.com/jimblandy/0014dc11233d2d40df922af850b0489a
- An initial provisional definition of collectability is given:
  >   No possible future execution of the program would observably
  >   reference the object, except through a chain of references which
  >   includes a [[Target]] field or [[Target]] internal slot.
  This is intended to resolve tc39#31 (by concluding that the impact on
  host specifications is minimal, and spec graph reachability does not
  imply liveness) and provide a starting point for tc39#105
- The wording around 'field or internal slot' is clarified, resolving tc39#115
littledan added a commit that referenced this issue May 29, 2019
This attempts to address various issues with how reachability
is defined:
- If one WeakRef object points to an object, which points to
  another one, the whole subgraph may be collected, as raised in
  https://gist.github.com/jimblandy/0014dc11233d2d40df922af850b0489a
- An initial provisional definition of collectability is given:
  >   No possible future execution of the program would observably
  >   reference the object, except through a chain of references which
  >   includes a [[Target]] field or [[Target]] internal slot.
  This is intended to resolve #31 (by concluding that the impact on
  host specifications is minimal, and spec graph reachability does not
  imply liveness) and provide a starting point for #105
- The wording around 'field or internal slot' is clarified, resolving #115
@littledan
Copy link
Member

Reopening this issue to discuss possibilities for spec notation for weak references; see #168 (comment) .

@littledan littledan reopened this Nov 23, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants