---
title: lifespan | CodeWeaver Docs
description: API reference for codeweaver.server.lifespan
url: "https://docs.knitli.com/api/server/lifespan"
type: static
generatedAt: "2026-04-17T17:21:09.816Z"
---

# lifespan
       [Open in ChatGPT](https://chatgpt.com/?q=Read%20https%3A%2F%2Fdocs.knitli.com%2Fcodeweaver%2Fapi%2Fserver%2Flifespan%2F.%20I%20want%20to%20ask%20questions%20about%20it.)[Open in Claude](https://claude.ai/new?q=Read%20https%3A%2F%2Fdocs.knitli.com%2Fcodeweaver%2Fapi%2Fserver%2Flifespan%2F.%20I%20want%20to%20ask%20questions%20about%20it.)[View in Markdown](/codeweaver/api/server/lifespan.md)       [Share on LinkedIn](https://www.linkedin.com/sharing/share-offsite/?url=https%3A%2F%2Fdocs.knitli.com%2Fcodeweaver%2Fapi%2Fserver%2Flifespan%2F)[Share on X](https://x.com/intent/tweet?url=https%3A%2F%2Fdocs.knitli.com%2Fcodeweaver%2Fapi%2Fserver%2Flifespan%2F&text=lifespan)[Share on Threads](https://threads.net/intent/post?url=https%3A%2F%2Fdocs.knitli.com%2Fcodeweaver%2Fapi%2Fserver%2Flifespan%2F&text=lifespan)[Share on Bluesky](https://bsky.app/intent/compose?text=lifespan%20https%3A%2F%2Fdocs.knitli.com%2Fcodeweaver%2Fapi%2Fserver%2Flifespan%2F)[Share on Facebook](https://www.facebook.com/sharer/sharer.php?u=https%3A%2F%2Fdocs.knitli.com%2Fcodeweaver%2Fapi%2Fserver%2Flifespan%2F)[Share on Reddit](https://reddit.com/submit?url=https%3A%2F%2Fdocs.knitli.com%2Fcodeweaver%2Fapi%2Fserver%2Flifespan%2F&title=lifespan)[Share on Hacker News](https://news.ycombinator.com/submitlink?u=https%3A%2F%2Fdocs.knitli.com%2Fcodeweaver%2Fapi%2Fserver%2Flifespan%2F&t=lifespan)[Share on Email](mailto:?subject=lifespan&body=https%3A%2F%2Fdocs.knitli.com%2Fcodeweaver%2Fapi%2Fserver%2Flifespan%2F)[Share on WhatsApp](https://wa.me/?text=lifespan%20https%3A%2F%2Fdocs.knitli.com%2Fcodeweaver%2Fapi%2Fserver%2Flifespan%2F)[Share on Telegram](https://t.me/share/url?url=https%3A%2F%2Fdocs.knitli.com%2Fcodeweaver%2Fapi%2Fserver%2Flifespan%2F&text=lifespan)
# `codeweaver.server.lifespan`
[Section titled “codeweaver.server.lifespan”](#codeweaverserverlifespan)
Lifespan management for CodeWeaver background services and servers.

This module provides lifespan context managers for different deployment modes:

 - background_services_lifespan: Background services only (daemon mode)
 - http_lifespan: Background services + HTTP MCP server integration

## Function: `background_services_lifespan`
[Section titled “Function: background_services_lifespan”](#function-background_services_lifespan)

**

```
background_services_lifespan()
```

Lifespan context manager for background services only.

This manages the lifecycle of CodeWeaver’s background services without requiring an MCP server. Used for daemon mode (`codeweaver start`).

Manages:

 - Background indexing task
 - File watcher
 - Health monitoring
 - Statistics and telemetry

Args: settings: Configuration settings statistics: Session statistics instance progress_reporter: ProgressReporter for user-facing output (created if None) verbose: Enable verbose logging debug: Enable debug logging

Yields: CodeWeaverState instance for background services

## Function: `http_lifespan`
[Section titled “Function: http_lifespan”](#function-http_lifespan)

**

```
http_lifespan()
```

Lifespan context manager for HTTP MCP server with background services.

This manages both the MCP HTTP server lifecycle and background services together. Used when running `codeweaver server --transport streamable-http`.

Args: mcp_state: MCP HTTP server state containing FastMCP app and config settings: Configuration settings statistics: Session statistics instance progress_reporter: ProgressReporter for user-facing output (created if None) verbose: Enable verbose logging debug: Enable debug logging

Yields: CodeWeaverState instance for background services