<div dir="ltr">I just realized I never got back on this.<div><br></div><div>Thank you for your help :-)</div><div><br></div><div>I ended up re-writing it using a TDD approach, only took a few hours to get everything working again</div>
<div>/Fredrik</div><div><br></div></div><div class="gmail_extra"><br><br><div class="gmail_quote">On Tue, Apr 30, 2013 at 6:13 AM, Greg Burd <span dir="ltr"><<a href="mailto:greg@basho.com" target="_blank">greg@basho.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Are you trying to build a NIF where the function is run on a<br>
non-Erlang scheduler thread (async, via some thread pool)? If so,<br>
here's a method for doing it.<br>
<br>
<a href="https://gist.github.com/gburd/4121795" target="_blank">https://gist.github.com/gburd/4121795</a><br>
<br>
It took a while to get this just right, reuse and enjoy. I'll update<br>
the nifwait (<a href="https://github.com/basho/nifwait" target="_blank">https://github.com/basho/nifwait</a>) repo with example code.<br>
The best example of how to use it is in the wterl<br>
(<a href="https://github.com/basho-labs/wterl" target="_blank">https://github.com/basho-labs/wterl</a>) repo.<br>
<br>
-greg<br>
<br>
@gregburd | Basho Technologies | Riak | <a href="http://basho.com" target="_blank">http://basho.com</a> | @basho<br>
<div class="HOEnZb"><div class="h5"><br>
<br>
On Mon, Apr 29, 2013 at 2:57 PM, Fredrik Linder <<a href="mailto:fredrik@machinezone.com">fredrik@machinezone.com</a>> wrote:<br>
> Found one error (yay!) -- enif_open_resource_type should have NULL as the<br>
> module argument (missed this one in the docs)<br>
><br>
> Running with cerl does not generate a segfault, nor any (extra) printouts<br>
> Running with erl still generate segfault<br>
><br>
> Now I get either of the following (using erl), does it reveal what my error<br>
> is?<br>
><br>
>> USE_GDB=1 rebar eunit<br>
> [cut]<br>
> Program received signal SIGSEGV, Segmentation fault.<br>
> [Switching to Thread 0x2aaaad6c0700 (LWP 6384)]<br>
> 0x000000000052b28a in process_main () at<br>
> x86_64-unknown-linux-gnu/opt/smp/beam_hot.h:979<br>
> 979 MoveDeallocateReturn(xb(tmp_packed1&BEAM_LOOSE_MASK), r(0),<br>
> Qb((tmp_packed1>>BEAM_LOOSE_SHIFT)));<br>
> (gdb) backtrace<br>
> #0 0x000000000052b28a in process_main () at<br>
> x86_64-unknown-linux-gnu/opt/smp/beam_hot.h:979<br>
> #1 0x0000000000491463 in sched_thread_func (vesdp=0x2aaaac343ac0) at<br>
> beam/erl_process.c:5632<br>
> #2 0x0000000000590440 in thr_wrapper (vtwd=0x7fffffffd850) at<br>
> pthread/ethread.c:106<br>
> #3 0x00002aaaab3ffe9a in start_thread (arg=0x2aaaad6c0700) at<br>
> pthread_create.c:308<br>
> #4 0x00002aaaab911cbd in clone () at<br>
> ../sysdeps/unix/sysv/linux/x86_64/clone.S:112<br>
> #5 0x0000000000000000 in ?? ()<br>
><br>
> or<br>
><br>
> Program received signal SIGSEGV, Segmentation fault.<br>
> [Switching to Thread 0x2aaaad6c0700 (LWP 6914)]<br>
> unlink_free_block (allctr=0x85e5c0, block=0x0, flags=0) at<br>
> beam/erl_goodfit_alloc.c:458<br>
> 458 Uint sz = MBC_FBLK_SZ(&blk->block_head);<br>
> (gdb) backtrace<br>
> #0 unlink_free_block (allctr=0x85e5c0, block=0x0, flags=0) at<br>
> beam/erl_goodfit_alloc.c:458<br>
> #1 0x0000000000444ee1 in get_free_block (allctr=0x85e5c0, size=<optimized<br>
> out>, cand_blk=0x0, cand_size=0, flags=0) at beam/erl_goodfit_alloc.c:426<br>
> #2 0x000000000043b6da in mbc_alloc_block (alcu_flgsp=<synthetic pointer>,<br>
> blk_szp=<synthetic pointer>, size=<optimized out>, allctr=0x85e5c0) at<br>
> beam/erl_alloc_util.c:1309<br>
> #3 mbc_alloc (allctr=0x85e5c0, size=<optimized out>) at<br>
> beam/erl_alloc_util.c:1451<br>
> #4 0x0000000000440d7b in do_erts_alcu_alloc (size=32, extra=0x85e5c0,<br>
> type=148) at beam/erl_alloc_util.c:3530<br>
> #5 erts_alcu_alloc_thr_pref (type=148, extra=<optimized out>,<br>
> size=<optimized out>) at beam/erl_alloc_util.c:3607<br>
> #6 0x0000000000490595 in erts_alloc (size=32, type=18967) at<br>
> beam/erl_alloc.h:208<br>
> #7 new_message_buffer (size=0) at beam/erl_message.c:72<br>
> #8 erts_alloc_message_heap_state (statep=0x2aaaad6bfc4c,<br>
> receiver_locks=0x2aaaad6bfcb0, receiver=0x2aaaac9c1db8, ohpp=<synthetic<br>
> pointer>, bpp=<synthetic pointer>, size=0) at beam/global.h:1017<br>
> #9 erts_send_message (sender=0x2aaaac9c2470, receiver=0x2aaaac9c1db8,<br>
> receiver_locks=0x2aaaad6bfcb0, message=564171, flags=<optimized out>) at<br>
> beam/erl_message.c:1039<br>
> #10 0x0000000000476350 in do_send (p=0x2aaaac9c2470, to=793099, msg=564171,<br>
> suspend=1, refp=<optimized out>) at beam/bif.c:2025<br>
> #11 0x0000000000476bf0 in erl_send (p=0x2aaaac9c2470, to=793099, msg=564171)<br>
> at beam/bif.c:2138<br>
> #12 0x000000000052d1d8 in process_main () at beam/beam_emu.c:2558<br>
> #13 0x0000000000491463 in sched_thread_func (vesdp=0x2aaaac343ac0) at<br>
> beam/erl_process.c:5632<br>
> #14 0x0000000000590440 in thr_wrapper (vtwd=0x7fffffffd850) at<br>
> pthread/ethread.c:106<br>
> #15 0x00002aaaab3ffe9a in start_thread (arg=0x2aaaad6c0700) at<br>
> pthread_create.c:308<br>
> #16 0x00002aaaab911cbd in clone () at<br>
> ../sysdeps/unix/sysv/linux/x86_64/clone.S:112<br>
> #17 0x0000000000000000 in ?? ()<br>
><br>
><br>
> /Fredrik<br>
><br>
><br>
><br>
><br>
><br>
><br>
> On Mon, Apr 29, 2013 at 2:58 AM, Sverker Eriksson<br>
> <<a href="mailto:sverker.eriksson@ericsson.com">sverker.eriksson@ericsson.com</a>> wrote:<br>
>><br>
>> Looks like some sort of memory corruption.<br>
>><br>
>> Run on debug emulator and hope for a better (earlier) crash.<br>
>><br>
>> # cd $ERL_TOP/erts/emulator<br>
>> # make TYPE=debug smp plain<br>
>> # $ERL_TOP/bin/cerl -debug<br>
>><br>
>> /Sverker, Erlang/OTP Ericsson<br>
>><br>
>> <a href="mailto:fredrik@machinezone.com">fredrik@machinezone.com</a> wrote:<br>
>>><br>
>>> Hello folks,<br>
>>><br>
>>> I'm having difficulties locating the cause of a segfault I'm getting when<br>
>>> running tests with a NIF implementation I have.<br>
>>><br>
>>> Anything that would shed light on what's wrong is appreciated.<br>
>>><br>
>>> The backtrace does not seem to have anything to do with my NIF. The<br>
>>> NIF:ed function is called from a few different processes, and it forwards<br>
>>> the call to a worker thread which sends a message back to the caller<br>
>>> process.<br>
>>><br>
>>><br>
>>>><br>
>>>> strings -a<br>
>>><br>
>>><br>
>>> ../../otp/R16B/pre-5.10.1-mz-0.2/lib/erlang/erts-5.10.1/bin/beam.smp|fgrep<br>
>>> GCC|sort -u<br>
>>> GCC: (Ubuntu/Linaro 4.6.3-1ubuntu5) 4.6.3<br>
>>><br>
>>><br>
>>>><br>
>>>> USE_GDB=1 rebar eunit<br>
>>>><br>
>>><br>
>>> [cut]<br>
>>> Program received signal SIGSEGV, Segmentation fault.<br>
>>> [Switching to Thread 0x2aaaef381700 (LWP 19162)]<br>
>>> unlink_free_block (allctr=0x85f240, block=0x0, flags=0) at<br>
>>> beam/erl_goodfit_alloc.c:458<br>
>>> 458 Uint sz = MBC_FBLK_SZ(&blk->block_head);<br>
>>> (gdb) backtrace<br>
>>> #0 unlink_free_block (allctr=0x85f240, block=0x0, flags=0) at<br>
>>> beam/erl_goodfit_alloc.c:458<br>
>>> #1 0x0000000000444ee1 in get_free_block (allctr=0x85f240,<br>
>>> size=<optimized out>, cand_blk=0x0, cand_size=0, flags=0) at<br>
>>> beam/erl_goodfit_alloc.c:426<br>
>>> #2 0x000000000043b6da in mbc_alloc_block (alcu_flgsp=<synthetic<br>
>>> pointer>, blk_szp=<synthetic pointer>, size=<optimized out>,<br>
>>> allctr=0x85f240) at beam/erl_alloc_util.c:1309<br>
>>> #3 mbc_alloc (allctr=0x85f240, size=<optimized out>) at<br>
>>> beam/erl_alloc_util.c:1451<br>
>>> #4 0x0000000000440d7b in do_erts_alcu_alloc (size=32, extra=0x85f240,<br>
>>> type=148) at beam/erl_alloc_util.c:3530<br>
>>> #5 erts_alcu_alloc_thr_pref (type=148, extra=<optimized out>,<br>
>>> size=<optimized out>) at beam/erl_alloc_util.c:3607<br>
>>> #6 0x0000000000490595 in erts_alloc (size=32, type=18967) at<br>
>>> beam/erl_alloc.h:208<br>
>>> #7 new_message_buffer (size=0) at beam/erl_message.c:72<br>
>>> #8 erts_alloc_message_heap_state (statep=0x2aaaef380c4c,<br>
>>> receiver_locks=0x2aaaef380cb0, receiver=0x2aaaac9ccac8, ohpp=<synthetic<br>
>>> pointer>, bpp=<synthetic pointer>, size=0) at beam/global.h:1017<br>
>>> #9 erts_send_message (sender=0x2aaaac9cd118, receiver=0x2aaaac9ccac8,<br>
>>> receiver_locks=0x2aaaef380cb0, message=513931, flags=<optimized out>) at<br>
>>> beam/erl_message.c:1039<br>
>>> #10 0x0000000000476350 in do_send (p=0x2aaaac9cd118, to=550027,<br>
>>> msg=513931, suspend=1, refp=<optimized out>) at beam/bif.c:2025<br>
>>> #11 0x0000000000476bf0 in erl_send (p=0x2aaaac9cd118, to=550027,<br>
>>> msg=513931) at beam/bif.c:2138<br>
>>> #12 0x000000000052d1d8 in process_main () at beam/beam_emu.c:2558<br>
>>> #13 0x0000000000491463 in sched_thread_func (vesdp=0x2aaaac34de40) at<br>
>>> beam/erl_process.c:5632<br>
>>> #14 0x0000000000590440 in thr_wrapper (vtwd=0x7fffffffd7e0) at<br>
>>> pthread/ethread.c:106<br>
>>> #15 0x00002aaaab3ffe9a in start_thread (arg=0x2aaaef381700) at<br>
>>> pthread_create.c:308<br>
>>> #16 0x00002aaaab911cbd in clone () at<br>
>>> ../sysdeps/unix/sysv/linux/x86_64/clone.S:112<br>
>>> #17 0x0000000000000000 in ?? ()<br>
>>> (gdb)<br>
>>><br>
>>><br>
>>> ------------------------------------------------------------------------<br>
>>><br>
>>> _______________________________________________<br>
>>> erlang-questions mailing list<br>
>>> <a href="mailto:erlang-questions@erlang.org">erlang-questions@erlang.org</a><br>
>>> <a href="http://erlang.org/mailman/listinfo/erlang-questions" target="_blank">http://erlang.org/mailman/listinfo/erlang-questions</a><br>
>>><br>
>><br>
>><br>
><br>
><br>
> _______________________________________________<br>
> erlang-questions mailing list<br>
> <a href="mailto:erlang-questions@erlang.org">erlang-questions@erlang.org</a><br>
> <a href="http://erlang.org/mailman/listinfo/erlang-questions" target="_blank">http://erlang.org/mailman/listinfo/erlang-questions</a><br>
><br>
</div></div></blockquote></div><br><br clear="all"><div><br></div>-- <br><div dir="ltr"><a href="http://whosawesome.com/" target="_blank">http://whosawesome.com/</a><br></div>
</div>