Bug 2545 – write(f)(ln) delays throwing StdioException("Bad file descriptor") when no console is available
Status
RESOLVED
Resolution
WONTFIX
Severity
enhancement
Priority
P2
Component
phobos
Product
D
Version
D2
Platform
x86
OS
Windows
Creation time
2008-12-29T09:29:00Z
Last change time
2015-06-09T01:20:50Z
Assigned to
andrei
Creator
2korden
Comments
Comment #0 by 2korden — 2008-12-29T09:29:13Z
It throws an exception at first call to flush when no stdout is a available (e.g. code compiled for SUBSYSTEM WINDOWS) and it causes some problems for GUI apps. I believe it should fail immediately (at the very first call to write) or (preferably) never fail at all. Otherwise, it leads to problem that are hard to track (e.g. all the unittests pass, but an application fails silently with no reason after a few hours of execution). I think its behavior should be close to printf which suppresses an error and goes on.
Test code:
import std.stdio;
void main()
{
while(true) {
//printf("!"); // works forever
write("!"); // fails at first call to flush
}
}
Comment #1 by smjg — 2008-12-29T10:21:18Z
How can it never fail at all? Or are you confusing "fail" with "throw an exception"?
In any case, failing silently is completely the wrong approach. If there is no stdout to write to, it should throw an immediate exception. That's what exceptions are there for.
printf doesn't suppress errors. It returns a value that indicates success or failure. This is because C doesn't have exception handling, and so C functions have to improvise. In D, OTOH, the only right thing to do is use exceptions to indicate failure.
http://www.digitalmars.com/d/1.0/errors.html
Comment #2 by andrei — 2008-12-29T12:06:33Z
Currently write forwards calls to the corresponding system routines and faithfully transforms their error codes into exceptions. I agree with Steward that non-failure is not an option :o), but I also agree that this state of affairs may also cause problems.
I could put a check in write to make it fail sooner, but I'm thinking that maybe that behavior on Windows is intentional, e.g. to allow users to create their own console and flush to them or whatnot.
Comment #3 by andrei — 2008-12-29T12:07:12Z
(In reply to comment #2)
> Currently write forwards calls to the corresponding system routines and
> faithfully transforms their error codes into exceptions. I agree with Steward
I meant Stewart, sorry. -- Andrei
Comment #4 by smjg — 2008-12-29T13:42:12Z
(In reply to comment #2)
> I could put a check in write to make it fail sooner, but I'm thinking that
> maybe that behavior on Windows is intentional, e.g. to allow users to create
> their own console and flush to them or whatnot.
What do you mean by this?
Comment #5 by andrei — 2010-09-26T12:07:13Z
I think the behavior is as designed now. If not, please reopen.