Skip to content

Redundant process spawning when watching #2674

@BHznJNs

Description

@BHznJNs

Description

I tried to use task to watch my FastAPI project.

The task script is like (since this project can not automatically kill the process, I used cleanup_port.py to do that):

  dev:
    deps: [install-dev]
    watch: true
    sources:
      - src/**
      - entry.py
    cmds:
      - uv run python scripts/cleanup_port.py 1460
      - uv run python entry.py

My entry.py points to my main.py, and this is how it look like:

# main.py
import argparse
import uvicorn
from loguru import logger
from .api import app

def main():
    parser = argparse.ArgumentParser()
    parser.add_argument("--port", type=int, default=1460)
    args = parser.parse_args()

    logger.info("Starting server on port {}", args.port)
    uvicorn.run(app, host="127.0.0.1", port=args.port) # no hot reload here

And here is the logs:

> task dev
task: Started watching for tasks: dev
task: Task "install-dev" is up to date
task: [dev] uv run python scripts/cleanup_port.py 1460
2026-02-02 23:54:38.913 | INFO     | __main__:kill_port_process:48 - Port 1460 is free, no need to clean
task: [dev] uv run python entry.py
INFO  [alembic.runtime.migration] Context impl SQLiteImpl.
INFO  [alembic.runtime.migration] Will assume non-transactional DDL.
2026-02-02 23:54:42.953 | INFO     | src.main:main:13 - Starting server on port 1460
INFO:     Started server process [19260]
INFO:     Waiting for application startup.
INFO:     Application startup complete.
INFO:     Uvicorn running on http://127.0.0.1:1460 (Press CTRL+C to quit) # I changed file here
task: Failed to run task "dev": exit status 1
task: Task "install-dev" is up to date
task: [dev] uv run python scripts/cleanup_port.py 1460
2026-02-02 23:55:02.488 | WARNING  | __main__:try_kill_process:11 - Killing process 19260 (python.exe)...
2026-02-02 23:55:02.514 | SUCCESS  | __main__:kill_port_process:43 - Process 19260 killed successfully
task: [dev] uv run python entry.py
task: Failed to run task "dev": exit status 1
task: Task "install-dev" is up to date
task: Task "install-dev" is up to date
task: [dev] uv run python scripts/cleanup_port.py 1460
task: [dev] uv run python scripts/cleanup_port.py 1460
2026-02-02 23:55:05.070 | INFO     | __main__:kill_port_process:48 - Port 1460 is free, no need to clean
2026-02-02 23:55:05.104 | INFO     | __main__:kill_port_process:48 - Port 1460 is free, no need to clean
task: [dev] uv run python entry.py
task: [dev] uv run python entry.py
INFO  [alembic.runtime.migration] Context impl SQLiteImpl.
INFO  [alembic.runtime.migration] Will assume non-transactional DDL.
2026-02-02 23:55:07.277 | INFO     | src.main:main:13 - Starting server on port 1460
INFO:     Started server process [16408]
INFO:     Waiting for application startup.
INFO:     Application startup complete.
INFO:     Uvicorn running on http://127.0.0.1:1460 (Press CTRL+C to quit)
INFO  [alembic.runtime.migration] Context impl SQLiteImpl.
INFO  [alembic.runtime.migration] Will assume non-transactional DDL.
2026-02-02 23:55:11.145 | INFO     | src.main:main:13 - Starting server on port 1460
INFO:     Started server process [20536]
INFO:     Waiting for application startup.
INFO:     Application startup complete.
ERROR:    [Errno 10048] error while attempting to bind on address ('127.0.0.1', 1460): [winerror 10048] only one usage of each socket address (protocol/network address/port) is normally permitted
INFO:     Waiting for application shutdown.
INFO:     Application shutdown complete.
INFO  [alembic.runtime.migration] Context impl SQLiteImpl.
INFO  [alembic.runtime.migration] Will assume non-transactional DDL.
2026-02-02 23:55:11.688 | INFO     | src.main:main:13 - Starting server on port 1460
INFO:     Started server process [25608]
INFO:     Waiting for application startup.
INFO:     Application startup complete.
ERROR:    [Errno 10048] error while attempting to bind on address ('127.0.0.1', 1460): [winerror 10048] only one usage of each socket address (protocol/network address/port) is normally permitted
INFO:     Waiting for application shutdown.
INFO:     Application shutdown complete.
task: Failed to run task "dev": exit status 1
task: Failed to run task "dev": exit status 1
task: Signal received: "interrupt"

Version

3.48.0

Operating system

Windows 11 25H2

Experiments Enabled

No response

Example Taskfile

version: '3'

tasks:
  install-dev:
    sources:
      - pyproject.toml
    generates:
      - .venv/**
    cmds:
      - uv sync --group dev

  dev:
    deps: [install-dev]
    watch: true
    sources:
      - src/**
      - entry.py
    cmds:
      - uv run python scripts/cleanup_port.py 1460
      - uv run python entry.py

Metadata

Metadata

Assignees

No one assigned

    Labels

    No fields configured for support.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions