Repository navigation
Description
Describe the bug
When a FutureCall task is picked up for execution, it is immediately removed from the database. If the server crashes or is terminated before completing the task, the task is lost and never retried.
To reproduce
- Schedule a future call.
- Let the server pick it up for execution.
- Forcefully shut down the server before the task is completed.
- Observe that the task is not executed and does not exist in the database anymore.
Expected behavior
Tasks should only be removed from the database once they have completed successfully. If the server crashes mid-execution, the task should be retained and retried later.
Library version
Serverpod version: latest main branch
Platform information
Affects all deployment targets with future call scheduling.
Additional context
A proposed solution:
When a server picks up a future call, it should update the execution time in the database with a retry offset, not delete the task.The retry offset should be configurable for each future call type (perhaps by adding a new property to the FutureCall class).The task should only be removed from the database after successful completion.If a server crashes mid-execution, the task remains and will be picked up again by another server at the new execution time.
This ensures fault tolerance in distributed environments and prevents task loss.
Edit: New proposal after feedback:
Proposed solution:
- When a server picks up a future call, it does not immediately remove it from the database.
- Instead, when execution is about to begin, the server attempts to delete the future call record as a signal that it is claiming execution.
- If the deletion succeeds, the task proceeds with execution.
- If the deletion fails (e.g., another server already deleted the row), the task is not executed.
This guarantees that only one server executes the task, and that it is only removed from the database if execution is guaranteed to begin.
How experienced are you with this library?
Expert - Experienced and comfortable with using this library in complex projects
Are you interested in working on a PR for this?
- I want to work on this
Metadata
Metadata
Assignees
Labels
Type
Projects
- StatusShow more project fieldsDone 🚀
Activity
inf0rmatix commentedon Apr 14, 2025
I propose changing this slightly to only guarantee that a future call has been invoked. Anything else should be left to the user to implement according to their business logic needs.
Using a delete statements are atomic and right before invoking the actual future call, trying to delete the entry ensures it has not been run already.
SandPod commentedon Apr 15, 2025
@inf0rmatix - I think your reasoning makes sense and will greatly simplify the mental model and allow users to put that guarantee in place if needed.
Will update the issue accordingly.
craiglabenz commentedon Jan 23, 2026
It's not clear to me how the proposed solution would alleviate the edge case of a poorly timed and unexpected server shutdown potentially dropping a task on the floor. As I read this proposed solution, I think it would correctly guarantee that a task is executed at most once, which includes zero executions.
I believe a cleaner solution would be to ensure that tasks are executed AT LEAST ONCE by introducing a second table (
inflight_tasks, or similar) with a unique constraint on thetaskIdcolumn. Then, instead of attempting to delete the task from the main tasks table, a server hoping to execute said task would attempt to write a new row to that table. If the write succeeds, that server has successfully claimed the task and can begin executing it.The initial insert would set a timestamp column to the execution start time, and then upon completion, would clean everything up. But, critically, in the case of a shutdown, that
inflight_tasksrecord would be left behind and after a specified TTL has elapsed, could be considered eligible for retry.Of course, this creates the possibility of a task being invoked more than once; but it is mathematically impossible to guarantee exactly once execution in any distributed system (see Two Generals problem).
However, developers can get around this themselves by writing their tasks in an idempotent way; aka; performing everything within a transaction and first checking for completion of their own work.
craiglabenz commentedon Jan 23, 2026
Relatedly, the TTL configuration should be task-specific. Sending a push notification likely either succeeds or fails within a few hundred milliseconds; whereas uploading large datasets for internal processing may take several minutes.
inf0rmatix commentedon Jan 23, 2026
@craiglabenz that's a very good point! initially, I did not consider what would happen if there were more than one instance of the server connected.
I second your suggestion of an "inflight_tasks" concept. Not deleting the scheduled future call row immediately would break the security of making sure it would not be run more than once.