Files
syncthing/lib/osutil/multiwriter_test.go
ShabloneandElias 5c0a725b82 fix(monitor): continue log writes when stdout is unavailable on detached Windows consoles (fixes #10882) (#10889)
### Purpose

#10726 introduces a problematic side effect which only surfaced recently
in #10882:
On Windows 24H2 stdout will be invalid when Syncthing is started
detached from a console. This caused the monitors logwriter to fail
silently before it writes it a log file.
This PR fixes this by letting the multiwriter continue even after
failing on stdout.

### Testing

#10882 can be reproduced without this PRs commit. With this PR logging
does work again.
Following scenarios are tested in which the logfile will be written
again
-console: syncthing.exe
-console: start "" syncthing.exe
-console: start "" syncthing.exe serve --log-file=path
-explorer: double click


`go test ./lib/osutil ` checks if the introduced tolerantMultiWriter
writes to all targets even on errors.

---------

Signed-off-by: Elias <1elias.bauer@gmail.com>
Co-authored-by: Elias <1elias.bauer@gmail.com>
2026-09-22 14:47:41 +02:00

50 lines
1.4 KiB
Go

// Copyright (C) 2026 The Syncthing Authors.
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
// You can obtain one at https://mozilla.org/MPL/2.0/.
package osutil
import (
"bytes"
"errors"
"testing"
)
// failingWriter simulates a writer that always fails. This is enough to verify
// that the tolerant multi-writer still writes to the remaining targets and
// reports the failure in a combined error.
type failingWriter struct {
err error
buf *bytes.Buffer
}
func (w *failingWriter) Write(p []byte) (int, error) {
if w.err != nil {
return 0, w.err
}
return w.buf.Write(p)
}
func TestTolerantMultiWriter(t *testing.T) {
// A successful writer should still receive the payload even when another
// writer in the set fails. The failing writer is expected to be reported in
// the returned error, but it must not prevent the other write from happening.
var ok bytes.Buffer
fail := &failingWriter{err: errors.New("boom")}
mw := TolerantMultiWriter{&ok, fail}
const payload = "hello"
n, err := mw.Write([]byte(payload))
if n != len(payload) {
t.Fatalf("Write returned %d bytes, want %d", n, len(payload))
}
if err == nil || !errors.Is(err, fail.err) {
t.Fatalf("Write returned %v, want wrapped failing write error", err)
}
if got := ok.String(); got != payload {
t.Fatalf("successful writer got %q, want %q", got, payload)
}
}