Skip to content

Future calls are lost if server shuts down during execution #3417

Closed

Description

@SandPod

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

  1. Schedule a future call.
  2. Let the server pick it up for execution.
  3. Forcefully shut down the server before the task is completed.
  4. 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

Activity

  1. added
    area: future calls
    bug
    on Apr 14, 2025
  2. inf0rmatix commented on Apr 14, 2025

    @inf0rmatix
    Contributor

    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.

  3. SandPod commented on Apr 15, 2025

    @SandPod
    ContributorAuthor

    @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.

  4. moved this to Later 🥳 in Serverpod Frameworkson Jun 16, 2025
  5. moved this from Later 🥳 to Bugs 🐞 in Serverpod Frameworkson Jun 19, 2025
  6. moved this from Bugs 🐞 to Planned 🔭 in Serverpod Frameworkson Aug 22, 2025
  7. moved this from Planned 🔭 to Inbox 📫 in Serverpod Frameworkson Sep 22, 2025
  8. added
    stake: cloud
    on Sep 22, 2025
  9. moved this from Inbox 📫 to Planned 🔭 in Serverpod Frameworkson Sep 29, 2025
  10. moved this from Planned 🔭 to Backlog 🗂️ in Serverpod Frameworkson Oct 24, 2025
  11. craiglabenz commented on Jan 23, 2026

    @craiglabenz
    Contributor

    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 the taskId column. 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_tasks record would be left behind and after a specified TTL has elapsed, could be considered eligible for retry.

    This second table amounts to "leasing" the task out to a worker node for a fixed window of time. It ensures that the task is never run twice concurrently.

    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.

  12. craiglabenz commented on Jan 23, 2026

    @craiglabenz
    Contributor

    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.

  13. inf0rmatix commented on Jan 23, 2026

    @inf0rmatix
    Contributor

    @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.

  14. moved this from Backlog 🗂️ to Done 🚀 in Serverpod Frameworkson Mar 27, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area: future callsbugstake: cloud

    Type

    No type

    Projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions