You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Getting segmentation fault after receiving some callbacks from NAN layer #1543
I have written a native addon [Using NAN] for my cpp [pub-sub] library.
Subscriber that is receving the events from the publisher.
Calling the application callback.
So, my native addon is providing wrapper APIs for cpp library.
Js application will create subscriber object and will subscribe for event. Internally I am creating c++ subscriber, subscribe to it and will register addon callback to cpp layer.
I am using libuv for getting the asyn callback from c++ layer to addon layer, and then I am calling NAN callback for calling js callback.
For callback implementation in addon, I followed: #1171
Issue:
After receving some events on subscriber side, subscriber sample is crashing [Segmentation fault].
Crash logs:
(gdb) bt
#0 0x0000000000f87c74 in v8::internal::JSReceiver::GetCreationContext() ()
#1 0x0000000000afd25c in v8::Object::CreationContext() ()
#2 0x0000000001225044 in node::MakeCallback(v8::Isolate*, v8::Local<v8::Object>, v8::Local<v8::Function>, int, v8::Local<v8::Value>*) ()
#3 0x00007ffff47a539b in npubsub::NSubscriber::uvAsynCallback(uv_async_s*) ()
from /home/jack/pubsubrepo/build/Release/pubsub.node
#4 0x000000000143e44b in uv__async_io (loop=0x21d0500 <default_loop_struct>, w=<optimized out>, events=<optimized out>) at ../deps/uv/src/unix/async.c:118
#5 0x000000000144ffa8 in uv__io_poll (loop=loop@entry=0x21d0500 <default_loop_struct>, timeout=-1) at ../deps/uv/src/unix/linux-core.c:400
#6 0x000000000143edd6 in uv_run (loop=0x21d0500 <default_loop_struct>, mode=UV_RUN_DEFAULT) at ../deps/uv/src/unix/core.c:368
#7 0x000000000122c1bd in node::Start(uv_loop_s*, int, char const* const*, int, char const* const*) ()
#8 0x0000000001224d03 in node::Start(int, char**) ()
#9 0x00007ffff6bd8f45 in __libc_start_main (main=0x8acef0 <main>, argc=8, argv=0x7fffffffe328, init=<optimized out>, fini=<optimized out>,
rtld_fini=<optimized out>, stack_end=0x7fffffffe318) at libc-start.c:287
Observation:
There is a strange observation on my subscriber sample:
If, I am creating my subscriber object in sample application like this:
subscriber = new subscriber(ip, port);
Result: It is working fine.
If, I am creating the subscriber object like this
var subscriber = new subscriber(ip, port);
Result: It is crashing after receiving some events.
Environment details:
Node.js Version: v8.11.1
OS: Linux [Ubuntu : 14.04]
I am struggling with this issue since quite long. Any help will be appreciated.
hello @jay11ca39 - how are you, nice to see you after some time!
Like last time, I am suspecting this has to do with one or more of: object scope, access or life cycle. Can you please provide your code of npubsub::NSubscriber::uvAsynCallback as well as illustrate the pub-sub cycle with threads in view?
typedef struct
{
NSubscriber* nSubscriber; // Class object needs for callback as Application callback is stored in this class
Message *message;
std::string topic;
} SubData;
I am calling uvAsynCallback from the callback that I received from the native layer as follows:
uv_async_send(&(obj->uvAsyncHandle)); //In this handle's data I filled the subData.
Regarding the pub-sub model:
JS Publisher that is publishing message.
JS subscriber that is receiving the message.
In Core CPP logic subscriber is having a thread that receive the message and call the application callback [In my case , cpp layer will call addon callback and from adddon callback I will call uv_async_send(&(obj->uvAsyncHandle)) which will call uvAsynCallback which will finally call the js callback.]
wherein the jsCallback field (a v8::Function object) is not created in the current context that is prevalent in the JS execution context. If so, we want a new instance of this function created in the current execution context.
gyp info it worked if it ends with ok
gyp info using node-gyp@3.8.0
gyp info using node@8.9.4 | linux | x64
gyp info ok
gyp info it worked if it ends with ok
gyp info using node-gyp@3.8.0
gyp info using node@8.9.4 | linux | x64
gyp info spawn /usr/bin/python2
gyp info spawn args [ '/home/jack/pubsubrepo/node_modules/node-gyp/gyp/gyp_main.py',
gyp info spawn args 'binding.gyp',
gyp info spawn args '-f',
gyp info spawn args 'make',
gyp info spawn args '-I',
gyp info spawn args '/home/jack/pubsubrepo/build/config.gypi',
gyp info spawn args '-I',
gyp info spawn args '/home/jack/pubsubrepo/node_modules/node-gyp/addon.gypi',
gyp info spawn args '-I',
gyp info spawn args '/home/jack/.node-gyp/8.9.4/include/node/common.gypi',
gyp info spawn args '-Dlibrary=shared_library',
gyp info spawn args '-Dvisibility=default',
gyp info spawn args '-Dnode_root_dir=/home/jack/.node-gyp/8.9.4',
gyp info spawn args '-Dnode_gyp_dir=/home/jack/pubsubrepo/node_modules/node-gyp',
gyp info spawn args '-Dnode_lib_file=/home/jack/.node-gyp/8.9.4/<(target_arch)/node.lib',
gyp info spawn args '-Dmodule_root_dir=/home/jack/pubsubrepo/',
gyp info spawn args '-Dnode_engine=v8',
gyp info spawn args '--depth=.',
gyp info spawn args '--no-parallel',
gyp info spawn args '--generator-output',
gyp info spawn args 'build',
gyp info spawn args '-Goutput_dir=.' ]
gyp info ok
gyp info it worked if it ends with ok
gyp info using node-gyp@3.8.0
gyp info using node@8.9.4 | linux | x64
gyp info spawn make
gyp info spawn args [ 'BUILDTYPE=Release', '-C', 'build' ]
make: Entering directory `/home/jack/pubsubrepo/build'
CXX(target) Release/obj.target/npubsub/src/nmessage.o
CXX(target) Release/obj.target/npubsub/src/npublisher.o
CXX(target) Release/obj.target/npubsub/src/nsubscriber.o
../src/nsubscriber.cc: In static member function ‘static void npubsub::NSubscriber::uvAsynCallback(uv_async_t*)’:
../src/nsubscriber.cc:119:63: warning: ‘v8::Localv8::Value Nan::Callback::Call(int, v8::Localv8::Value*) const’ is deprecated [-Wdeprecated-declarations]
((data ->nSubscriber)->jsSubCallback).Call(1, argv);
^
In file included from ../src/nsubscriber.h:23:0,
from ../src/nsubscriber.cc:20:
../node_modules/nan/nan.h:1647:3: note: declared here
Call(int argc, v8::Localv8::Value argv[]) const {
^
SOLINK_MODULE(target) Release/obj.target/pubsub.node
COPY Release/pubsub.node
make: Leaving directory `/home/jack/pubsubrepo/build'
gyp info ok
Hello @addaleax and @gireeshpunathil ,
As I shared earlier, there is a strange observation on my subscriber sample:
If, I am creating my subscriber object in sample application like this:
subscriber = new subscriber(ip, port);
Result: It is working fine.
If, I am creating the subscriber object like this
var subscriber = new subscriber(ip, port);
Result: It is crashing after receiving some events.
Here, what I am suspecting is : garbage collector is collecting subscriber object for destroying in 2nd case because I do not have any
code that is using the subscriber object after creating in sample application.
Activity
jay11ca39 commentedon Oct 11, 2018
Full backtrace:
jay11ca39 commentedon Oct 15, 2018
Hello @gireeshpunathil ,
Can you please provide your opinion for this issue.
It will be a great help.
gireeshpunathil commentedon Oct 15, 2018
hello @jay11ca39 - how are you, nice to see you after some time!
Like last time, I am suspecting this has to do with one or more of: object scope, access or life cycle. Can you please provide your code of
npubsub::NSubscriber::uvAsynCallbackas well as illustrate the pub-sub cycle with threads in view?jay11ca39 commentedon Oct 15, 2018
Hi @gireeshpunathil ,
Following is the uv callback:
I am calling uvAsynCallback from the callback that I received from the native layer as follows:
uv_async_send(&(obj->uvAsyncHandle)); //In this handle's data I filled the subData.
Regarding the pub-sub model:
In Core CPP logic subscriber is having a thread that receive the message and call the application callback [In my case , cpp layer will call addon callback and from adddon callback I will call uv_async_send(&(obj->uvAsyncHandle)) which will call uvAsynCallback which will finally call the js callback.]
jay11ca39 commentedon Oct 16, 2018
Hi @gireeshpunathil ,
Can you please let me know, if any other information is needed.
gireeshpunathil commentedon Oct 18, 2018
I guess the failure is coming from:
((data ->nSubscriber)->jsCallback).Call(2, argv);wherein the
jsCallbackfield (av8::Functionobject) is not created in the currentcontextthat is prevalent in the JS execution context. If so, we want a new instance of this function created in the current execution context./cc @addaleax for an expert opinion.
jay11ca39 commentedon Oct 18, 2018
Yes @gireeshpunathil ,
You are right the issue is created by the line:
((data ->nSubscriber)->jsCallback).Call(2, argv);When I commented this line, It was working fine.
Hello @addaleax
Can you please provide you opinion on this.
addaleax commentedon Oct 18, 2018
nodejs/nan#813 is probably unrelated, but might be worth looking at.
My guess is that
jsCallbackis not properly initialized in some way. A stack trace for anode-gyp rebuild --debugbuild might help?jay11ca39 commentedon Oct 22, 2018
Hello @addaleax ,
Following are the logs:
gyp info it worked if it ends with ok
gyp info using node-gyp@3.8.0
gyp info using node@8.9.4 | linux | x64
gyp info ok
gyp info it worked if it ends with ok
gyp info using node-gyp@3.8.0
gyp info using node@8.9.4 | linux | x64
gyp info spawn /usr/bin/python2
gyp info spawn args [ '/home/jack/pubsubrepo/node_modules/node-gyp/gyp/gyp_main.py',
gyp info spawn args 'binding.gyp',
gyp info spawn args '-f',
gyp info spawn args 'make',
gyp info spawn args '-I',
gyp info spawn args '/home/jack/pubsubrepo/build/config.gypi',
gyp info spawn args '-I',
gyp info spawn args '/home/jack/pubsubrepo/node_modules/node-gyp/addon.gypi',
gyp info spawn args '-I',
gyp info spawn args '/home/jack/.node-gyp/8.9.4/include/node/common.gypi',
gyp info spawn args '-Dlibrary=shared_library',
gyp info spawn args '-Dvisibility=default',
gyp info spawn args '-Dnode_root_dir=/home/jack/.node-gyp/8.9.4',
gyp info spawn args '-Dnode_gyp_dir=/home/jack/pubsubrepo/node_modules/node-gyp',
gyp info spawn args '-Dnode_lib_file=/home/jack/.node-gyp/8.9.4/<(target_arch)/node.lib',
gyp info spawn args '-Dmodule_root_dir=/home/jack/pubsubrepo/',
gyp info spawn args '-Dnode_engine=v8',
gyp info spawn args '--depth=.',
gyp info spawn args '--no-parallel',
gyp info spawn args '--generator-output',
gyp info spawn args 'build',
gyp info spawn args '-Goutput_dir=.' ]
gyp info ok
gyp info it worked if it ends with ok
gyp info using node-gyp@3.8.0
gyp info using node@8.9.4 | linux | x64
gyp info spawn make
gyp info spawn args [ 'BUILDTYPE=Release', '-C', 'build' ]
make: Entering directory `/home/jack/pubsubrepo/build'
CXX(target) Release/obj.target/npubsub/src/nmessage.o
CXX(target) Release/obj.target/npubsub/src/npublisher.o
CXX(target) Release/obj.target/npubsub/src/nsubscriber.o
../src/nsubscriber.cc: In static member function ‘static void npubsub::NSubscriber::uvAsynCallback(uv_async_t*)’:
../src/nsubscriber.cc:119:63: warning: ‘v8::Localv8::Value Nan::Callback::Call(int, v8::Localv8::Value*) const’ is deprecated [-Wdeprecated-declarations]
((data ->nSubscriber)->jsSubCallback).Call(1, argv);
^
In file included from ../src/nsubscriber.h:23:0,
from ../src/nsubscriber.cc:20:
../node_modules/nan/nan.h:1647:3: note: declared here
Call(int argc, v8::Localv8::Value argv[]) const {
^
SOLINK_MODULE(target) Release/obj.target/pubsub.node
COPY Release/pubsub.node
make: Leaving directory `/home/jack/pubsubrepo/build'
gyp info ok
jay11ca39 commentedon Oct 23, 2018
Hello @addaleax and @gireeshpunathil ,
Please let me know, if any additional information is required.
jay11ca39 commentedon Oct 25, 2018
Hello @addaleax and @gireeshpunathil ,
Previously I was using:
((data ->nSubscriber)->jsCallback).Call(2, argv);Now, I used:
Nan::Call(((data ->nSubscriber)->jsSubCallback), Nan::GetCurrentContext()->Global(), 2, argv);But, still it is giving the same crash.. :(
Can you please help me in this.?
gireeshpunathil commentedon Oct 25, 2018
@jay11ca39 - if I understand correctly, @addaleax 's suggestion was to:
node-gyp rebuild --debugand then run to crash with
ulimit -c unlimited, and launch the core in gdb, collect the stack trace.will you please follow that and get the stack?
jay11ca39 commentedon Oct 25, 2018
Hello @gireeshpunathil ,
Following I have followed:
Built the native addon with: node-gyp rebuild --debug
Ran my sample application:
$ gdb node subscriber.js
Following is the output of $ bt full
jay11ca39 commentedon Oct 25, 2018
Following are my software version:
jay11ca39 commentedon Oct 26, 2018
Hello @addaleax and @gireeshpunathil ,
As I shared earlier, there is a strange observation on my subscriber sample:
subscriber = new subscriber(ip, port);Result: It is working fine.
var subscriber = new subscriber(ip, port);Result: It is crashing after receiving some events.
Here, what I am suspecting is :
garbage collector is collecting subscriber object for destroying in 2nd case because I do not have any
code that is using the subscriber object after creating in sample application.
Can you please provide your opinion on this ?
jay11ca39 commentedon Nov 13, 2018
Hello @gireeshpunathil ,
Any comment on my observation ?
gireeshpunathil commentedon Jun 26, 2019
@jay11ca39 - is this resolved?
gireeshpunathil commentedon Jul 15, 2019
inactive, closing