Skip to content
This repository has been archived by the owner on Feb 27, 2022. It is now read-only.

Commit

Permalink
Remove extra logging, put the name field back, and update README
Browse files Browse the repository at this point in the history
  • Loading branch information
isaacs committed Aug 19, 2010
1 parent e1f8051 commit ae57ba9
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 66 deletions.
35 changes: 0 additions & 35 deletions README
@@ -1,41 +1,6 @@
This is about the simplest reduction you can get for using eio_custom.

Unfortunately, it blows up on Solaris, and I can't seem to figure out why.

To run:

node-waf configure build && node test.js

Macs and Linux are like:

js 0 before
>>>DoSomethingAsync 1 2
>>>about to eio_custom, sr=6438512
>>>returning
>>>DoSomething 6438544,6438512
>>>sr pointer 6438512
>>>about to sleep
>>>read req, about to set result
>>>x 1
>>>y 2
>>>returning
>>>DoSomething_After 6438544
js 2016 null 3


But solaris is all:

js 0 before
>>>DoSomethingAsync 1 2
>>>about to eio_custom, sr=139274000
>>>returning
>>>DoSomething 139241808,1024
>>>sr pointer 1024
>>>about to sleep
>>>read req, about to set result
Segmentation Fault (core dumped)

Take a look again. Note that the req->data pointer is getting set from
some big number which corresponds to a valid memory location assigned
by malloc, to 1024, which isn't valid at all.

38 changes: 14 additions & 24 deletions nas.cc
Expand Up @@ -18,62 +18,52 @@ struct simple_request {
int x;
int y;
Persistent<Function> cb;
// maybe it matters to put the char[] last? not sure.
char name[1];
};

static Handle<Value> DoSomethingAsync (const Arguments& args) {
HandleScope scope;
const char *usage = "usage: doSomething(x, y, cb)";
if (args.Length() != 3) {
const char *usage = "usage: doSomething(x, y, name, cb)";
if (args.Length() != 4) {
return ThrowException(Exception::Error(String::New(usage)));
}
int x = args[0]->Int32Value();
int y = args[1]->Int32Value();
fprintf(stderr, " >>>DoSomethingAsync %d %d\n", x, y);
Local<Function> cb = Local<Function>::Cast(args[2]);
String::Utf8Value name(args[2]);
Local<Function> cb = Local<Function>::Cast(args[3]);

simple_request *sr = (simple_request *)
malloc(sizeof(struct simple_request) + 1);
malloc(sizeof(struct simple_request) + name.length() + 1);

sr->cb = Persistent<Function>::New(cb);
strncpy(sr->name, *name, name.length() + 1);
sr->x = x;
sr->y = y;

fprintf(stderr, " >>>about to eio_custom, sr=%d\n", sr);
eio_custom(DoSomething, EIO_PRI_DEFAULT, DoSomething_After, sr);
ev_ref(EV_DEFAULT_UC);
fprintf(stderr, " >>>returning\n");
return Undefined();
}

// this function happens on the thread pool
static int DoSomething (eio_req *req) {
fprintf(stderr, " >>>DoSomething %d,%d\n", req, req->data);
struct simple_request * sr = (struct simple_request *)req->data;
// note that this is always 1024 on Solaris, not some big number.
// It's like it loses the req->data somewhere along the way. ?
fprintf(stderr, " >>>sr pointer %d\n", sr);
fprintf(stderr, " >>>about to sleep\n");
sleep(2);
fprintf(stderr, " >>>read req, about to set result\n");
// just READING the data seems to blow it up.
fprintf(stderr, " >>>x %d\n", sr->x);
fprintf(stderr, " >>>y %d\n", sr->y);
// Why does this crash in Solaris?
sleep(2); // just to make it less pointless to be async.
req->result = sr->x + sr->y;
fprintf(stderr, " >>>returning\n");
return 0;
}

static int DoSomething_After (eio_req *req) {
fprintf(stderr, " >>>DoSomething_After %d\n", req);
HandleScope scope;
ev_unref(EV_DEFAULT_UC);
struct simple_request * sr = (struct simple_request *)req->data;
Local<Value> argv[2];
Local<Value> argv[3];
argv[0] = Local<Value>::New(Null());
argv[1] = Integer::New(req->result);
argv[2] = String::New(sr->name);
TryCatch try_catch;
sr->cb->Call(Context::GetCurrent()->Global(), 2, argv);
sr->cb->Call(Context::GetCurrent()->Global(), 3, argv);
if (try_catch.HasCaught()) {
FatalException(try_catch);
}
Expand Down
14 changes: 7 additions & 7 deletions test.js
@@ -1,11 +1,11 @@
var nas = require("./build/default/nas")
, start = Date.now()

console.log("js "+(Date.now() - start), "before")
nas.doSomething(1, 2, function (er, res) {
console.log("js "+(Date.now() - start), er, res)
console.log("js "+(Date.now() - start), "before hello")
nas.doSomething(1, 2, "hello", function (er, res, n) {
console.log("js "+(Date.now() - start), er, res, n)
})
console.log("js "+(Date.now() - start), "before goodbye")
nas.doSomething(3, 4, "goodbye", function (er, res, n) {
console.log("js "+(Date.now() - start), er, res, n)
})
// console.log(Date.now(), "before goodbye")
// nas.doSomething(3, 4, "goodbye", function (er, res, n) {
// console.log(Date.now(), er, res, n)
// })
1 change: 1 addition & 0 deletions wscript
Expand Up @@ -11,6 +11,7 @@ def configure(conf):

def build(bld):
obj = bld.new_task_gen("cxx", "shlib", "node_addon")
# without this, eio_custom doesn't keep a ref to the req->data
obj.cxxflags = ["-D_FILE_OFFSET_BITS=64", "-D_LARGEFILE_SOURCE"]
obj.target = "nas"
obj.source = "nas.cc"

0 comments on commit ae57ba9

Please sign in to comment.