Skip to content

[BUG] Repeated background task has no lifecycle handle/cancel path (orphan task risk) #371

@Binilkks

Description

@Binilkks

Describe the bug
The repeat_every scheduler starts a background task but does not keep or expose the created task handle.
Because of this, consumers cannot explicitly cancel/inspect the repeating task during shutdown/reload, which can cause lifecycle-management issues in long-running apps.

No direct traceback is produced in normal cases because this is primarily a lifecycle/control bug, not an immediate exception bug.

To Reproduce
Create a FastAPI app and define a startup task using repeat_every.
Use a short interval (for example 1 second) and log each execution.
Start the app, then trigger shutdown/reload (or restart worker process).
Observe that task lifecycle is not explicitly controllable by library API (no returned handle to cancel/manage).
Example app:
`from fastapi import FastAPI
from fastapi_utils.tasks import repeat_every
import logging

app = FastAPI()
log = logging.getLogger("demo")

@app.on_event("startup")
@repeat_every(seconds=1)
def periodic_job() -> None:
log.warning("periodic job tick")`

Expected behavior
repeat_every should provide a lifecycle-safe path to track and cancel the created background task (directly or via documented shutdown hook support), so periodic jobs can be cleanly managed during app shutdown/reload.

Screenshots
If applicable, add screenshots to help explain your problem.

Environment:

  • OS: [macOS]
  • FastAPI Utils version: 0.8.0
import fastapi_utils
import fastapi
import pydantic.utils
print(fastapi_utils.__version__)
print(fastapi.__version__)
print(pydantic.utils.version_info())
  • Python version, get it with:
python --version

Additional context
line 114 currently uses asyncio.ensure_future(loop()) without preserving task handle.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions