[0] https://www.schneems.com/2017/02/21/the-oldest-bug-in-ruby-w...
[1] https://github.com/zombocom/rack-timeout (c.f. term_on_timeout)
The two big benefits are (1) if you pass the cancellation token to the task that might be canceled, hopefully it will be obvious that the task might be canceled; and (2) the task that might be canceled has a way to find out it’s being canceled and, if that ever happens, it can clean up.
- Run a subprocess such as "rg" and terminate early if necessary
- Run a SQLite SQL a query that errors if it takes more than a second
- Same but for other DBs - MySQL, PostgreSQL, Elastic, Mongo etc
- Execute a fragment of JavaScript in something like QuickJS with a time limit
- Brute force some kind of algorithm with a time limit - using a library I did not write myself
When said software is closed source and doesn't have that feature then that company sells a problem and not a solution.
If you can't trust the process, fork, wait in the parent, and kill the child if it hasn't terminated.
I don't think alarm() is exposed in Ruby, unless it's been added recently, but it's easy enough to add. However since fork + kill is more reliable anyway, it's often a better choice.
Timeout is for threads not processes, and not everything that would be done on a thread can be conveniently rewritten to be a separate process.
Say you're looping over 1m items, maybe every 1k you check to see if you're out of time and return or raise