testing: Panic propagation for test code #3217
Labels
A-tokio
Area: The main tokio crate
C-feature-request
Category: A feature request.
M-runtime
Module: tokio/runtime
Related issue: #2699.
Summary
The proposal aims to improve the testability of applications and libraries which spawn tasks internally by creating a new scheduler that doesn't capture panics.
Motivation
Tokio tasks mimic
std::thread
behavior by capturing panics and only propagating these when the associatedJoinHandle
is joined. The usage of OS-level threads and tokio tasks however differs in the sense that applications and libraries may liberally spawn the latter, whilst usually making a thread pool of the former.To make good tests of such libraries and applications we typically avoid threading at all to remove nondeterminism, however, with tokio this might not be possible. We need to allow tests to fail on panics in detached tasks to thoroughly test these libraries and apps.
Proposal
The proposed changes are:
In addition to copying the
basic_scheduler
code but changing the way spawn constructs aharness
, in this case, harnesses are constructed with an alternative vtable wherepoll
does not capture panics.Changes for the end-user of Tokio
This change is an additive feature put behind the
rt-test
flag. It does not impact existing tokio users in any way, nor does it impact performance by not adding any new branches or extra code to run for those who do not use the test scheduler (with the exception of a few matches in the runtime, but that should be negligible). The test scheduler yields aRuntime
object such that the API is unchanged.Risks
Risk of breaking API if this scheduler is removed.
Open questions
Running the test scheduler in different threads may nondeterministically cause a panic to propagate in the thread that runs a future that happens to panic. This makes panics appear in an unspecified thread. Is this desirable?
Can this cause UB due to the panic happening inside the vtable call? Are there invariants that must be upheld?
It's not yet known what other features we want to add to the test scheduler, or whether we want
#[tokio::test]
to use the test scheduler.The text was updated successfully, but these errors were encountered: