ccall, C pointers and Multi-Core


I’m having a bit of trouble with parallelization and a C library. I’ll try to describe the issue as best as possible:

I have a certain mutable struct that stores pointers to memory allocated by C through a ccall. I can use certain functions on the struct that again call a C library and modify the pointers and so on.

Now, I initialize the struct (call it p) with @spawnat. I apply a function f to the struct on those workers with @spawnat N f§. (This function can take a few seconds and sets the pointer of a field [let’s call it F] from undef to memory allocated by C.) If I do this in the REPL manually for a certain worker then everything works just fine and as intended.

The problem occurs if try to generalize this. I define a wrapper function that initializes the struct on every worker on a variable called p. Then I have a second function that applies the function f from above to the variable p on every worker. This variable does exist, but the C pointers in the struct seem to be broken.

additional Information: I don’t return the pointer when someone tries to access the field but apply unsafe_wrap beforehand and return the result.

I receive this error when trying fetch(@spawnat 5 p.F):

 Error: Fatal error on process 5
│   exception =
│    IOError: write: bad address in system call argument (EFAULT)
│    Stacktrace:
│     [1] try_yieldto(::typeof(Base.ensure_rescheduled), ::Base.RefValue{Task}) at ./event.jl:196

Has someone an idea what may cause this behavior?

Okay, i figured it out. If I have 10 workers they are indexed from 2 to 11 and not 1 to 10. This messed up the scope of the variables.