Skip to content

Commit 88b9359

Browse files
committed
Fix stderr flushing problem
1 parent 49de41e commit 88b9359

1 file changed

Lines changed: 18 additions & 2 deletions

File tree

src/node_stdio.cc

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
#include <unistd.h>
66
#include <fcntl.h>
7+
#include <string.h>
8+
#include <errno.h>
79

810
using namespace v8;
911
using namespace node;
@@ -42,6 +44,15 @@ EmitClose (void)
4244
emit->Call(stdio, 1, argv);
4345
}
4446

47+
48+
static inline Local<Value> errno_exception(int errorno) {
49+
Local<Value> e = Exception::Error(String::NewSymbol(strerror(errorno)));
50+
Local<Object> obj = e->ToObject();
51+
obj->Set(String::NewSymbol("errno"), Integer::New(errorno));
52+
return e;
53+
}
54+
55+
4556
/* STDERR IS ALWAY SYNC */
4657
static Handle<Value>
4758
WriteError (const Arguments& args)
@@ -53,8 +64,13 @@ WriteError (const Arguments& args)
5364

5465
String::Utf8Value msg(args[0]->ToString());
5566

56-
fprintf(stderr, "%s", *msg);
57-
fflush(stderr);
67+
ssize_t r;
68+
size_t written = 0;
69+
while (written < msg.length()) {
70+
r = write(STDERR_FILENO, (*msg) + written, msg.length() - written);
71+
if (r < 0) return ThrowException(errno_exception(errno));
72+
written += (size_t)r;
73+
}
5874

5975
return Undefined();
6076
}

0 commit comments

Comments
 (0)