Skip to content

Avoid flush in finalizers for Socket and IO::FileDescriptor#14882

Merged
straight-shoota merged 7 commits intocrystal-lang:masterfrom
straight-shoota:fix/fd-socket-finalize-close
Aug 13, 2024
Merged

Avoid flush in finalizers for Socket and IO::FileDescriptor#14882
straight-shoota merged 7 commits intocrystal-lang:masterfrom
straight-shoota:fix/fd-socket-finalize-close

Conversation

@straight-shoota
Copy link
Member

@straight-shoota straight-shoota commented Aug 8, 2024

Trying to flush means that it can try to write, hence call into the event loop, that may have to wait for the fd to be writable, which means calling into epoll_wait. This all while the world is stopped during a garbage collection run. The event loop implementation may need to allocate, or we get an error and try to raise an exception that will also try to allocate memory... again during a GC collection.

For Socket this change has little impact becase sync is true by default which means every byte is sent directly without buffering.
IO::FileDescriptor (and File) is buffered by default, so this may lead to loss of data if you don't properly close the file descriptor after use.

Resolves #14807

@straight-shoota straight-shoota added breaking-change May have breaking effect in some edge cases. Mostly negligible, but still noteworthy. topic:stdlib:files kind:chore labels Aug 8, 2024
@straight-shoota straight-shoota self-assigned this Aug 8, 2024
@straight-shoota straight-shoota added kind:bug A bug in the code. Does not apply to documentation, specs, etc. and removed kind:chore labels Aug 8, 2024
@straight-shoota straight-shoota added this to the 1.14.0 milestone Aug 12, 2024
@straight-shoota straight-shoota merged commit d54a91f into crystal-lang:master Aug 13, 2024
@straight-shoota straight-shoota deleted the fix/fd-socket-finalize-close branch August 13, 2024 21:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

breaking-change May have breaking effect in some edge cases. Mostly negligible, but still noteworthy. kind:bug A bug in the code. Does not apply to documentation, specs, etc. topic:stdlib:files

Projects

None yet

Development

Successfully merging this pull request may close these issues.

IO::FileDescriptor & Socket finalizers do far too much

2 participants