From error-handling to structured concurrency
4 days ago
- #error-handling
- #concurrency
- #structured-concurrency
- Error-handling in concurrent programs lacks a single stack for propagation, unlike single-threaded programs.
- Two common approaches for unhandled errors in concurrent programs: terminate the thread and continue, or terminate the entire program.
- Python's asyncio offers a third way by delivering exceptions to tasks waiting on the failed task, but risks swallowing exceptions if not handled.
- Proposal: enforce waiting on every spawned task to handle exceptions, using context managers to ensure cleanup.
- Challenges include deadlocks and resource leaks when tasks depend on each other or fail unexpectedly.
- Solution: implement a cancellation mechanism to promptly respond to errors in any task, ensuring cleanup and preventing deadlocks.
- Structured concurrency introduces a tree of tasks with nested lifetimes and automatic cancellation, improving error handling and resource management.
- Structured concurrency frameworks like Python's asyncio TaskGroup, trio's nursery, and Go's errgroup package embody these principles.
- Error-handling in concurrent programs is crucial not just for robustness but also for a smoother development experience, reducing frustration from deadlocks and swallowed exceptions.