planck

Planck ClojureScript REPL
mfikes 2016-06-26T01:50:53.000496Z

@johanatan: I’m finding it difficult to find clear documentation on the threading semantics of the different entities exposed by the JavaScriptCore API. I tried your code and I am also getting SEGV on the bit of code that makes a number. Empirically I can see that making a change like this makes the SEGVs go away:

params->ctx = JSGlobalContextCreateInGroup(JSContextGetGroup(ctx), NULL);
(where previously it was passing the JSContextRef to the thread). I’ve never seen a need to do this before, but perhaps you are right, and this aspect is indeed not thread safe, and it is exhibiting issues in this case.

johanatan 2016-06-26T01:52:07.000497Z

Strange. Pretty sure the number creation part was working

johanatan 2016-06-26T01:52:22.000498Z

Did you try uncommenting the return NULL?

johanatan 2016-06-26T01:53:17.000501Z

Also seemed like a pretty blanket statement from Apple regarding thread safety

mfikes 2016-06-26T01:54:15.000502Z

Yes. I found that if I sprinkled fprintf(stderr, “here”) calls in the code that it would also crash when creating the number. But it could survive that if I was looking at it via the debugger.

mfikes 2016-06-26T01:55:30.000503Z

I’m now curious if the idea of creating a new context for use in the thread leads to the overall API working. (It didn’t work for me, but I figure perhaps you don’t have it all hooked up yet for that code path.)

johanatan 2016-06-26T04:09:33.000505Z

Ahh yea the code definitely isn't complete

johanatan 2016-06-26T04:09:39.000506Z

Perhaps you're right

johanatan 2016-06-26T05:00:42.000507Z

Did the JSObjectMakeArray also succeed with your change?

johanatan 2016-06-26T05:00:50.000508Z

@mfikes: ^

mfikes 2016-06-26T05:01:15.000509Z

Yes. It did not crash at least.

johanatan 2016-06-26T05:01:50.000510Z

Hmm, in that case I think I just need to add the code that actually calls the callback and we'll be done here

johanatan 2016-06-26T05:02:21.000511Z

Btw, how did you discover JSGlobalContextCreateInGroup?

mfikes 2016-06-26T05:02:33.000512Z

Yeah, just hook it up. JSGlobalContextCreateInGroup may be creating a context that needs to be released / freed.

mfikes 2016-06-26T05:07:37.000513Z

@johanatan: This archive entry mentions it https://lists.webkit.org/pipermail/webkit-dev/2008-December/005915.html (But even that post is inconsistent with the comment’s you can see in the header file.)

mfikes 2016-06-26T05:08:42.000514Z

I went in there and then concluded that perhaps the right thing to do is create a separate JSContext for use with the other thread.

mfikes 2016-06-26T05:09:09.000515Z

Hence my comment that there is really no clear documentation on the thread-safety.

johanatan 2016-06-26T05:26:42.000516Z

Hmm interesting. Well here's to hoping it works. Should be able to try it out tomorrow evening

johanatan 2016-06-26T18:31:05.000518Z

@mfikes: My first attempt resulted in that stack trace ^. But after trying 5-10 times to reproduce, no luck.

johanatan 2016-06-26T18:31:19.000519Z

Hope it isn't a heisenbug

mfikes 2016-06-26T18:32:15.000520Z

Hate those 🙂

johanatan 2016-06-26T18:34:41.000521Z

Ya, weird thing is it happens on (require '[planck.shell]) which shouldn't be running any of this risky native stuff

johanatan 2016-06-26T18:55:21.000522Z

I remember seeing this somewhere but how do you cleanup memory for a JSStringRef (or other JSValue for that matter)?

johanatan 2016-06-26T18:55:30.000524Z

@mfikes: ^

johanatan 2016-06-26T18:55:52.000525Z

[for example, a JSStringRef obtained via c_string_to_value in jsc_utils.h/c].

johanatan 2016-06-26T18:57:09.000526Z

ahh, found it.

johanatan 2016-06-26T18:57:12.000527Z

JSStringRelease

johanatan 2016-06-26T19:03:18.000528Z

Well, it's getting further now but the SEGV still occurs. Now it's on the call to JSEvaluateScript

johanatan 2016-06-26T19:03:27.000529Z

cljs.user=> (require '[planck.shell])
nil
cljs.user=> (planck.shell/sh-async "ls" :dir "/Users/jonathan/Documents" :env {"blah" "blaz"} (fn [_] 9))
ASAN:DEADLYSIGNAL
=================================================================
==52643==ERROR: AddressSanitizer: SEGV on unknown address 0x001000000011 (pc 0x7fff8ee337e1 bp 0x700000428c40 sp 0x700000428c30 T18)
    #0 0x7fff8ee337e0 in WTF::String::isolatedCopy() const & (JavaScriptCore+0x37e0)
    #1 0x7fff8ee57861 in OpaqueJSString::string() const (JavaScriptCore+0x27861)
    #2 0x7fff8ee576b8 in JSEvaluateScript (JavaScriptCore+0x276b8)
    #3 0x105ac6844 in wait_for_child (planck+0x100040844)
    #4 0x105ac6924 in thread_proc (planck+0x100040924)
    #5 0x7fff9dbc499c in _pthread_body (libsystem_pthread.dylib+0x399c)
    #6 0x7fff9dbc4919 in _pthread_start (libsystem_pthread.dylib+0x3919)
    #7 0x7fff9dbc2350 in thread_start (libsystem_pthread.dylib+0x1350)
@mfikes: ^

johanatan 2016-06-26T19:03:51.000530Z

Looks like it panics trying to copy a WTF string. WTF?!?

johanatan 2016-06-26T19:23:24.000531Z

Hmm, the heisenbug is also hitting with frequency > 0 now.

johanatan 2016-06-26T19:26:31.000532Z

@mfikes: I've unfortunately got to run. If you (or anyone) wants to take a look, the code is here: https://github.com/johanatan/planck/commit/50156f326653bcec4f51f6c0332dd6adb4635b1b

mfikes 2016-06-26T19:26:42.000533Z

Cool

johanatan 2016-06-26T22:59:00.000537Z

@mfikes: were you able to try it? Perhaps the results are different on Linux

mfikes 2016-06-26T22:59:29.000538Z

@johanatan: No, I haven’t been able to.