Configuring System.Timeout.timeout for masked asynchronous exceptions
29.5K reputation · 24 Aug 2020, 18:51 UTC
The goal is to ensure that System.Timeout.timeout can cancel an action even when the action masks asynchronous exceptions.
The current implementation relies on throwing ThreadKilled via throwTo to the worker thread; if the action uses mask or uninterruptibleMask, the exception is delayed until masking ends, allowing the action to exceed the time limit.
An open design question is whether the library should instead use a cooperative cancellation mechanism, such as an STM TVar, to guarantee timely interruption regardless of masking state.
What are the implications for existing code that depends on exception masking for resource safety? Would a cooperative approach introduce overhead or latency? Should the timeout function provide an option to choose between exception‑based and cooperative cancellation?