#6467 [Engine] 0.2.17 on Windows: the CPU picture-reader fallback dies with "Bad file descriptor" (the encoder log fd is read as a path)

open · @zhuojun1024 · 0 コメント · GitHub で見る

Server & APINVIDIA / CUDAModels & quantsWindows

本文

## Summary

On Windows, when the GPU picture reader does not start, the CPU fallback never starts: the server dies with `OSError: [Errno 9] Bad file descriptor` inside `subprocess`, before `pictures are read on the CPU` is printed. The fallback is effectively dead on 0.2.17 — a reader that cannot start now takes the whole server with it instead of degrading to the CPU.

It is not the reader. The encoder log file returned by `serve/appendlog.py` has the **file descriptor number** as its `.name`, and `vision_log_tail()` (`serve/server_parts/engines.py:595`) does `open(name, "rb")` with it — Python reads an `int` argument as a file descriptor, wraps the *same* CRT fd and closes it when the block ends. The next `Popen(stderr=log)` then fails on `msvcrt.get_osfhandle(stderr.fileno())`.

## Environment

- Windows 11 10.0.26200, i5-14600K, 63.7 GiB RAM, driver 616.92
- Engine 0.1.63 from 0.2.17 sources (CUDA 13.3), server run directly: `python terminal/serve/server.py --engine strata --config <cfg> --port 3489`
- Cards: RTX 5060 Ti (sm_120) x2 as the engine pair, Tesla T10 (sm_75) as the vision card (`vision.cuda_device: 1`)
- The GPU reader cannot start here for a legitimate reason: `strata-vision.exe` built with sm_120 only, reading on the sm_75 T10

## Reproduction (15 lines, no model needed)

```python
import sys; sys.path.insert(0, "terminal")
from serve import appendlog
from serve.server_parts.engines import vision_log_size, vision_log_tail
import msvcrt

log = appendlog.open_append("serve.log", "utf-8")   # Windows: fdopen(fd, "a")
print(repr(log.name))                                # 3  <- the fd, not the path
print(vision_log_size(log))                          # os.path.getsize(3): stats the fd
vision_log_tail(log)                                 # open(3, "rb") -> closes fd 3 on exit
msvcrt.get_osfhandle(log.fileno())                   # OSError: [Errno 9] Bad file descriptor
```

Full run on this machine:

```
name = 3 isinstance str: False
handle before: 0x1f8
vision_log_size -> 0
vision_log_tail -> ''
handle after : FAILED OSError(9, 'Bad file descriptor') closed: False
write+flush FAILED OSError(9, 'Bad file descriptor')
```

Note `log.closed` stays `False` the whole time, so nothing in the server notices; the first use of the handle afterwards is what raises.

## What it looks like through the server

`Vision._start()` catches the GPU failure and calls `_start_reader()` again with the CPU args (`stderr=log`), and the spawn itself raises:

```
File "terminal/serve/server.py", line 2969, in _start
    self._start_reader()
File "terminal/serve/server.py", line 3031, in _start_reader
    self.proc = popen("the image encoder", args, role="vision", ..., stderr=log or subprocess.DEVNULL, ...)
File ".../subprocess.py", line 1422, in _execute_child
    errwrite = msvcrt.get_osfhandle(stderr.fileno())
OSError: [Errno 9] Bad file descriptor
```

and the server ends (nothing catches it in `_main`). At interpreter exit the log's finalizer repeats `Exception ignored in: <_io.TextIOWrapper name=4 mode='a' encoding='utf-8'> / OSError: [Errno 9] Bad file descriptor`.

## Where it comes from

- `serve/appendlog.py:43-47` — `fd = msvcrt.open_osfhandle(handle, os.O_APPEND | os.O_WRONLY); return os.fdopen(fd, "a", encoding=encoding)`. `os.fdopen` sets `.name` to the integer fd; TextIOWrapper's `name` is read-only, so it cannot be corrected afterwards.
- `serve/server_parts/engines.py:586-607` — both helpers take the file object and use `getattr(log, "name", None)` as a path: `vision_log_size` does `os.path.getsize(name)` (works by accident: `os.stat(fd)`), `vision_log_tail` does `with open(name, "rb") as f:` (thefd is treated as an fd and closed).
- Called on every failed reader start: `server.py:2981`, `2988`, `3040`, `3054`.

## Scope

Any failed GPU reader start on Windows reaches this path — no kernel for the card, card busy, no room — so on 0.2.17 the "fall back to the CPU" promise in the 0.2.16 notes does not hold. The engine's own start-failure helpers (`start_log_tail`, `engines.py:244`) take a path string and are not affected; only these two take the file object.

It is a side effect of the #5175 fix (the append-only handle), which is why it appears only with 0.2.17.

## Suggested fix

`engines.py`: only treat `.name` as a path when it is one, and get the path from the caller:

```python
name = getattr(log, "name", None)
if not isinstance(name, (str, bytes, os.PathLike)):
    name = None
```

With `name = None`, `vision_log_size` returns 0 and `vision_log_tail` returns "" — correct, but the "last lines of its log" that `vision_start_error` wants would be lost. So `Vision` should keep the path (`cfg["log"]` is what `server.py` opens the log from) and pass it down, or `appendlog` should return a small wrapper whose `.name` is the path and whose `fileno()`/`write()`/`flush()`/`close()`/`closed` go to the fdopen object; `subprocess` only needs `fileno()`.

I would send a patch, but which of the two directions you prefer is a design call — say which and I will write it with a test.

関連リンク

インストール・モデル・リリースへのサイト内リンク。