Background Task Timeout Limits for Large File Uploads

Hi Anvil Community,

I’m working on a scheduled background task for a video upload feature. The task processes large video files by splitting them into chunks and uploading them sequentially via a platform API. However, I’m encountering the following error during execution:

anvil.server.TimeoutError: Server code took too long

My Setup:

  • A scheduled background task that uploads video chunks in a loop
  • Each chunk is uploaded via a separate HTTP request to an external platform API

Questions:

  1. What are the exact timeout limits for background tasks across different Anvil plans?
  2. Is it possible to increase the background task timeout, or is it a fixed/hard limit?
  3. Do scheduled background tasks have the same timeout restrictions as user-triggered background tasks?

Thanks in advance for your help!

I’ve never seen that error message about a background task, but I have seen it about a server call.

Are you calling a function that starts a background task and waits for it to be done?

@stefano.menci Its a scheduled task that automatically gets triggered based on the scheduler. All I am doing is invoking a function and making API calls in the background function.

Below is the sample high level overview of the code

@anvil.server.background_task  
def long_upload_task():
    for i in range(chunk_length):  
        requests.post(api_url, data=chunks[i])  #

I am also not sure why I am getting the timeout error as I am not invoking anything from client side neither running any function with decorator “@anvil.server.callable

Where do you see the error message, in the app logs?

Yes. Below is teh screenshot from the app session log

With my plan background tasks don’t have a time limit.
I don’t know what plan you are in and whether there are time limits on different plans.

I am on a business plan.

@stefano.menci I ran a test with just time.sleep(5) in a loop for 100 iterations - this completed successfully in 10 minutes without timeout error. However, my upload task with external HTTP requests times out in few minutes.

This suggests Anvil may have separate timeout limits for:

  • Background task execution time (10+ minutes)
  • External HTTP requests from background tasks or maximum API requests it can make continuously. For my API requests, I have a timeout set to 300 seconds for each API call

Does Anvil has a different timeout policies for external requests vs internal processing in background tasks?

I forgot to update the post but I finally figured out why the background task was hitting a server timeout—even though Anvil states that background tasks aren’t subject to server timeouts. It turned out to be a surprisingly tricky issue: I had left a breakpoint in the code for debugging, and whenever Anvil hits a breakpoint, it imposes a limit on how long it can maintain the server connection. Once I removed all the breakpoints, it ran successfully even for a long running job. Took a while to crack that one!

2 Likes

Been there, done that :frowning:

It wasn’t the main reason, but one of the reasons for this FR, now addressed.

And if I remember, breakpoints in the background tasks only affect execution from the IDE, not from production?
If that is the case, then you didn’t really have the problem?
That would be fun!