question about assert macro

35 views
Skip to first unread message

billywhizz

unread,
Feb 8, 2012, 10:44:32 AM2/8/12
to nodejs
i've been tinkering with the node.js bindings directly and have come
across an issue where i get a fatal exception thrown on a tcp_wrap
write operation when the buffer is not valid:

node: ../src/stream_wrap.cc:293: static v8::Handle<v8::Value>
node::StreamWrap::Write(const v8::Arguments&): Assertion
`Buffer::HasInstance(args[0])' failed.
Aborted

This is on 0.6.10.

I'm confused about the usage of assert everywhere in the c++ bindings
in the production release. shouldn't assert only be used in debug
mode? won't all these asserts hurt performance?

as a result of the assert, i don't seem to be able to catch the error
anywhere in uncaughtException handler or in SIG* handlers.

forgive me if it's a stupid question - i'm no c/c++ guru...

you can see the issue with the following script (assuming a server
listening on localhost:21):


var TCP = process.binding("tcp_wrap").TCP
var sock = new TCP();

process.on("uncaughtException", function(err) {
console.error(err);
});

var r = sock.connect("127.0.0.1", 21);
if(!r) process.exit(1);
r.oncomplete = function(status, peer, req) {
var wr = peer.write({});
if(!wr) {
peer.close();
return;
}
wr.oncomplete = function(status, peer, req) {
if(status != 0) {
peer.close();
return;
}
console.log("sent");
}
};

billywhizz

unread,
Feb 8, 2012, 10:59:54 AM2/8/12
to nodejs
also, if i define NDEBUG in node.h the assert doesn't happen and an
EFAULT error is returned when attempting to write to the socket. this
is on fedora12/64.

Ben Noordhuis

unread,
Feb 8, 2012, 11:13:45 AM2/8/12
to nod...@googlegroups.com
On Wed, Feb 8, 2012 at 16:44, billywhizz <apjo...@gmail.com> wrote:
> I'm confused about the usage of assert everywhere in the c++ bindings
> in the production release. shouldn't assert only be used in debug
> mode?

Yes, and eventually we will. Note that V8 assertions are already
compiled away in release builds.

> won't all these asserts hurt performance?

Not really. They're quite cheap with maybe one or two exceptions.

> as a result of the assert, i don't seem to be able to catch the error
> anywhere in uncaughtException handler or in SIG* handlers.

That's correct. Assertions are sanity checks, they check for internal
consistency. If an assertion triggers, it means the program is in an
undefined state. A C++ assertion that triggers is *always* a bug so
please report it.

billywhizz

unread,
Feb 8, 2012, 11:37:55 AM2/8/12
to nodejs
Thanks ben. is it also not recommended to compile with NDEBUG set
then? i'm only seeing this error because i am working directly with
the c++ bindings. in net.js it checks that we have a buffer before
making a call to handle.write so i don't think this is technically a
bug in node.js...

On Feb 8, 4:13 pm, Ben Noordhuis <i...@bnoordhuis.nl> wrote:
Reply all
Reply to author
Forward
0 new messages