-
Notifications
You must be signed in to change notification settings - Fork 3k
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
Add documentation on returning via throw #5450 #6053
base: master
Are you sure you want to change the base?
Conversation
CT Test Results 2 files 95 suites 35m 23s ⏱️ For more details on these failures, see this check. Results for commit 077f9ed. ♻️ This comment has been updated with latest results. To speed up review, make sure that you have read Contributing to Erlang/OTP and that all checks pass. See the TESTING and DEVELOPMENT HowTo guides for details about how to run test locally. Artifacts// Erlang/OTP Github Action Bot |
Hello! There are no tests that this actually works for gen_server, so if we want to extend the documentation to guarantee that this should work then there need to be new tests written. Since gen_statem allows this type of return, I think that we should add it to gen_server as well. global_group (nor any other module that I'm aware of in Erlang/OTP) does not use this undocumented feature. It wraps all calls in a try catch that catches the throw and returns the correct value. |
@garazdawi you make some good points (I didn't actually check the modules I mentioned; I only searched for
I don't know which of these is the way forwards, and the first might be a sensible stopgap for the next release if it's controversial. What do you think? |
lib/stdlib/doc/src/gen_server.xml
Outdated
<c>gen_server</c> process terminates.</p> | ||
<c>gen_server</c> process terminates. However, if a callback function | ||
raises a <c>throw</c> exception, the callback will be treated as if | ||
it succeeded and returned the exception's exit reason.</p> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wonder if that comes from ancient catch
usage. I've seen throws being mis-labelled as normal returns elsewhere for that reason.
IIRC that's been discussed on the Erlang Forums (with no specific conclusion). I'm not sure if this behaviour is in fact desired - so far I know no other generic behaviour (gen_statem?) that does it. |
@max-au: When I wrote |
The In practice few libraries handle this correctly. |
If the behaviour were to be preserved, I think the correct way to do it would be via a tagged exit reason, much like how I prefer the approach of forcing implementations to write their own exception handling, though, and I think we actually use this in our code-base, not having known about the |
I think some tests should be added, that we simply keep this ancient behaviour behaviour, and document it. |
OK, so I was looking at what tests would be needed. For (Searching implementations to verify these modules indeed have this bug/feature was somewhat confusing as it looks like, as @mikpe suggests, it's basically down to use of |
A few callbacks that uses Handlers that do not use |
Mmm, apologies, I conflated two things there -- the "default throw" shape made just grepping for "throw" inaccurate, but clearly isn't the original source. The |
@bucko909: From what I remember, catch throw(4711). % -> 4711.
catch exit(4711). % -> {'EXIT', 4711}.
catch error(4711). % -> {'EXIT', {4711, Stacktrace}} So When using this, unwrapping the EXIT tuple, to detect When then |
I've pushed proposed tests for Have rebased as the branch is oooooolde. |
I've rebased this change onto master, removing the documentation changes -- it seems like the documentation already included a phrase equivalent to this after it was refactored to use I still await advice on if the testing is sufficient, and hence should be added to the other two modules. |
(Looks like I may have caused test failures with the rebase? I'll have to check those out, I guess -- but would still appreciate feedback on style beforehand.) |
I think the test code looks good enough. The first GitHub action log mentions our HowTos for DEVELOPMENT and TESTING. Please check them out for how to run in this case only stdlib gen_server_SUITE locally, to debug the PR test code... |
This is a proposed documentation amendment to cover the undocumented behaviour whereby
gen_server:try_dispatch
will catchthrow
and treat the reason as the function's return value (used inglobal_group
for example).