[GH-ISSUE #5175] [Engine] Windows: the image encoder's log handle overwrites the engine's lines in the shared server log #3080

Open
opened 2026-11-07 14:37:55 +07:00 by bsakaguchi · 0 comments
Owner

Originally created by @Cryonic7 on GitHub (Nov 4, 2026).
Original GitHub issue: https://github.com/Niko1221/Strata/issues/5175

Area

[Engine] (speed, kernels, models, the server and API)

Desktop app, Terminal or source?

Source (START-HERE.bat, setup.sh or a build)

What happened

On Windows, with a "log" file and a "vision" section in the config, the server writes three components' output into that one file through separate handles:

  • the engine's, opened at each engine start (serve/server.py:1121);
  • the image encoder's, opened once at server start (serve/server.py:9754);
  • the image generator's, also opened at server start (serve/server.py:9867).

Each handle is passed to a child process as its stderr. On Windows each handle keeps its own file position, and a child process writes at that position. Python's "a" mode only moves to the end of the file for writes made by the Python process itself, not for writes a child makes through the inherited handle.

So an image encoder started minutes after the engine (an elastic encoder starts on the first picture) writes at the position the log had when the server started. That is over the engine's start-up lines. We saw the session's first lines (engine started: ..., the cache sizing lines) overwritten by the encoder's output, more with each picture, and the engine's next lines can land over the encoder's in turn.

Expected: every component's lines kept, in the order they were written.

Smallest steps or command that reproduce it

The mechanism, without Strata (Windows, Python 3):

import os, subprocess, sys, tempfile
log = os.path.join(tempfile.mkdtemp(), "x.log")
open(log, "w").write("previous session\n")
reader = open(log, "a")              # like the encoder's handle, opened at server start
engine = open(log, "a")              # like the engine's handle, opened at engine start
def child(fh, text):
    subprocess.Popen([sys.executable, "-c", f"import sys; sys.stderr.write({text!r})"], stderr=fh).wait()
engine.write("engine started\n"); engine.flush()
child(engine, "engine 1\n")
child(reader, "reader A\n")          # lands right after "previous session", over "engine started"
print(open(log).read())

In Strata: start a server on Windows with "log" and an elastic "vision" section, let the engine come up, send one request with a picture, then read the first lines of the log.

Reproduction status

Reproduced locally

Strata version

Strata 0.2.10 · engine 0.1.56 has the same three handles at the lines above (read, not run live). We saw it happen on our lab server built from 0.1.44, which has the same code at these sites, and the script above shows the mechanism.

Graphics card, memory and system

RTX 4090 24 GB · 64 GB RAM · Windows 11 Pro

Model, size and settings

Swift 1.5 IQ3_S · 262K context · pictures on (GPU encoder, "elastic": true)

A fix that works here

Open every log handle a child process writes to with FILE_APPEND_DATA and without FILE_WRITE_DATA. Windows then puts each write at the end of the file, whoever makes it. POSIX already does this with O_APPEND, so nothing changes there.

def log_opener(path, flags):
    if os.name != "nt":
        return os.open(path, flags, 0o666)
    import _winapi
    import msvcrt
    try:        # FILE_APPEND_DATA | FILE_READ_ATTRIBUTES | SYNCHRONIZE; share read and write; OPEN_ALWAYS; a normal file
        handle = _winapi.CreateFile(os.fspath(path), 0x0004 | 0x0080 | 0x00100000, 0x1 | 0x2, 0, 4, 0x80, 0)
    except OSError:
        return os.open(path, flags, 0o666)     # as before
    try:
        return msvcrt.open_osfhandle(handle, os.O_APPEND)
    except OSError:
        _winapi.CloseHandle(handle)
        return os.open(path, flags, 0o666)

At each of the three sites, use open(log, "a", encoding="utf-8", opener=log_opener).

Passed:

  • The script above, in three cases:
    • both handles plain: lines overwritten;
    • only the encoder's handle append-only: the engine's writes still land over the encoder's;
    • both append-only: every line in order.
  • Live on our lab server: a start with two picture-encoder starts kept the whole log intact.

Not run:

  • the fix on a stock 0.2.10 server, live;
  • Linux and Mac (unaffected: O_APPEND).
Originally created by @Cryonic7 on GitHub (Nov 4, 2026). Original GitHub issue: https://github.com/Niko1221/Strata/issues/5175 ### Area [Engine] (speed, kernels, models, the server and API) ### Desktop app, Terminal or source? Source (START-HERE.bat, setup.sh or a build) ### What happened On Windows, with a `"log"` file and a `"vision"` section in the config, the server writes three components' output into that one file through separate handles: - the engine's, opened at each engine start (`serve/server.py:1121`); - the image encoder's, opened once at server start (`serve/server.py:9754`); - the image generator's, also opened at server start (`serve/server.py:9867`). Each handle is passed to a child process as its stderr. On Windows each handle keeps its own file position, and a child process writes at that position. Python's `"a"` mode only moves to the end of the file for writes made by the Python process itself, not for writes a child makes through the inherited handle. So an image encoder started minutes after the engine (an elastic encoder starts on the first picture) writes at the position the log had when the server started. That is over the engine's start-up lines. We saw the session's first lines (`engine started: ...`, the cache sizing lines) overwritten by the encoder's output, more with each picture, and the engine's next lines can land over the encoder's in turn. Expected: every component's lines kept, in the order they were written. ### Smallest steps or command that reproduce it The mechanism, without Strata (Windows, Python 3): ```python import os, subprocess, sys, tempfile log = os.path.join(tempfile.mkdtemp(), "x.log") open(log, "w").write("previous session\n") reader = open(log, "a") # like the encoder's handle, opened at server start engine = open(log, "a") # like the engine's handle, opened at engine start def child(fh, text): subprocess.Popen([sys.executable, "-c", f"import sys; sys.stderr.write({text!r})"], stderr=fh).wait() engine.write("engine started\n"); engine.flush() child(engine, "engine 1\n") child(reader, "reader A\n") # lands right after "previous session", over "engine started" print(open(log).read()) ``` In Strata: start a server on Windows with `"log"` and an elastic `"vision"` section, let the engine come up, send one request with a picture, then read the first lines of the log. ### Reproduction status Reproduced locally ### Strata version Strata 0.2.10 · engine 0.1.56 has the same three handles at the lines above (read, not run live). We saw it happen on our lab server built from 0.1.44, which has the same code at these sites, and the script above shows the mechanism. ### Graphics card, memory and system RTX 4090 24 GB · 64 GB RAM · Windows 11 Pro ### Model, size and settings Swift 1.5 IQ3_S · 262K context · pictures on (GPU encoder, `"elastic": true`) ### A fix that works here Open every log handle a child process writes to with `FILE_APPEND_DATA` and without `FILE_WRITE_DATA`. Windows then puts each write at the end of the file, whoever makes it. POSIX already does this with `O_APPEND`, so nothing changes there. ```python def log_opener(path, flags): if os.name != "nt": return os.open(path, flags, 0o666) import _winapi import msvcrt try: # FILE_APPEND_DATA | FILE_READ_ATTRIBUTES | SYNCHRONIZE; share read and write; OPEN_ALWAYS; a normal file handle = _winapi.CreateFile(os.fspath(path), 0x0004 | 0x0080 | 0x00100000, 0x1 | 0x2, 0, 4, 0x80, 0) except OSError: return os.open(path, flags, 0o666) # as before try: return msvcrt.open_osfhandle(handle, os.O_APPEND) except OSError: _winapi.CloseHandle(handle) return os.open(path, flags, 0o666) ``` At each of the three sites, use `open(log, "a", encoding="utf-8", opener=log_opener)`. **Passed:** - **The script above, in three cases:** - both handles plain: lines overwritten; - only the encoder's handle append-only: the engine's writes still land over the encoder's; - both append-only: every line in order. - **Live on our lab server:** a start with two picture-encoder starts kept the whole log intact. **Not run:** - the fix on a stock 0.2.10 server, live; - Linux and Mac (unaffected: `O_APPEND`).
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
Niko1221/Strata#3080
No description provided.