This is an archived snapshot of W3C's public bugzilla bug tracker, decommissioned in April 2019. Please see the home page for more details.

Bug 23412 - PortCollection exposes GC behavior
Summary: PortCollection exposes GC behavior
Status: RESOLVED MOVED
Alias: None
Product: WHATWG
Classification: Unclassified
Component: HTML (show other bugs)
Version: unspecified
Hardware: PC Linux
: P2 major
Target Milestone: Unsorted
Assignee: Ian 'Hixie' Hickson
QA Contact: contributor
URL:
Whiteboard: blocked looking for a better solution
Keywords:
Depends on:
Blocks:
 
Reported: 2013-10-01 18:04 UTC by Olli Pettay
Modified: 2015-09-28 17:18 UTC (History)
4 users (show)

See Also:


Attachments

Description Olli Pettay 2013-10-01 18:04:23 UTC
One can iterate the ports in the collection using a timer to see
when gc has collected certain ports.
Comment 1 Ian 'Hixie' Hickson 2013-10-01 20:28:00 UTC
We need something for shared workers to be able to keep notifying their clients of server updates without the shared worker actually holding a reference to the port (since that would prevent the port from getting GC'ed). Do you have another proposal for how to do this?
Comment 2 Anne 2013-10-05 19:53:34 UTC
Nobody has implemented this and the API has issues. We're not introducing close events for MessagePort either for exactly this reason. Let's not add new bugs.
Comment 3 Ian 'Hixie' Hickson 2013-10-07 22:14:20 UTC
That doesn't answer my question...
Comment 4 Anne 2013-10-08 09:18:57 UTC
We don't have another proposal. We thought of moving postMessage() to PortCollection and remove the enumerability, but that would still allow you to observe GC.

I hope no vendor will add a WeakMap with enumerability though, that'd be bad.
Comment 5 Ian 'Hixie' Hickson 2013-10-09 20:21:33 UTC
WeakMap has exactly the same problem. You just maintain a list of keys that you've added, and check to see which are still there. Adding built-in enumerability doesn't add anything new, in terms of being able to detect when GC happened.

Fundamentally, the goal here is to do something detectable on the subset of objects that has not yet been garbage collected from a larger set of objects that have been registered. This is the exact same goal as WeakMap; in fact, you could completely implement PortCollection using WeakMap (though it'd be a bit ugly, which is why we have PortCollection). Fundamentally, I don't see how, with that goal, you can't avoid being able to detect when GC has happened.

The API design of both PortCollection and WeakMap, however, has the feature that while you can detect if you go out of your way to do so, the normal way of using these APIs will not result in you noticing when one of the objects is GC'ed.
Comment 6 Olli Pettay 2013-10-09 21:02:50 UTC
WeakMap doesn't expose GC behavior.
The weak part is the key there, not value, and you can't 
iterate WeakMap
Comment 7 Ian 'Hixie' Hickson 2013-10-09 21:16:04 UTC
My bad. I misunderstood WeakMap. (I don't understand why we have WeakMap then. It doesn't seem to do anything you can't do by just hanging a property off the key.)

In any case, we need _something_ to be able to broadcast to ports without preventing them from getting garbage collected, since otherwise we'd have a huge memory leak. The API is indeed designed to avoid making it easy to depend on the precise details of GC. It's no guarantee. But without a better idea...