golang / go

The Go programming language
https://go.dev
BSD 3-Clause "New" or "Revised" License
123.17k stars 17.56k forks source link

proposal: syscall/js: remove Func.Release() #57435

Open garet90 opened 1 year ago

garet90 commented 1 year ago

I propose we remove the js.Func.Release() method.

Problem

Currently, js.Func's are very prone to bugs. Developers have to manually call Release() on allocated js.Func's after they are sure there are no references. But, releasing funcs too early leads to errors when called again from the js side. On the other hand, never releasing funcs (although safer) results in memory leaks.

Unlike OS files, socket connections, and other manually freed resources, the lifetime of js.Func's are sometimes unclear, making them a good candidate for automatic collection.

Solution

In 2021, JavaScript added a new FinalizationRegistry class where a function may be called when an object is cleaned up by the garbage collector. Using this, we can clean up the func's resources when they are gc'd on the JavaScript side.

Compatibility

syscall/js is not covered by the go compatibility promise.

Alternatively, to not break current apps, Func.Release() can be turned into a no-op or work as usual alongside the new auto gc.

Most modern versions of major browsers support FinalizationRegistry, which is the current precedent for implementing changes to the wasm backend (see comment). In the event of incompatibility, the js.Func can be leaked, still allowing the program to run as normally as possible.

ianlancetaylor commented 1 year ago

CC @golang/js

Zxilly commented 1 month ago

I suppose I could write a patch for this, converting func.Release to a no-op

gopherbot commented 3 weeks ago

Change https://go.dev/cl/610566 mentions this issue: syscall/js: auto recycle Func