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

Fix: make Crystal::EventLoop#remove(io) a class method #15282

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

ysbaddaden
Copy link
Contributor

@ysbaddaden ysbaddaden commented Dec 16, 2024

The method is called from IO::FileDescriptor and Socket finalizers, which means they can be run from any thread during GC collections, yet calling an instance method means accessing the current event loop, which may have not been instantiated yet for the thread.

Only the polling event loops (epoll, kqueue) need this method, and they don't need an instance method to clean up the fd in the global arena. The other event loops don't need it.

This patch thus changes the instance method into a class method so there isn't any attempt to allocate in a GC finalizer.

Bonus: being a NOOP for other event loops, the call will be optimized away, while it previously always had to resolve the event loop instance; the polling event loops will also be able to directly cleanup.

The method is called from IO::FileDescriptor and Socket finalizers,
which means they can be run from any thread during GC collections, yet
calling an instance method means accessing the current event loop, which
may have not been instantiated yet for the thread.
@ysbaddaden ysbaddaden force-pushed the fix/event-loop-remove branch from 9e0efc2 to 31f39d7 Compare December 16, 2024 11:51
@ysbaddaden ysbaddaden added kind:bug A bug in the code. Does not apply to documentation, specs, etc. topic:stdlib:runtime labels Dec 16, 2024
@ysbaddaden ysbaddaden self-assigned this Dec 16, 2024
The API won't change if we start having two potential event loop
implementations in a single binary (e.g. io_uring with a fallback to
epoll).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind:bug A bug in the code. Does not apply to documentation, specs, etc. topic:stdlib:runtime
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants