Hi, First of all, on what platform are you seeing this SEGFAULTs?
Libuv is using posix threads for unixes ( https://github.com/joyent/libuv/blob/master/src/unix/thread.c#L70-73 ), and CriticalSection on windows ( https://github.com/joyent/libuv/blob/master/src/win/thread.c#L129-131 ). So if you suppose that failure is related to those routines - you're probably experiencing a kernel bug. However it's hard to tell what is exactly happen no your side, can you post backtraces or core dumps there? (`gdb --args node test.js` then `run` and `bt` once segfaulted). Also I've noticed that you ain't destroying mutex there: https://github.com/navaneeth/libvarnam-nodejs/blob/master/src/varnamjs.h#L24. I really recommend you to fix this, because it may be possible that your object gets garbage collected when a worker thread is running. If this is the case - you can fix it by adding Ref() call before starting work request and Unref() once it was completed. Cheers, Fedor. On Sat, Sep 1, 2012 at 10:31 PM, Navaneeth.K.N <navaneet...@gmail.com>wrote: > Hello, > > I have a C library and I am writing a non-blocking wrapper using Node JS. > I am using uv_work_queue to call C library function. Workflow will be like, > > 1- First function gets called on main thread. This one sets up a > WorkerData (a struct used for passing data to thread) instance with all > necessary information required to do the job. This object is wrapped inside > uv_work_t instance's "data" field. > 2 - Calls uv_queue_work (uv_default_loop(), ... ) > 3 - Each worker thread will acquire a mutex lock, get a work handle (C > library requires this handle for every call) from the queue (std::queue) > and removes the handle from queue. It releases the mutex after all these. > If no handles are available in the queue, a new one will be created. > 4 - C library will be called with the above handle. Once the work is done, > acquire mutex lock and the handle will be added back to the queue. > > I wrote some JS code to test the above functionality. It will be something > like, > > for(i = 0; i < 100; i ++) > { > mylib.process ("test", function (err, result) { > }); > } > > actual code is available here - > https://github.com/navaneeth/libvarnam-nodejs/blob/master/test.js > > Interestingly, this fails with random segmentation faults. I have seen > some segfaults at accessing the queue even though all the operations that > modify the queue are in critical section. I have also got assertion > failures inside libuv. node: ../deps/uv/src/unix/stream.c:489: uv__write: > Assertion `n == 0' failed. > > Some observations: > > * When I reduce the loop iterations to 20, all works good. > * When I add a delay in between calls, it works. > > I am wondering why this happens? It looks like when in a tight loop, > mutexes are not working correctly and allowing multiple threads to get into > the critical section. When these segfaults happening, I have seen queue's > size() returns some random values which is a clear indication of mutexes > failing. > > Code is avilable here - > https://github.com/navaneeth/libvarnam-nodejs/blob/master/src/varnamjs.cc. > Varnam::Transliterate() is the entry point. > > Any help would be great! > > -- > Thanks > Navaneeth > > -- > Job Board: http://jobs.nodejs.org/ > Posting guidelines: > https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines > You received this message because you are subscribed to the Google > Groups "nodejs" group. > To post to this group, send email to nodejs@googlegroups.com > To unsubscribe from this group, send email to > nodejs+unsubscr...@googlegroups.com > For more options, visit this group at > http://groups.google.com/group/nodejs?hl=en?hl=en > -- Job Board: http://jobs.nodejs.org/ Posting guidelines: https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines You received this message because you are subscribed to the Google Groups "nodejs" group. To post to this group, send email to nodejs@googlegroups.com To unsubscribe from this group, send email to nodejs+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/nodejs?hl=en?hl=en