when running code with `withTimeout` is it possibl...
# coroutines
c
when running code with
withTimeout
is it possible to get a stacktrace of the thread where the timeout occurred when it occurred?
s
If you catch
TimeoutCancellationException
you should get a stacktrace but I don't know if it'll be a very useful one
Not sure if the stacktrace gets filled in at the point when the coroutine is cancelled or at the next point when a cancellable function is called
I guess that's the problem...
Have you done any experimenting? Interested to know what led you to ask the question
c
i have a test runner and when a test times out it would be nice to see at what position in the test the execution was
i can start my own timeout timer, then
Thread.getAllStackTraces()
and find the correct thread
the stacktrace that withTimeout throws has the line number of the
withTimeout
line
and the cause is inside coroutine code:
Copy code
Caused by: kotlinx.coroutines.TimeoutCancellationException: Timed out waiting for 40000 ms
	at kotlinx.coroutines.TimeoutKt.TimeoutCancellationException(Timeout.kt:184)
	at kotlinx.coroutines.TimeoutCoroutine.run(Timeout.kt:154)
👍 1
s
Makes sense
I don't think it would be possible to do that using
withTimeout
, it doesn't provide any control over the exception that gets thrown
The docs also say
The timeout event in withTimeout is asynchronous with respect to the code running in its block and may happen at any time, even right before the return from inside of the timeout block.
So they're kind of implying that there isn't necessarily a "current thread"
which makes sense because there could be multiple things running in parallel that all get timed out
c
yeah. in my case i could just walk all thread stack traces and find the test because i know where it is
I guess i will just look at how withTimeout works
j
For what it's worth, inside
withTimeout
's body you should have a
CancellationException
with the correct stacktrace if I remember correctly
c
i think what i want is a callback that is called to create the CancellationException. then i can walk all thread stacktraces and find the correct one (or more than one) and attach them to my exception