[novice] SIGSEGV upon entering new top of the stack context

62 views
Skip to first unread message

Thomas Barusseau

unread,
Dec 1, 2016, 11:09:01 AM12/1/16
to v8-users
Hello,

I'm having a hard time making this piece of code work. Basically, I need to take a node.js function (in this case : `process.emit()`),
store it in a C++ class, and use it at runtime whenever a callback method is called.



Could anyone help me with that? There's no doubt my code has stupid things in it.
I've read a lot of documentation today but somehow I can't figure out some things and I'm stuck trying things without fully understanding
the big picture...

Thanks in advance.

Zac Hansen

unread,
Dec 1, 2016, 12:13:01 PM12/1/16
to v8-users
I don't know the exact answer ottomh, but make sure you're running against a debug build - the error messages are a little better.  Also, maybe try putting a handlescope before th eline that's crashing?

--Zac

Ben Noordhuis

unread,
Dec 1, 2016, 2:34:41 PM12/1/16
to v8-users
From your gist:

Local<Context> context = isolate->GetCurrentContext();
context->Enter(); // /!\ SIGSEGV HAPPENS HERE /!\

I bet that context.IsEmpty() is true. You could move this line up:

Local<Function> lpe = Local<Function>::New(isolate, process_emit);

And then you can get its context with lpe->CreationContext().

One last word of advice: conditionally creating a new isolate when
Isolate::GetCurrent() returns NULL is not going to work. Objects and
functions are intrinsically tied to the isolate that created them.

Thomas Barusseau

unread,
Dec 1, 2016, 3:55:35 PM12/1/16
to v8-users
Hello,

That makes sense, and I was pretty sure that just creating a new isolate was
clearly not the way to go. Since I'm dealing with Persistent objects, what should
I do, then? I read somewhere that if Isolate::GetCurrent() returned NULL, it was
because the current isolate was being already used, I guess by the running
node.js itself?

If so, I really need assistance, because I technically can't stop it (because other
modules are running on it and some are time-sensitive), and even if I could, I don't
see how I'd do that.

I can't test your suggestions right now but will try tomorrow and make a follow-up.

Thanks a lot!

Ben Noordhuis

unread,
Dec 1, 2016, 4:46:05 PM12/1/16
to v8-users
On Thu, Dec 1, 2016 at 9:55 PM, Thomas Barusseau
<baruss...@gmail.com> wrote:
> I read somewhere that if Isolate::GetCurrent() returned NULL, it was
> because the current isolate was being already used, I guess by the running
> node.js itself?

Node.js never unlocks the isolate while running so if
Isolate::GetCurrent() returns NULL, you either call it from a
different thread or at program exit when the VM has been shut down.

Thomas Barusseau

unread,
Dec 2, 2016, 3:43:45 AM12/2/16
to v8-users
Let's assume that I can't wait for the VM to shut down.
Do I have to fork the main node.js process itself? I suppose a simple fork in my
node addon won't do much good...

Thomas Barusseau

unread,
Dec 2, 2016, 3:52:24 AM12/2/16
to v8-users
So, anyway, I tried your fix. New errors/crashes arise. I believe it's because of the
structure of my code, but here's what I do now :

I put up the Local<Function> copying of the Persistent<Function> earlier, and tried
to get its context the way you told me to.
First of all, to copy it I had to define a HandleScope scope(isolate), else I had a 
"#Fatal error in v8::HandleScope::CreateHandle()
 # Cannot create a handle without a HandleScope"
error. So after I added the HandleScope right above the `Local<Function> lpe` definition,
now I have a segfault during the part where I get the context, from lpe,

Would be really thankful if you could help me further. Thanks in advance.

Ben Noordhuis

unread,
Dec 2, 2016, 4:25:18 AM12/2/16
to v8-users
On Fri, Dec 2, 2016 at 9:52 AM, Thomas Barusseau
<baruss...@gmail.com> wrote:
> So, anyway, I tried your fix. New errors/crashes arise. I believe it's
> because of the
> structure of my code, but here's what I do now :
>
> I put up the Local<Function> copying of the Persistent<Function> earlier,
> and tried
> to get its context the way you told me to.
> First of all, to copy it I had to define a HandleScope scope(isolate), else
> I had a
> "#Fatal error in v8::HandleScope::CreateHandle()
> # Cannot create a handle without a HandleScope"
> error. So after I added the HandleScope right above the `Local<Function>
> lpe` definition,
> now I have a segfault during the part where I get the context, from lpe,
> here is the stack trace :
> https://gist.github.com/anonymous/1d939e3e339243fc19baacf3066ffd12
>
> Would be really thankful if you could help me further. Thanks in advance.

I can tell from your stack trace that you are calling into V8 from a
different thread. That's not safe, use a mechanism like
uv_async_send() to message the main thread.

Thomas Barusseau

unread,
Dec 2, 2016, 4:33:27 AM12/2/16
to v8-u...@googlegroups.com
Thanks a lot for your help, I'll look into that!


--
--
v8-users mailing list
v8-u...@googlegroups.com
http://groups.google.com/group/v8-users
---
You received this message because you are subscribed to a topic in the Google Groups "v8-users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/v8-users/rdlZrw8xKhI/unsubscribe.
To unsubscribe from this group and all its topics, send an email to v8-users+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Thomas Barusseau

Thomas Barusseau

unread,
Dec 2, 2016, 5:18:45 AM12/2/16
to v8-users
Isn't there a simpler way, by the way, than using uv_async_send?

I've seen in the documentation that there are Lockers/Unlockers. From what I've
read they might be the things I need?
Also, I decided to just pass around the pointer to Isolate since it shouldn't change

However, as the code suggests, the execution is completely locked in the Local<Function>->Call().

Any idea why?

Ben Noordhuis

unread,
Dec 2, 2016, 5:50:59 AM12/2/16
to v8-users
On Fri, Dec 2, 2016 at 11:18 AM, Thomas Barusseau
<baruss...@gmail.com> wrote:
> Isn't there a simpler way, by the way, than using uv_async_send?

Maybe not the answer you're looking for but: don't use threads. :-)

> I've seen in the documentation that there are Lockers/Unlockers. From what
> I've
> read they might be the things I need?

Node.js never releases the lock while running so that won't really
work. You _might_ be able to make it work by releasing/acquiring the
lock in uv_prepare_t and uv_check_t callbacks but then you'll be deep
in uncharted territory.

> Also, I decided to just pass around the pointer to Isolate since it
> shouldn't change
> anyway. So now doing this :
> https://gist.github.com/anonymous/585d2e8577b9c8669e31c9844f3d41de
>
> However, as the code suggests, the execution is completely locked in the
> Local<Function>->Call().
>
> Any idea why?

You're violating thread safety. Hangs and crashes are to be expected.

Thomas Barusseau

unread,
Dec 2, 2016, 5:56:01 AM12/2/16
to v8-u...@googlegroups.com
I see! Thanks a lot for all the help :)

--
--
v8-users mailing list
v8-u...@googlegroups.com
http://groups.google.com/group/v8-users
---
You received this message because you are subscribed to a topic in the Google Groups "v8-users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/v8-users/rdlZrw8xKhI/unsubscribe.
To unsubscribe from this group and all its topics, send an email to v8-users+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Thomas Barusseau
Reply all
Reply to author
Forward
0 new messages