# Embedded Julia: Backslash hangs thread

**URL:** <https://discourse.julialang.org/t/embedded-julia-backslash-hangs-thread/4233>\
**Category:** General Usage\
**Tags:** dotnet, embedding\
**Created:** [June 12, 2017, 5:33pm UTC](https://discourse.julialang.org/t/embedded-julia-backslash-hangs-thread/4233 "2017-06-12T17:33:19Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sinfaen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sinfaen/32/1162_2.png) [@Sinfaen](https://discourse.julialang.org/u/Sinfaen)\
**Post date:** [June 12, 2017, 5:33pm UTC](https://discourse.julialang.org/t/embedded-julia-backslash-hangs-thread/4233/1 "2017-06-12T17:33:19Z")

</div>

I’m not entirely sure where the best place to put this question as I am new to this place and Julia in general. I’ve tried stack overflow but I think this is too specific for that.

I’ve embedded Julia into C# using the public C API. When I run the backslash command, execution stalls, and the process keeps consuming processor power. I’ve tried it both by running functions from imported modules and just running statements with `jl_eval_string`

Here are two examples of I can do that get’s it to hang.  
**Julia Module**  
function doStuff()  
a = [1 2;3 4];  
b = [4;6];  
c = a\b;  
end

**In C# main function**  
jl\_eval\_string(“a=[1 2;3 4];”);  
jl\_eval\_string(“b = [4;6];”);  
jl\_eval\_string(“c=a\b;”);

I tried running it in a try/catch block to see if that would help, but it didn’t. Running the statements in the REPL or with a script executed from the command line work just fine. I’m not entirely sure what the differences are between running Julia from the command line and from C#.

Does anyone have an idea about what is going on here? I’ve tried using other lin alg functions and they work just fine.

---

<div class="post-metadata">

**Author:** ![anon94023334](https://avatars.discourse-cdn.com/v4/letter/a/e274bd/32.png) [@anon94023334](https://discourse.julialang.org/u/anon94023334)\
**Post date:** [June 12, 2017, 5:36pm UTC](https://discourse.julialang.org/t/embedded-julia-backslash-hangs-thread/4233/2 "2017-06-12T17:36:49Z")

</div>

Just a guess, but do you need to escape the backslash in `\b` ? That’s a valid escape sequence for C / C++ strings.

Try, perhaps, `"c=a\\b"` instead?

Ref: [Escape sequences in C - Wikipedia](https://en.wikipedia.org/wiki/Escape_sequences_in_C#Table_of_escape_sequences)

---

<div class="post-metadata">

**Author:** ![Sinfaen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sinfaen/32/1162_2.png) [@Sinfaen](https://discourse.julialang.org/u/Sinfaen)\
**Post date:** [June 12, 2017, 5:39pm UTC](https://discourse.julialang.org/t/embedded-julia-backslash-hangs-thread/4233/3 "2017-06-12T17:39:39Z")

</div>

Oops, I wrote that in wrong. I’ve been using `jl_eval_string("c=a\\b;")`. I know that works because I’ve been able to change directories using the the double backslash.

Still doesn’t explain why it stops working in the Julia module though.

---

<div class="post-metadata">

**Author:** ![anon94023334](https://avatars.discourse-cdn.com/v4/letter/a/e274bd/32.png) [@anon94023334](https://discourse.julialang.org/u/anon94023334)\
**Post date:** [June 12, 2017, 5:42pm UTC](https://discourse.julialang.org/t/embedded-julia-backslash-hangs-thread/4233/4 "2017-06-12T17:42:16Z")

</div>

I can’t reproduce this on 0.6-rc3:

```julia
julia> include("mod1.jl")
Foo

julia> Foo.doStuff()
2-element Array{Float64,1}:
 -2.0
  3.0

```

`mod1.jl` is just your code wrapped with `module Foo ... end`.

---

<div class="post-metadata">

**Author:** ![ihnorton](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ihnorton/32/26_2.png) [@ihnorton](https://discourse.julialang.org/u/ihnorton)\
**Post date:** [June 12, 2017, 5:49pm UTC](https://discourse.julialang.org/t/embedded-julia-backslash-hangs-thread/4233/5 "2017-06-12T17:49:00Z")

</div>

I can’t reproduce from C on a tip-of-trunk build (on a mac). Please try from C. If you have a local build, comment out everything in `examples/embedding/embedding.c` between `jl_init();` and `int ret = 0;` replace with:

```
    jl_eval_string("a=[1 2;3 4];");
    jl_eval_string("b = [4;6];");
    jl_eval_string("c=a\\b;");
    jl_eval_string("@show c");

```

Then in the `examples` dir run:

> make  
> embedding/embedding

---

<div class="post-metadata">

**Author:** ![Sinfaen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sinfaen/32/1162_2.png) [@Sinfaen](https://discourse.julialang.org/u/Sinfaen)\
**Post date:** [June 12, 2017, 5:49pm UTC](https://discourse.julialang.org/t/embedded-julia-backslash-hangs-thread/4233/6 "2017-06-12T17:49:08Z")

</div>

It works when run in the REPL. However, it happens when it’s run from C#.

Here is a more complete version of my code that has been heavily cut (I have a lot of testing code, and some rewritten stuff from julia.h for the garbage collector)

```julia
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.InteropServices;
namespace TestCInCSharp{
    class Program{
        [DllImport("kernel32.dll", SetLastError = true)]
        static extern bool SetDllDirectory(string lpPathName);

        [DllImport("libjulia.dll", SetLastError = true)]
        public static extern void jl_init(string path);

        [DllImport("libjulia.dll", CallingConvention = CallingConvention.Cdecl)]
        public static extern IntPtr jl_eval_string(string input);
        //other dll imports here....
        //
        static unsafe void Main(string[] args){
            string p;
            string julia_path = @"C:\Users\schulk4\AppData\Local\Julia-0.5.2\bin";
            IntPtr module;
            SetDllDirectory(julia_path);
            jl_init(julia_path);
            //
            p = @"C:\\Users\\schulk4\\Documents\\Programming\\TestJuliaSim\\Assets\\test_julia.jl";
            p = "include(\"" + p + "\")";
            module = jl_eval_string(p); //holds module pointer? it appears to be so
            
            jl_eval_string("a=[1 2;3 4];");
            jl_eval_string("b=[4;6];");
            jl_eval_string("c=a\\b;");
         
            jl_atexit_hook(0); //tell Julia to shut down
            Console.ReadLine(); //pause so that output stays up
        }
    }
}

```

---

<div class="post-metadata">

**Author:** ![ihnorton](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ihnorton/32/26_2.png) [@ihnorton](https://discourse.julialang.org/u/ihnorton)\
**Post date:** [June 12, 2017, 5:51pm UTC](https://discourse.julialang.org/t/embedded-julia-backslash-hangs-thread/4233/7 "2017-06-12T17:51:12Z")

</div>

> [@Sinfaen](#):
>
> some rewritten stuff from julia.h for the garbage collector

I assume you are talking about this, which is almost certainly not going to work as-written, and is most likely the cause of the problem:

> <https://stackoverflow.com/questions/44442654/embedding-julia-into-c-garbage-collector-rewrite-into-c-issues-and-questions>

---

<div class="post-metadata">

**Author:** ![ihnorton](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ihnorton/32/26_2.png) [@ihnorton](https://discourse.julialang.org/u/ihnorton)\
**Post date:** [June 12, 2017, 5:52pm UTC](https://discourse.julialang.org/t/embedded-julia-backslash-hangs-thread/4233/8 "2017-06-12T17:52:17Z")

</div>

As I said in my comment there, it is probably possible _in principle_ but hooking in to pgc\_stack like that is asking for trouble and probably provides no benefit (especially not if as-written you are allocating on the managed heap rather than using alloca).

---

<div class="post-metadata">

**Author:** ![Sinfaen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sinfaen/32/1162_2.png) [@Sinfaen](https://discourse.julialang.org/u/Sinfaen)\
**Post date:** [June 12, 2017, 5:52pm UTC](https://discourse.julialang.org/t/embedded-julia-backslash-hangs-thread/4233/9 "2017-06-12T17:52:39Z")

</div>

Yes, but I stopped using the functions I wrote to try and see what was going wrong. That shouldn’t influence the code in the Julia function in the module though?

---

<div class="post-metadata">

**Author:** ![Sinfaen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sinfaen/32/1162_2.png) [@Sinfaen](https://discourse.julialang.org/u/Sinfaen)\
**Post date:** [June 12, 2017, 5:53pm UTC](https://discourse.julialang.org/t/embedded-julia-backslash-hangs-thread/4233/10 "2017-06-12T17:53:31Z")

</div>

I’ve decided that I’ll try and do it the way that Ipython does it with the global array of references, and see if that works

---

<div class="post-metadata">

**Author:** ![ihnorton](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ihnorton/32/26_2.png) [@ihnorton](https://discourse.julialang.org/u/ihnorton)\
**Post date:** [June 12, 2017, 5:56pm UTC](https://discourse.julialang.org/t/embedded-julia-backslash-hangs-thread/4233/11 "2017-06-12T17:56:30Z")

</div>

Ok, if you are not doing the GC manipulation right now then that’s fine, I wasn’t sure based on how the intro to your example was worded.

> Console.ReadLine(); //pause so that output stays up

Are you sure that work is actually happening? Try adding `jl_eval_string("@show c");` – maybe it’s actually finished.

---

<div class="post-metadata">

**Author:** ![ihnorton](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ihnorton/32/26_2.png) [@ihnorton](https://discourse.julialang.org/u/ihnorton)\
**Post date:** [June 12, 2017, 6:18pm UTC](https://discourse.julialang.org/t/embedded-julia-backslash-hangs-thread/4233/12 "2017-06-12T18:18:35Z")

</div>

I just tried both C and C# (Visual Studio 2015) on Windows 10 against 0.5.2 release binary and it worked fine.

---

<div class="post-metadata">

**Author:** ![Sinfaen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sinfaen/32/1162_2.png) [@Sinfaen](https://discourse.julialang.org/u/Sinfaen)\
**Post date:** [June 12, 2017, 6:23pm UTC](https://discourse.julialang.org/t/embedded-julia-backslash-hangs-thread/4233/13 "2017-06-12T18:23:41Z")

</div>

I made an entirely new project and copied only the things that I needed. I guess I’ll just work with this now. Thanks for your help

---

<div class="post-metadata">

**Author:** ![ihnorton](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ihnorton/32/26_2.png) [@ihnorton](https://discourse.julialang.org/u/ihnorton)\
**Post date:** [June 13, 2017, 2:29pm UTC](https://discourse.julialang.org/t/embedded-julia-backslash-hangs-thread/4233/14 "2017-06-13T14:29:13Z")

</div>

If there’s still an issue, please try some of the suggestions and report back – I’m happy to help debug further if I can find a way to reproduce it.
