📝 Add exception handling guidance for BackgroundTasks by tsayin4 · Pull Request #14599 · fastapi/fastapi · GitHub
Skip to content

📝 Add exception handling guidance for BackgroundTasks - #14599

Closed
tsayin4 wants to merge 2 commits into
fastapi:masterfrom
tsayin4:docs-backgroundtasks-exception-handling
Closed

tsayin4 wants to merge 2 commits into
fastapi:masterfrom
tsayin4:docs-backgroundtasks-exception-handling

Conversation

@tsayin4

@tsayin4 tsayin4 commented Dec 24, 2025

Copy link
Copy Markdown

Summary

Adds exception handling guidance to the BackgroundTasks tutorial to help users avoid silent failures in production.

Context

Several users have reported confusion around exception handling in BackgroundTasks:

Maintainers have previously noted that this behavior should be more explicit in the documentation.

Motivation

While building production systems, I explored async exception visibility patterns and noticed the same confusion affects FastAPI's BackgroundTasks. Exceptions in background tasks don't propagate to the response handler, which can lead to silent failures that are difficult to debug.

Changes

Adds a focused section to the BackgroundTasks tutorial explaining:

  • How exceptions behave in background tasks (not propagated to the request handler)
  • An example showing a silent failure scenario
  • An example showing proper error handling with logging
  • A pointer to task queue libraries for more complex production use cases

Scope

  • Single documentation section (~50 lines)
  • No API changes
  • Located immediately after the basic BackgroundTasks examples
  • Minimal and focused on production visibility

Happy to adjust wording or move this to a more appropriate section if you think that fits better.

@github-actions github-actions Bot added the docs Documentation about how to use FastAPI label Dec 24, 2025
@github-actions

Copy link
Copy Markdown
Contributor

@codspeed

codspeed Bot commented Dec 24, 2025 •

Copy link
Copy Markdown

CodSpeed Performance Report

Merging #14599 will not alter performance

Comparing tsayin4:docs-backgroundtasks-exception-handling (cdf6693) with master (7203e86)1

Summary

✅ 40 untouched

Footnotes

  1. No successful run was found on master (2b212dd) during the generation of this report, so 7203e86 was used instead as the comparison base. There might be some changes unrelated to this pull request in this report. ↩

@YuriiMotov YuriiMotov left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tsayin4, thank you for your interest!

This is a good idea to add such clarification and code example, but current implementation should be improved:

  • Code examples should be placed in separate .py-files and covered by tests
  • I think code examples should be updated:
    • catch specific exceptions instead of except Exception (that's considered as bad practice)
    • code example should be more meaningful - just catching the exception and logging it doesn't really change a lot (error traceback would be printed even without this logic)
  • I would also add console output for each example and some clarifications

Would you like to continue working on this?

Comment on lines +95 to +101
!!! tip
For production systems with complex background jobs, consider:

- [Celery](https://docs.celeryq.dev) - Distributed task queue
- [arq](https://arq-docs.helpmanual.io/) - Async task queue for Python
- [Dramatiq](https://dramatiq.io/) - Fast and reliable task processing

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
!!! tip
For production systems with complex background jobs, consider:
- [Celery](https://docs.celeryq.dev) - Distributed task queue
- [arq](https://arq-docs.helpmanual.io/) - Async task queue for Python
- [Dramatiq](https://dramatiq.io/) - Fast and reliable task processing
/// tip
For production systems with complex background jobs, consider:
- [Celery](https://docs.celeryq.dev) - Distributed task queue
- [arq](https://arq-docs.helpmanual.io/) - Async task queue for Python
- [Dramatiq](https://dramatiq.io/) - Fast and reliable task processing
///

@alejsdev alejsdev changed the title docs: Add exception handling guidance for BackgroundTasks 📝 Add exception handling guidance for BackgroundTasks Feb 10, 2026
@github-actions

Copy link
Copy Markdown
Contributor

Heads-up: this will be closed in 3 days unless there's new activity.

@github-actions

Copy link
Copy Markdown
Contributor

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

docs Documentation about how to use FastAPI waiting

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants