glog: check that stderr is valid before using it by default #72
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Windows Services are spawned without
stdout
andstderr
, so any attempt to use them equates to referencing an invalid fileHandle
.stderrSink
returns the error, which makeslogsink
trigger the termination of the process. The result is that any call tolog.Error
(orlog.Fatal
) from a Windows Service will always crash the program.This approach checks that
os.Stderr
's embedded file descriptor (Handle
, for Windows) is non-NULL once duringinit
to determine its validity and if it fails, then never registersstderrSink
as a logsink.Disabling based upon whether
os.Stderr
is NULL was chosen because it's precise, doesn't actually influence the result of any output and doesn't require any syscalls, but still detects the case in whichstderr
is unavailable on Windows.A rejected approach here would've been to swallow the errors from
stderrSink.Emit
, thus sparing us program termination at the hands oflogsink
. I rejected this approach because it felt like it could result in some subtle behavioral changes. For example, invokinglog.Error
afteros.Stderr.Close()
would no longer exit the program and that felt like a non-trivial change (although I can think of no reason why one would desire this behavior).cl/680817112 (google-internal)