# Fix portable Start.bat — OSError(9) / lost sys.stderr ### Root cause 1. **User failure (`OSError(9, 'Неверный дескриптор')`, `lost sys.stderr`)** `run_server.py` called `os.dup2(log.fileno(), 1/2)` while the process was started with `start /b`. The inherited Windows stdio HANDLEs can already be invalid; `dup2` fixes CRT fds but Python/CPython still write through the old `sys.stderr` object / Windows handle → `OSError(9)` and `lost sys.stderr`. Uvicorn then dies → `startup.log` shows `TIMEOUT` / `health timeout`. 2. **Automation hang** (reproduced in smoke): a child started with `start /b` or `os.spawnv` inherits the BAT’s stdout **pipe**; parent `cmd /c` waits for EOF until the server exits. `os.spawnv` also does not quote spaces in paths (`финал 179…\WorkVolumeTracker` broke argv). ### Fix - **No `os.dup2`** in the server process. - `Start.bat` → `python scripts\start_server.py` (synchronous, short-lived). - `start_server.py` spawns `run_server.py` with `subprocess.Popen` **list argv** (quotes spaces/Cyrillic) + `DETACHED_PROCESS | CREATE_NEW_PROCESS_GROUP`, `stdin=NUL`, `stdout/stderr=wvt.log` file handles. - `run_server.py` only replaces Python-level `sys.stdout/stderr` (never OS fds); writes `wvt.pid`; full traceback to `wvt-crash.log`. - `wait_health.py` uses raw sockets (no urllib). - `Start.bat`: `startup.log` STEPs, diagnostics (`diagnose_start.py`) on timeout, `WVT_NO_PAUSE` / `WVT_NO_BROWSER` for automation; `pause` on errors in interactive use. ### Changed files `packaging/Start.bat`, `packaging/scripts/run_server.py`, `packaging/scripts/start_server.py`, `packaging/scripts/wait_health.py`, `packaging/scripts/diagnose_start.py` (already present), `packaging/build_portable.py` (ships scripts, BAT as UTF-8 BOM+CRLF). Host repo user data, legacy dirs, QA, POST-E2E/BUG-UI, KS-6a — untouched. Staging empty. No commit/push. ### New ZIP **`C:\MVP-project\_portable_build\WorkVolumeTracker-portable-win11-x64.zip`** 21 853 833 bytes · 2026-10-20 ### Tests (clean extract, path with spaces + Cyrillic) | Check | Result | |---|---| | Start.bat rc=0 (~5–12 s) | PASS | | `/health` 200, `/projects/` 200 | PASS | | `wvt.log` has `Uvicorn running` | PASS | | `wvt.log` **no** `OSError` / `lost sys.stderr` | PASS | | `wvt-crash.log` missing (no crash) | PASS | | Stop.bat, port closed | PASS | | **Second** Start.bat after stop | PASS | | `startup.log` STEPs + READY | PASS | | Missing `python.exe` / busy port (earlier) | PASS | ### Notes - `save_pid.py` may still print `PID_NOT_FOUND`; `run_server.py` writes `data\wvt.pid` (used by Stop). - Browser open uses `start "" /b` and can be skipped via `WVT_NO_BROWSER=1`.