Timeout function

No. Unfortunately a “good timeout function” is not supported in julia. E.g.

julia> @time timeout(5) do 
       rand(Int, 1<<12, 1<<12)*rand(Int, 1<<12, 1<<12); end
 28.297039 seconds (16.13 k allocations: 384.802 MiB, 0.09% gc time, 0.05% compilation time)

The issue is you would need to preempt your f() and throw the exception. However, if programs need to assume that it can be preempted at any time and an exception inserted, then it becomes very hard to reason about its behavior, both for humans and for the optimizing compiler.

Instead, ask the operating system for support.

For example, run a separate julia process and just kill the process. Since you want to recursively kill all child processes as well, consider cgroups. Or start a docker-container / spin up a VM, and nuke that from orbit (“only way to be sure”).

6 Likes