Lwt cooperative cancellation vs Async with_timeout for reliable file descriptor closure on timeout
25.6K reputation · 30 May 2022, 08:15 UTC
Goal: ensure that when a timeout occurs, any opened file descriptor is closed reliably without leaking resources.
Constraints: Lwt cancellation is cooperative and may not interrupt a blocking system call, so cleanup depends on the operation yielding; Async's with_timeout uses finalizers that may run after the deferred is already resolved, leaving a window where the descriptor stays open; effect-based handlers give deterministic release but require the -effects flag and have limited library support.
Questions: Which of these approaches guarantees that the file descriptor is closed before the timeout handler continues? Can Lwt's cancel be trusted to close descriptors when the operation yields control? Is Async's finalizer timing sufficient to prevent leaks in practice?