-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Unexpected behaviour of the join() method of CompletableJob #1527
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
Comments
No, the described behavior was documented
Please read carefully the official documentation |
This behaviour is indeed documented. Throwing an arbitrary exception from the Let's put coroutines aside and fallback to regular Java threads:
You do not expect In general,
It is not and it's stated in the documentation. Moreover, that's why |
But, why can't we implement the join() of CompletableJob behaves similar to that of the join() of a launch job, i.e. throwing CancellationException. This is more consistent and would allow us to emulate the behaviour of a launch job using CompletableJob. In the current implementation, the coroutine waiting at join() detects nothing, just resumes quietly. Also, what is the purpose of providing the completeExceptionally(exception: Throwable) method in the CompletableJob interface? It is documented as "This function transitions this job into cancelled state if it was not completed or cancelled yet.". Clearly it means that when completeExceptionally() is called, the state of this CompletableJob becomes cancelled, and this implies that the coroutine waiting at the join() would receive a CancellationException. |
I agree that join() should throw CancellationException not IllegalStateException, but it should not be just silent as in the current implementation. |
Note that
I think we can improve the documentation here and clarify in the documentation that the owner of the job is responsible for handling the exception that he passes to
It does not imply it.
|
Thanks for your clarification. I think I have misunderstood the |
…sts to ensure its contract Addresses #1527
…sts to ensure its contract Addresses #1527
I happen to find that the behaviour of the join() method of CompletableJob returned by Job() is not what I would expect. Consider the following code:
I would expect the
IllegalStateException
would be caught, and "bar" and the stack trace would be printed. But, it turns out the "foo" is printed. This doesn't seem right to me because if I replace the use of Job() by CompletableDeferred() as in the following example, calling await() throws the exception.If join() of Job is considered as the counterpart of await() of Deferred, then their behaviour should be consistent.
If I change my first example to use the job object returned by launch() as in the following example, the behaviour is consistent with what I would expect, i.e. the exception is caught:
This leads me to the conclusion that the behaviour of the join() method of CompletableJob is incorrectly implemented. It is inconsistent with other similar or equivalent cases.
The text was updated successfully, but these errors were encountered: