Implement finalizers for Context, Sender, Receiver #13
Labels
enhancement
New feature or request
good first issue
Good for newcomers
help wanted
Contributions are welcome
Context, Sender, and Receiver are long-living objects that own rather heavy native resources (C library handles). User is responsible to call Close() to free those resources.
If user forgets to call Close(), GC will collect go structs, but corresponding native resources will leak. It may be relatively easy to forget calling Close() because this objects typically are not used as local variables for which we can use defer, but instead are leaving in global variables or being part of other long-living objects.
We could reduce damage by forgetting calling Close() by setting finalizers for those objects. Finalizers have performance hit, but it's not critical here because the number of these kind of objects is not high.
Steps:
to Sender and Receiver, add unexported field referencing their Context; it will prevent GC to collect Context before Sender and Receiver; it's not allowed to close native context before closing all native senders and receivers attached to it
in OpenContext, OpenSender, OpenReceiver, attach finalizer to the created object; finalizer should just invoke Close method
in Close method, detach finalizer from the object; so that if the user explicitly closes the object, finalizers are not needed anymore
add tests for context, sender, and receiver; check that after creating those objects and forgetting to close them, and after repeatedly calling GC() in a loop, all those objects are closed automatically
The text was updated successfully, but these errors were encountered: