Skip to content

Docs: std::alloc::set_alloc_error_hook clarifications #56965

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

Open
brson opened this issue Dec 18, 2018 · 2 comments
Open

Docs: std::alloc::set_alloc_error_hook clarifications #56965

brson opened this issue Dec 18, 2018 · 2 comments
Labels
A-docs Area: Documentation for any part of the project, including the compiler, standard library, and tools C-enhancement Category: An issue proposing an enhancement or a PR with one. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.

Comments

@brson
Copy link
Contributor

brson commented Dec 18, 2018

In using this function I am wondering a few things:

  • What happens if I oom while handling an oom? Is the oom handler called again? Does the process immediately abort (ala panic)?

  • Does triggering oom remove the custom oom handler? The presence of take_alloc_error_hook makes it seem like that may the case, but it is not. It looks like the custom handler stays in place through its invocation. Docs would be better to mention that it's possible to oom recursively and invoke your oom handler recursively.

  • Is OOM the only time that memory allocation fails? The document doesn't mention "out of memory", but the situation this handles is commonly called "OOM". The docs would better mention "out of memory" and whether there is a distinction between allocation failure types.

  • Does take_alloc_error_hook restore the default error hook?

Are the answers to these questions known and intended? Please add them to the docs.

Here are some harder questions:

Can I and should I try to recover from an oom? The oom handling mechanism calls abort after calling the oom handler, but what if I panic in the handler?

I could empty my caches or otherwise mess with the allocator, panic, catch the panic, and try to continue. Is that a valid and intended use case?

@brson brson changed the title Docs: alloc::set_alloc_error_hook clarifications Docs: std::alloc::set_alloc_error_hook clarifications Dec 18, 2018
@sfackler sfackler added the A-docs Area: Documentation for any part of the project, including the compiler, standard library, and tools label Dec 18, 2018
@steveklabnik steveklabnik added T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. and removed A-docs Area: Documentation for any part of the project, including the compiler, standard library, and tools labels Dec 27, 2018
@steveklabnik
Copy link
Member

re-tagging as @rust-lang/libs ; please let me know what semantics you want documented, and then we can document them. Thanks!

@sfackler
Copy link
Member

  • What happens if I oom while handling an oom? Is the oom handler called again? Does the process immediately abort (ala panic)?

The alloc_error_hook is a little bit less "magical" than panic_hook - it just sets what code runs when you call handle_alloc_error, which is what library code does by convention when it hits an allocation failure that it doesn't want to handle itself. There are no reentrancy issues - if you call the hook from the hook, it's just a normal recursive call.

  • Does triggering oom remove the custom oom handler? The presence of take_alloc_error_hook makes it seem like that may the case, but it is not. It looks like the custom handler stays in place through its invocation. Docs would be better to mention that it's possible to oom recursively and invoke your oom handler recursively.

It doesn't remove the custom handler. take_alloc_error_hook is a mirror of take_panic_hook - it's designed to allow you to "wrap" the existing hook rather than replacing it entirely.

  • Is OOM the only time that memory allocation fails? The document doesn't mention "out of memory", but the situation this handles is commonly called "OOM". The docs would better mention "out of memory" and whether there is a distinction between allocation failure types.

The other possible failure situation is if you ask for a layout that the allocator can't provide. I think the main example of this is an over-aligned allocation.

  • Does take_alloc_error_hook restore the default error hook?

Yep.

@Enselic Enselic added A-docs Area: Documentation for any part of the project, including the compiler, standard library, and tools C-enhancement Category: An issue proposing an enhancement or a PR with one. labels Nov 19, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-docs Area: Documentation for any part of the project, including the compiler, standard library, and tools C-enhancement Category: An issue proposing an enhancement or a PR with one. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

4 participants