Hi @valentin.vieriu, I think that’s exactly the issue you’re experiencing here: each individual request is using more than 50ms CPU time, and eventually the CPU time limiter kicks in for that worker. The CPU time consumption could theoretically be caused by memory pressure (the GC at work), but in this case I think it’s just the raw image resize computation that is too much. The reason I believe that is because there doesn’t appear to be any memory leak in your worker – no global data structures grow per-request, and WASM just uses the memory you give it.
The earliest description of the problem I can find is Kenton’s, here (last paragraph): Long running WebCrypto API? - #18 by KentonVarda
While the problem is not directly related to the latest release, we do include a fix in the latest release, but only for the preview service:
Fixed a bug in the preview service where the CPU time limiter was overly lenient for the first several requests handled by a newly-started worker. The same bug actually exists in production as well, but we are much more cautious about fixing it there, since doing so might break live sites. If you find your worker now exceeds CPU time limits in preview, then it is likely exceeding time limits in production as well, but only appearing to work because the limits are too lenient for the first few requests. Such workers will eventually fail in production, too (and always have), so it is best to fix the problem in preview before deploying.
That change should be live now, so if you could try your script again in the preview, our expectation is you’ll see an 1102 error much earlier, which should, in theory, help you optimize the script. The behavior of the deployed script will remain the same for now, however.
I say “in theory help you optimize the script”, because in this case I’m not sure how much more optimal you can make image resizing – it is inherently CPU-intensive, and its time complexity is a function of the input size, which you may or may not control. Metered CPU billing (on our roadmap) will eventually provide an option here; another option you could use today is our Image Resizing feature, though that does require a BIZ zone.
I agree 100%. We’re acutely aware of this, and we’re working on providing those tools.
Harris