zap icon indicating copy to clipboard operation
zap copied to clipboard

sync /dev/stderr: inappropriate ioctl for device

Open caevv opened this issue 4 years ago • 9 comments

Both on mac and linux I get this error when doing zapLogger.Sync():

sync /dev/stderr: inappropriate ioctl for device

Code:

zapLogger, err := zap.NewProduction()
if err != nil {
	log.Fatal(err.Error())
}

defer func() {
	if err := zapLogger.Sync(); err != nil {
		log.Print(err.Error())
	}
}()
logger := zapLogger.Sugar()

caevv avatar Nov 20 '20 12:11 caevv

Thanks for the report @caevv

This is a known issue, see previous threads related to this: #772 and #370.

The error itself is slightly different on OS X vs Linux. OS X: inappropriate ioctl for device Linux: sync /dev/stderr: invalid argument

The error only happens when stdout/stderr point to the console, but not if they are redirected to a file (since files support sync).

I think it's worth considering some workarounds for this issue it's a common scenario. A couple of possible ideas:

  • Ignore these specific errors from Sync (for any file)
  • After opening a file, immediately perform a Sync, and if returns an error, then ignore future Sync errors (if they are the same).

prashantv avatar Nov 20 '20 16:11 prashantv

Can we have errors.Is(err, zap.ErrConsoleSync) in the mean time?

caevv avatar Nov 20 '20 16:11 caevv

This error comes from the underlying platform, so to achieve the above, we'd need to have platform-specific detection, and wrapping of the errors in zap. So it would require a similar amount of work in zap first.

prashantv avatar Nov 20 '20 16:11 prashantv

Hey, it's been almost a year. Is there any update on this issue?

Using this as a hotfix. correct me if I have done anything wrong.

	lc.Append(fx.Hook{
++		OnStart: func(c context.Context) error {
++			if logger.Sync() != nil {
++				ignoreSync = true
++			}
++			return nil
++		},
		OnStop: func(ctx context.Context) error {
++			if ignoreSync {
++				return nil
++			}
			err := logger.Sync()
			return eris.Wrap(err, "log: could not sync before exit")
		},
	})

hsblhsn avatar Aug 26 '21 04:08 hsblhsn

any update?

loeffel-io avatar Jun 14 '22 15:06 loeffel-io

any update?

vigo avatar Jul 06 '22 10:07 vigo

any update?

erhanakp avatar Jul 06 '22 10:07 erhanakp

I think you can use this approach for now: https://github.com/uber-go/zap/issues/991#issuecomment-962098428

adriancarayol avatar Jul 12 '22 14:07 adriancarayol