Skip to content

Task timeout

In a software application, it can happen that scheduled tasks are executed but never finish. This can occur for several reasons:

  • The task contains an infinite loop
  • The task simply takes too long to execute
  • The task is waiting indefinitely for an external service (e.g., API call, database, or network request)
  • A deadlock occurs (e.g., waiting on a locked resource)
  • Resource exhaustion (e.g., CPU, memory) prevents the task from progressing

To handle such situations, lilota provides a mechanism to deal with tasks that never complete by stopping the corresponding worker process. When this happens, the task is terminated and its status is set to expired.

In lilota, it is recommended to specify a timeout for each task.

The timeout can be set when registering a task.

worker = LilotaWorker(
  ...
)

@worker.task("my_task", timeout=timedelta(minutes=5))

The default timeout is 5 minutes. This can be adjusted using Python’s timedelta object. If None is specified, the timeout is disabled.

The worker process that was stopped is replaced by a new one, so the tasks that are waiting are still executed. The expired task is retried if it has attempts left. See Retry mechanism.

A task that ends at the moment it expires

A task that ends just when its timeout is over is either completed or expired, never both. If the task was set to expired first, its result is not stored, and the task is retried if it has attempts left. The worker writes a warning to the lilota_log table:

The result of the task was not stored, because the task had ended before: it has run into its timeout, or it was deleted (id: 0d3c...)

The timeout is checked by another worker than the one that executes the task. If your workers run on several machines, their clocks have to be in sync. See Clocks on several machines.