sync /dev/stdout: The handle is invalid.
Author: noneymousCreated Aug 18, 2021Updated Sep 18, 2025
Hi,
I'm using Zap in a Windows executable. I'm trying to tee multiple cores together, which is working fine. One of them is a standard Zap console core using os.Stdout. On program termination, I'm closing down all loggers, which also includes calling .Sync(). The sync function works fine on my cores but fails on the console logger (It seems the stdout handle is already gone). I'm wondering, if there is something I can do about it...
Here is a basic code sample (only involving the console core) demonstrating the issue:
// Lock stdout
ws := zapcore.Lock(os.Stdout)
// Create the encoder
enc := zapcore.NewConsoleEncoder(zap.NewDevelopmentEncoderConfig())
// Create the core
consCore := zapcore.NewCore(enc, ws, zapcore.Level(-1))
// Tee all the cores together
// My applications uses multiple cores
tee := zapcore.NewTee(consCore)
// Set the global logger
logger := zap.New(tee).Sugar()
// Send test message
logger.Infof("Test")
// Execute last sync
defer func(){
err := logger.Sync()
if err != nil {
fmt.Println(err)
}
}()Here is the output, .Sync() failed on this console logger:

Just to make it clear, I cannot skip calling .Sync() because I have multiple cores teeed together which need the Sync call.
Source: uber-go/zap