Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement finalizers for Context, Sender, Receiver #13

Open
gavv opened this issue Nov 13, 2019 · 4 comments
Open

Implement finalizers for Context, Sender, Receiver #13

gavv opened this issue Nov 13, 2019 · 4 comments
Labels
enhancement New feature or request good first issue Good for newcomers help wanted Contributions are welcome

Comments

@gavv
Copy link
Member

gavv commented Nov 13, 2019

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

@gavv

This comment was marked as outdated.

@gavv gavv added the enhancement New feature or request label Jun 3, 2020
@Asalle

This comment was marked as outdated.

@gavv gavv added help wanted Contributions are welcome good first issue Good for newcomers hacktoberfest labels Sep 28, 2020
@gavv gavv removed the hacktoberfest label Dec 3, 2020
@Asalle

This comment was marked as outdated.

@gavv

This comment was marked as outdated.

@gavv gavv changed the title Implement finalizers? Implement finalizers Dec 23, 2022
@gavv gavv changed the title Implement finalizers Implement finalizers for Context, Sender, Receiver May 23, 2023
@gavv gavv pinned this issue Sep 30, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request good first issue Good for newcomers help wanted Contributions are welcome
Projects
None yet
Development

No branches or pull requests

2 participants