-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Coroutines, exception handling and withTimeout. Can't wrap my head around this combination #1914
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
A user in StackOverflow reports:
|
Thanks. Something strange is going on when timeout exception is caught inside the |
Hi @elizarov , thanks for your response. Could you please clarify what would be the expected behavior here for the exceptions? If they are caught inside the |
Hi, @elizarov I did some tracking. It seems to be much a design problem (maybe not a problem at all) than a bug as I could see. Actually,
If we modify the code above as below, everything becomes obvious:
Since Thread.sleep(1000) won't suspend, it will always print withTimeout done The detail why the origin code
won't (always) suspend is:
It seems we could fix the documentation or adjust the design (maybe this is not necessary or proper as I could see). |
The reasoning is indeed perfectly right, so I'm closing this as "works as intended". |
For the future self: the difference in code paths becomes quite clear after the following modification:
|
This gives me the output:
On the first loop, withTimeout done gets printed.
Question 1: Why only on the first iteration?
Question 2: If I change the wait time in withTimeout from 1L to 10L, it does not get printed. Instead, the exception is propagated to the outer try/catch, like on the next iterations (See below). Why?
Question 3: Why, even though the exception is being caught inside a try/catch block in withTimeout(), it is being propagated to the outer try/catch block?
I appreciate and thank you in advance for your help.
The text was updated successfully, but these errors were encountered: