Description:
On Windows, the "traditional" error log is not a file mysqld writes to through a
logging API. It is stderr (and stdout) redirected into a file. open_error_log()
in sql/log.cc:
do {
errors = 0;
if (!my_freopen(filename, "a", stderr)) errors++;
if (!my_freopen(filename, "a", stdout)) errors++;
} while (retries-- && errors);
if (errors) goto fail;
/* The error stream must be unbuffered. */
setbuf(stderr, nullptr);
and the writes are plain stdio on that stream (log_write_errstream(),
sql/log.cc:2184-2185):
fprintf(stderr, "%.*s\n", (int)length, buffer);
fflush(stderr);
On Windows, my_freopen() forwards to my_win_freopen() (mysys/my_winfile.cc),
which does this:
HANDLE osfh =
CreateFile(path, GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
nullptr, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, nullptr);
if (osfh == INVALID_HANDLE_VALUE) return nullptr;
auto chg = create_scope_guard([osfh]() { CloseHandle(osfh); });
const int handle_fd = _open_osfhandle((intptr_t)osfh, _O_APPEND | _O_TEXT);
if (handle_fd == -1) return nullptr;
...
if (_dup2(handle_fd, fd) < 0) return nullptr;
The append semantics therefore live only in the CRT's file-descriptor flag; the
handle itself carries no append right. The CRT implements _O_APPEND in user mode
as two separate kernel calls, SetFilePointer(END) followed by WriteFile. There is
no mutual exclusion of any kind between those two steps, so two processes that
hold the same log file will overwrite each other's output.
Windows does provide the guarantee; this call asks for it in a way that turns it
off. Per the FILE_APPEND_DATA documentation:
"For a file object, the right to append data to the file. (For local files,
write operations will not overwrite existing data if this flag is specified
without FILE_WRITE_DATA.)"
GENERIC_WRITE contains FILE_WRITE_DATA, so the condition in that sentence does not
hold: writes follow the file pointer, and the kernel is not forcing EOF either.
Why the code ends up in that form (it is not a choice among three; it is the only
one left). Three points:
1. The mode argument does not take part in the access rights. The second argument
of CreateFile is the hardcoded constant GENERIC_READ | GENERIC_WRITE; mode only
appears in the fallback branch for services (no console, so _fileno() returns
-1 and the code falls back to freopen(path, mode, stream)). Passing "a" and
passing anything else produce exactly the same handle; there is no step of the
form "you want append, so you get append rights".
2. The correct implementation of an atomic append already exists in the same file:
my_win_write() (mysys/my_winfile.cc:581-588) uses an
OVERLAPPED{0xFFFFFFFF, 0xFFFFFFFF} and even quotes MSDN in a comment. But the
error log is a CRT stream: the WriteFile is issued by the CRT's write(), which
cannot reach my_win_write() (the mysys handle table and the CRT descriptors are
two disjoint spaces).
3. So the only place that can express "append" is
_open_osfhandle(..., _O_APPEND | _O_TEXT) - the two-step implementation above.
The write path is not careless about concurrency, which is what makes this look
like an oversight rather than a decision: log_write_errstream() takes
LOCK_error_log around the fprintf/fflush pair (sql/log.cc:2182-2187), precisely so
that two writers cannot interleave. But that is a per-process mutex: it serializes
writers inside one mysqld and cannot serialize two processes holding the same file.
Within one process the log is safe; the moment a second process is given the same
--log-error, nothing protects it.
Impact
======
1. The error log is silently corrupted: whole lines lost, lines truncated, two
lines interleaved character by character. As evidence for auditing and
troubleshooting its value is undermined - with no error and no warning
whatsoever.
2. This is a real mechanism by which malformed input arises without anyone editing
the file. It is a defect independent of Bug #121441 (the parse side, where one
malformed line makes startup crash): this report does not need "does it crash"
to be established, and is neither proved nor disproved by it.
3. It is different from Bug #31158239: that one is "the CRT's append flag is not
inherited by a child process across CreateProcess"; this one is "two processes
open the same file independently and neither handle carries an append right".
Same root cause (append semantics kept in a CRT flag instead of on the handle),
different trigger path.
- MariaDB fixed it this way. MDEV-10907 (2016-09-27, Bug, priority Major,
component Platform Windows, fixed in 5.5.53) says: "On Windows, these writes can
interleave, since appends to the log file are not atomic. Sometimes, some lines
from server get lost. Sometimes some lines from mtr get lost." Its fix commit
b38d3c3d is titled "Ensure atomic appends to the error log by using CreateFile
with FILE_APPEND_DATA flag to open error log file (both MTR and server)" and
makes exactly the two changes above, in the same function my_win_freopen().
- MySQL fixed the same root cause on a different path. Bug #31158239
(2020-04-19, commit e2f6859) added fix_file_append_flag_inheritance() to
mysql-test/lib/My/SafeProcess/safe_process_win.cc, using
ReOpenFile(old_handle, FILE_APPEND_DATA, FILE_SHARE_WRITE | FILE_SHARE_READ, 0).
The comment above it reads: "This is in contrast to the C runtime flag that is
set by f(re)open methods and is not communicated to OS, i.e. is local to
process... This method allows several processes to append a common file
concurrently, without the safe_process child overwriting what others append."
- Java went this way earlier: JDK-6631352 (filed 2007, fixed in JDK 7) is titled
"File{OutputStream,Writer} should implement atomic append mode using
FILE_APPEND_DATA (win)".
- Python records the same thing: CPython issue 42606 (2020-12, still open) -
"the Microsoft C runtime implements O_APPEND as an lseek(fd, 0, SEEK_END)
followed by write()", and it lists the same two correct approaches.
The latest code still has the old form. I verified this because it decides whether
this report is still needed: the source of 8.4.8 on this machine, and the 8.4 and
trunk branches of mysql/mysql-server on GitHub (all copyright 2026), all three
still have CreateFile(path, GENERIC_READ | GENERIC_WRITE, ...) together with
_open_osfhandle(..., _O_APPEND | _O_TEXT). The most recent commit touching
mysys/my_winfile.cc on trunk (5c0ce87a, 2026-07-01) does change this very
function, and for the error log - it handles the delete-pending error log case
(Bug#32901809), and its new comment starts with "open_error_log() uses this
function for stderr and stdout and retries a failed reopen." The path is being
maintained; only those two lines about append semantics have never been touched.
Environment
===========
MySQL Server 8.4.8 (Windows x86_64), mysqld.exe 53,796,480 bytes, built 2025-12-15.
Windows 11 build 26200. The mechanism is not version-specific: it affects every
Windows release and every version that still uses this function.
Related bugs
============
- Bug #121441 - the parse side (one malformed line crashes startup at 0xC0000409).
Related, not a duplicate: the two defects are independent.
- Bug #31158239 - same root cause, fixed on a different path.
How to repeat:
(1) Minimal experiment. Two independent processes, each appending 100,000
fixed-length lines to the same file. The only variable is the desired access
passed to CreateFileW():
HANDLE h = CreateFileW(path, ACCESS,
FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
int fd = _open_osfhandle((intptr_t)h, _O_APPEND | _O_TEXT);
for (int i = 0; i < 100000; i++) _write(fd, line, linelen);
A: ACCESS = GENERIC_READ | GENERIC_WRITE (0xC0000000)
B: ACCESS = GENERIC_READ | FILE_APPEND_DATA (0x80000004)
C: ACCESS = GENERIC_READ | GENERIC_WRITE, no _O_APPEND on the fd, and an
explicit _lseeki64(fd, 0, SEEK_END) before every _write (control)
Results on Windows 11 build 26200, two independent runs, same numbers:
group declared writes lines on disk lines lost
C 200,000 108,362 91,638 (45.8%)
A 200,000 106,310 93,690 (46.8%)
B 200,000 200,000 0 (0.0%)
Each worker reported that it completed all 100,000 writes, so the missing lines
were written and then overwritten - they were not skipped.
Actual result: group A behaves like the control. GENERIC_READ | GENERIC_WRITE
plus _O_APPEND is no better than an explicit seek-then-write.
Expected result: group B's behaviour - zero lines lost.
(2) With mysqld itself. Two mysqld processes, each doing a real
--initialize-insecure into its own empty datadir, both told to use the same
--log-error and the same --log-error-verbosity=3, so both go through
open_error_log() -> my_freopen(filename, "a", stderr):
control, one writer, 3 runs: 242 lines each, 0 malformed
two writers, 8 runs: 3,861 lines on disk (of 3,872 expected)
2,890 malformed (74.9%)
83 lines carrying two separate "[MY-" markers
(3) Field evidence. In a 56 MB production error log, 441 occurrences of
"starting as process" can be matched, and 363 of them are paired starts - two
processes 2-3 lines apart, with different PIDs, typically 11-120 ms apart (280
distinct PIDs). Lines 234-241 are one clean example:
234: 07:29:58.879979Z 0 [System] [MY-015015] [Server] MySQL Server - start. <- instance 1
235: 07:29:59.122653Z 0 [System] [MY-010116] [Server] ... starting as process 7640 <- instance 1
236: 07:29:59.171742Z 1 [System] [MY-013576] [InnoDB] InnoDB initialization has started. <- instance 1
237: 07:29:58.891475Z 0 [System] [MY-015015] [Server] MySQL Server - start. <- instance 2
238: 07:29:59.210600Z 0 [System] [MY-010116] [Server] ... starting as process 7560 <- instance 2
239: 07:29:59.229316Z 1 [System] [MY-013576] [InnoDB] InnoDB initialization has started. <- instance 2
240: 07:29:59.259741Z 1 [ERROR] [MY-012611] [InnoDB] Operating system error number 32 in a file operation.
241: 07:29:59.261685Z 1 [ERROR] [MY-012615] [InnoDB] The error means that another program is using
InnoDB's files. ... or another instance of MySQL.
Both processes ran their own full startup sequence (both printed "InnoDB
initialization has started."), so these are two real servers racing for the same
datadir. Instance 2 lost: error 32 is Windows ERROR_SHARING_VIOLATION (the file is
in use by another process). It retries every 10 seconds and does not exit - that
pair of error lines occurs 157,933 times in the file.
The longest single stretch is 13 hours 26 minutes (2026-04-19 02:39:07 ->
16:05:31, 4,834 retries), while the winning instance reached "ready for
connections" at 02:39:08 and only shut down gracefully at 16:05:36 - so for those
13 hours 26 minutes both processes held the same file and each wrote its own
output into it. This is not a one-off: it happens almost daily in that log.
(4) A note on detecting this. With equal-length lines, an overwrite replaces one
complete line with another complete line: the file looks perfectly well-formed and
a line-format check reports no corruption at all. Only the line count reveals the
loss. Real log lines differ in length, so an overwrite leaves a truncated line
behind - and when the two positions differ by a few bytes, the result is the
character-by-character interleaving.
Suggested fix:
HANDLE osfh =
- CreateFile(path, GENERIC_READ | GENERIC_WRITE,
+ CreateFile(path, GENERIC_READ | FILE_APPEND_DATA,
FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
nullptr, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, nullptr);
...
- const int handle_fd = _open_osfhandle((intptr_t)osfh, _O_APPEND | _O_TEXT);
+ const int handle_fd = _open_osfhandle((intptr_t)osfh, _O_TEXT);
Both lines have to change. _O_APPEND should go: the append right is now on the
kernel handle, and keeping it only makes the CRT perform one pointless lseek.
After the change, append no longer depends on any CRT behaviour.
FILE_APPEND_DATA has to replace GENERIC_WRITE, not be added to it: GENERIC_WRITE
already contains FILE_APPEND_DATA, and the documented behaviour applies only while
FILE_WRITE_DATA is absent.
Note also that mode does not take part in the access rights in this function (see
above), so switching to FILE_APPEND_DATA affects every caller. The only current
caller, open_error_log(), passes "a", but it is worth making that contract
explicit - MariaDB's patch added exactly this line:
DBUG_ASSERT(strchr(mode, 'a')); /* We use FILE_APPEND_DATA below */
Description: On Windows, the "traditional" error log is not a file mysqld writes to through a logging API. It is stderr (and stdout) redirected into a file. open_error_log() in sql/log.cc: do { errors = 0; if (!my_freopen(filename, "a", stderr)) errors++; if (!my_freopen(filename, "a", stdout)) errors++; } while (retries-- && errors); if (errors) goto fail; /* The error stream must be unbuffered. */ setbuf(stderr, nullptr); and the writes are plain stdio on that stream (log_write_errstream(), sql/log.cc:2184-2185): fprintf(stderr, "%.*s\n", (int)length, buffer); fflush(stderr); On Windows, my_freopen() forwards to my_win_freopen() (mysys/my_winfile.cc), which does this: HANDLE osfh = CreateFile(path, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, nullptr, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, nullptr); if (osfh == INVALID_HANDLE_VALUE) return nullptr; auto chg = create_scope_guard([osfh]() { CloseHandle(osfh); }); const int handle_fd = _open_osfhandle((intptr_t)osfh, _O_APPEND | _O_TEXT); if (handle_fd == -1) return nullptr; ... if (_dup2(handle_fd, fd) < 0) return nullptr; The append semantics therefore live only in the CRT's file-descriptor flag; the handle itself carries no append right. The CRT implements _O_APPEND in user mode as two separate kernel calls, SetFilePointer(END) followed by WriteFile. There is no mutual exclusion of any kind between those two steps, so two processes that hold the same log file will overwrite each other's output. Windows does provide the guarantee; this call asks for it in a way that turns it off. Per the FILE_APPEND_DATA documentation: "For a file object, the right to append data to the file. (For local files, write operations will not overwrite existing data if this flag is specified without FILE_WRITE_DATA.)" GENERIC_WRITE contains FILE_WRITE_DATA, so the condition in that sentence does not hold: writes follow the file pointer, and the kernel is not forcing EOF either. Why the code ends up in that form (it is not a choice among three; it is the only one left). Three points: 1. The mode argument does not take part in the access rights. The second argument of CreateFile is the hardcoded constant GENERIC_READ | GENERIC_WRITE; mode only appears in the fallback branch for services (no console, so _fileno() returns -1 and the code falls back to freopen(path, mode, stream)). Passing "a" and passing anything else produce exactly the same handle; there is no step of the form "you want append, so you get append rights". 2. The correct implementation of an atomic append already exists in the same file: my_win_write() (mysys/my_winfile.cc:581-588) uses an OVERLAPPED{0xFFFFFFFF, 0xFFFFFFFF} and even quotes MSDN in a comment. But the error log is a CRT stream: the WriteFile is issued by the CRT's write(), which cannot reach my_win_write() (the mysys handle table and the CRT descriptors are two disjoint spaces). 3. So the only place that can express "append" is _open_osfhandle(..., _O_APPEND | _O_TEXT) - the two-step implementation above. The write path is not careless about concurrency, which is what makes this look like an oversight rather than a decision: log_write_errstream() takes LOCK_error_log around the fprintf/fflush pair (sql/log.cc:2182-2187), precisely so that two writers cannot interleave. But that is a per-process mutex: it serializes writers inside one mysqld and cannot serialize two processes holding the same file. Within one process the log is safe; the moment a second process is given the same --log-error, nothing protects it. Impact ====== 1. The error log is silently corrupted: whole lines lost, lines truncated, two lines interleaved character by character. As evidence for auditing and troubleshooting its value is undermined - with no error and no warning whatsoever. 2. This is a real mechanism by which malformed input arises without anyone editing the file. It is a defect independent of Bug #121441 (the parse side, where one malformed line makes startup crash): this report does not need "does it crash" to be established, and is neither proved nor disproved by it. 3. It is different from Bug #31158239: that one is "the CRT's append flag is not inherited by a child process across CreateProcess"; this one is "two processes open the same file independently and neither handle carries an append right". Same root cause (append semantics kept in a CRT flag instead of on the handle), different trigger path. - MariaDB fixed it this way. MDEV-10907 (2016-09-27, Bug, priority Major, component Platform Windows, fixed in 5.5.53) says: "On Windows, these writes can interleave, since appends to the log file are not atomic. Sometimes, some lines from server get lost. Sometimes some lines from mtr get lost." Its fix commit b38d3c3d is titled "Ensure atomic appends to the error log by using CreateFile with FILE_APPEND_DATA flag to open error log file (both MTR and server)" and makes exactly the two changes above, in the same function my_win_freopen(). - MySQL fixed the same root cause on a different path. Bug #31158239 (2020-04-19, commit e2f6859) added fix_file_append_flag_inheritance() to mysql-test/lib/My/SafeProcess/safe_process_win.cc, using ReOpenFile(old_handle, FILE_APPEND_DATA, FILE_SHARE_WRITE | FILE_SHARE_READ, 0). The comment above it reads: "This is in contrast to the C runtime flag that is set by f(re)open methods and is not communicated to OS, i.e. is local to process... This method allows several processes to append a common file concurrently, without the safe_process child overwriting what others append." - Java went this way earlier: JDK-6631352 (filed 2007, fixed in JDK 7) is titled "File{OutputStream,Writer} should implement atomic append mode using FILE_APPEND_DATA (win)". - Python records the same thing: CPython issue 42606 (2020-12, still open) - "the Microsoft C runtime implements O_APPEND as an lseek(fd, 0, SEEK_END) followed by write()", and it lists the same two correct approaches. The latest code still has the old form. I verified this because it decides whether this report is still needed: the source of 8.4.8 on this machine, and the 8.4 and trunk branches of mysql/mysql-server on GitHub (all copyright 2026), all three still have CreateFile(path, GENERIC_READ | GENERIC_WRITE, ...) together with _open_osfhandle(..., _O_APPEND | _O_TEXT). The most recent commit touching mysys/my_winfile.cc on trunk (5c0ce87a, 2026-07-01) does change this very function, and for the error log - it handles the delete-pending error log case (Bug#32901809), and its new comment starts with "open_error_log() uses this function for stderr and stdout and retries a failed reopen." The path is being maintained; only those two lines about append semantics have never been touched. Environment =========== MySQL Server 8.4.8 (Windows x86_64), mysqld.exe 53,796,480 bytes, built 2025-12-15. Windows 11 build 26200. The mechanism is not version-specific: it affects every Windows release and every version that still uses this function. Related bugs ============ - Bug #121441 - the parse side (one malformed line crashes startup at 0xC0000409). Related, not a duplicate: the two defects are independent. - Bug #31158239 - same root cause, fixed on a different path. How to repeat: (1) Minimal experiment. Two independent processes, each appending 100,000 fixed-length lines to the same file. The only variable is the desired access passed to CreateFileW(): HANDLE h = CreateFileW(path, ACCESS, FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); int fd = _open_osfhandle((intptr_t)h, _O_APPEND | _O_TEXT); for (int i = 0; i < 100000; i++) _write(fd, line, linelen); A: ACCESS = GENERIC_READ | GENERIC_WRITE (0xC0000000) B: ACCESS = GENERIC_READ | FILE_APPEND_DATA (0x80000004) C: ACCESS = GENERIC_READ | GENERIC_WRITE, no _O_APPEND on the fd, and an explicit _lseeki64(fd, 0, SEEK_END) before every _write (control) Results on Windows 11 build 26200, two independent runs, same numbers: group declared writes lines on disk lines lost C 200,000 108,362 91,638 (45.8%) A 200,000 106,310 93,690 (46.8%) B 200,000 200,000 0 (0.0%) Each worker reported that it completed all 100,000 writes, so the missing lines were written and then overwritten - they were not skipped. Actual result: group A behaves like the control. GENERIC_READ | GENERIC_WRITE plus _O_APPEND is no better than an explicit seek-then-write. Expected result: group B's behaviour - zero lines lost. (2) With mysqld itself. Two mysqld processes, each doing a real --initialize-insecure into its own empty datadir, both told to use the same --log-error and the same --log-error-verbosity=3, so both go through open_error_log() -> my_freopen(filename, "a", stderr): control, one writer, 3 runs: 242 lines each, 0 malformed two writers, 8 runs: 3,861 lines on disk (of 3,872 expected) 2,890 malformed (74.9%) 83 lines carrying two separate "[MY-" markers (3) Field evidence. In a 56 MB production error log, 441 occurrences of "starting as process" can be matched, and 363 of them are paired starts - two processes 2-3 lines apart, with different PIDs, typically 11-120 ms apart (280 distinct PIDs). Lines 234-241 are one clean example: 234: 07:29:58.879979Z 0 [System] [MY-015015] [Server] MySQL Server - start. <- instance 1 235: 07:29:59.122653Z 0 [System] [MY-010116] [Server] ... starting as process 7640 <- instance 1 236: 07:29:59.171742Z 1 [System] [MY-013576] [InnoDB] InnoDB initialization has started. <- instance 1 237: 07:29:58.891475Z 0 [System] [MY-015015] [Server] MySQL Server - start. <- instance 2 238: 07:29:59.210600Z 0 [System] [MY-010116] [Server] ... starting as process 7560 <- instance 2 239: 07:29:59.229316Z 1 [System] [MY-013576] [InnoDB] InnoDB initialization has started. <- instance 2 240: 07:29:59.259741Z 1 [ERROR] [MY-012611] [InnoDB] Operating system error number 32 in a file operation. 241: 07:29:59.261685Z 1 [ERROR] [MY-012615] [InnoDB] The error means that another program is using InnoDB's files. ... or another instance of MySQL. Both processes ran their own full startup sequence (both printed "InnoDB initialization has started."), so these are two real servers racing for the same datadir. Instance 2 lost: error 32 is Windows ERROR_SHARING_VIOLATION (the file is in use by another process). It retries every 10 seconds and does not exit - that pair of error lines occurs 157,933 times in the file. The longest single stretch is 13 hours 26 minutes (2026-04-19 02:39:07 -> 16:05:31, 4,834 retries), while the winning instance reached "ready for connections" at 02:39:08 and only shut down gracefully at 16:05:36 - so for those 13 hours 26 minutes both processes held the same file and each wrote its own output into it. This is not a one-off: it happens almost daily in that log. (4) A note on detecting this. With equal-length lines, an overwrite replaces one complete line with another complete line: the file looks perfectly well-formed and a line-format check reports no corruption at all. Only the line count reveals the loss. Real log lines differ in length, so an overwrite leaves a truncated line behind - and when the two positions differ by a few bytes, the result is the character-by-character interleaving. Suggested fix: HANDLE osfh = - CreateFile(path, GENERIC_READ | GENERIC_WRITE, + CreateFile(path, GENERIC_READ | FILE_APPEND_DATA, FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, nullptr, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, nullptr); ... - const int handle_fd = _open_osfhandle((intptr_t)osfh, _O_APPEND | _O_TEXT); + const int handle_fd = _open_osfhandle((intptr_t)osfh, _O_TEXT); Both lines have to change. _O_APPEND should go: the append right is now on the kernel handle, and keeping it only makes the CRT perform one pointless lseek. After the change, append no longer depends on any CRT behaviour. FILE_APPEND_DATA has to replace GENERIC_WRITE, not be added to it: GENERIC_WRITE already contains FILE_APPEND_DATA, and the documented behaviour applies only while FILE_WRITE_DATA is absent. Note also that mode does not take part in the access rights in this function (see above), so switching to FILE_APPEND_DATA affects every caller. The only current caller, open_error_log(), passes "a", but it is worth making that contract explicit - MariaDB's patch added exactly this line: DBUG_ASSERT(strchr(mode, 'a')); /* We use FILE_APPEND_DATA below */