Limiting CPU/memory usage of `IO` action?

Hey,

Is there a way to limit resource usage for a particular action? Let’s say I have a typed expression compiled from user’s input and a way to evaluate it: eval :: Expr a -> IO a.

Is there a way to somehow cap the memory usage of eval? I want to somehow prevent users from crashing entire backend by having them input an expression that uses a lot of memory (like calculating a very big number, for instance). So something like timeout, but for memory usage.

3 Likes

You can use enableAllocationLimit from System.Mem to limit the amount of memory allocated for the current thread. This doesn’t directly constrain the maximum residency, though; it’s more like a limit on the amount of work the program does (because Haskell programs usually allocate a lot, much of which is more or less immediately garbage).

If you need to limit the amount of live data present on the heap, I don’t think there’s an easy way to do that with the RTS. You could launch a separate process for each request, which would give access to various OS-level features to control the resources used by each process, but of course that comes at significant overhead.

4 Likes

This is probably clear to you already, but just to make sure, another option is to explicitly carry the amount of fuel you can burn and decrement it at each operation, until you hit 0, which is when you bail out. This doesn’t limit allocations, but can be a good enough proxy.

2 Likes