Hacker News new | past | comments | ask | show | jobs | submit login

Basically, looking at the target of a WeakRef makes it ineligible for collection until at least the next await, which the spec mandates by way of the [[KeptAlive]] list. This is meant to prevent TOCTOU bugs by foolish programmers doing this:

    if (weakref.deref()) {
        /* garbage collection here */
        use(weakref.deref());
    }



Isn't that a rather narrow case? What happens on the following, is that protected?

if (weakref.deref()) { /* garbage collection here */ use(heavyComputations(), weakref.deref()); }


Yes, as long as there is no intervening suspension (await, or the script otherwise exiting to the event loop).




Consider applying for YC's W25 batch! Applications are open till Nov 12.

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: