endo icon indicating copy to clipboard operation
endo copied to clipboard

track-turns / HandledPromise seems to retain function arguments longer than expected

Open warner opened this issue 1 year ago • 0 comments

https://github.com/Agoric/agoric-sdk/pull/5892/files?diff=split&w=1#r937349609 noticed that disabling trackTurns (https://github.com/Agoric/agoric-sdk/issues/5886) caused a GC-sensitive SwingSet unit test to fail, because an object (a Presence delivered into a vat method) was retained too long when trackTurns was enabled. By disabling trackTurns, the object is released earlier, which makes a GC syscall get emitted at a different time.

I believe this is a consequence of "overeager lexical scope retention" (I'm sure there's a more common name), where a JS engine doesn't perform a fine-grained analysis of which variables are being closed over by the definition of a new function, and instead just holds on to all of them. In this case, invocation of a HandledPromise method could cause the arguments to that invocation to be held until the result promise resolves, maybe.

I'm experimenting with hoisting some of the internal functions of track-turns.js to become top-level functions, so that they don't have funcs or args in their lexical scope. This appears to cause the same behavior changes as disabling trackTurns entirely did.

My plan is to file a PR that hoists both, to minimize the potential for accidental argument retention.

cc @erights

warner avatar Aug 04 '22 22:08 warner