# Task serialization error: Running a Task on a remote process fails with "cannot serialize a running Task"

**URL:** <https://discourse.julialang.org/t/task-serialization-error-running-a-task-on-a-remote-process-fails-with-cannot-serialize-a-running-task/72812>\
**Category:** General Usage\
**Tags:** parallel, multithreading\
**Created:** [December 9, 2021, 9:25am UTC](https://discourse.julialang.org/t/task-serialization-error-running-a-task-on-a-remote-process-fails-with-cannot-serialize-a-running-task/72812 "2021-12-09T09:25:49Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Bossax](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bossax/32/29872_2.png) [@Bossax](https://discourse.julialang.org/u/Bossax)\
**Post date:** [December 9, 2021, 9:25am UTC](https://discourse.julialang.org/t/task-serialization-error-running-a-task-on-a-remote-process-fails-with-cannot-serialize-a-running-task/72812/1 "2021-12-09T09:25:49Z")

</div>

Hi all,

I have written a routine to do image acquisition. This routine calls camera C APIs to take and save images. I tested it by running the routine on a local process and it works fine; I can send SIGINT to interrupt the loop and it quits nicely. However, when I try to send it to run on a worker process, it fails with an error message **cannot serialize a running Task**

Here is the routine code

```julia
function work(camera::Camera, remcam::RemoteCamera)
    Base.exit_on_sigint(false)
    # set acquisition mode
    set_acquisitionmode(camera, "Continuous")

    # attach shared arrays in a remote process
    img_array, imgTime_array = attach_remote_process()

    # begin acquisition
    start(camera)
    counter = 0
    
    try
        while true
            #get image
            img =
            try
                SpinnakerCameras.next_image(camera, 1)
            catch ex

                if (!isa(ex, SpinnakerCameras.CallError) ||
                    ex.code != SpinnakerCameras.SPINNAKER_ERR_TIMEOUT)
                    rethrow(ex)
                else
                    @warn "image corrupted"
                end
                nothing
            end
            
            if img.incomplete == 1 && img.status != 0
               @goto clear_img
            end

            counter +=1
            img_data = @view img.data[:,:]
            ts = img.timestamp
         
           # lock shared arrays and write data to the shared arrays
            wrlock(img_array,1.0) do
                copyto!(img_array, img_data)
            end

            wrlock(imgTime_array,1.0) do
                imgTime_array[1] = ts
            end

            # clear image handle 
            @label clear_img
                finalize(img)

        end
    catch e
        if e isa InterruptException
            
            @info "Acquisition loop is terminated"

            try
                finalize(img)
            catch e
                if !(e isa UndefVarError)
                    rethrow(e)
                end
            end

            stop(camera)
            return nothing
        else
            rethrow(e)
            return nothing
        end
    end
end

```

The arguments of the function are complex data types which contain a pointer to the camera device and other data containers.

This is how I run the routine on a remote process

```julia
using Distributed
addprocs(1)
remote_do(work,2,camera,remcam)

```

And this is the returned error

```julia
ERROR: cannot serialize a running Task
Stacktrace:
  [1] error(s::String)
    @ Base ./error.jl:33
  [2] serialize(s::Distributed.ClusterSerializer{Sockets.TCPSocket}, t::Task)
    @ Serialization /buildworker/worker/package_linux64/build/usr/share/julia/stdlib/v1.6/Serialization/src/Serialization.jl:445
  [3] serialize_any(s::Distributed.ClusterSerializer{Sockets.TCPSocket}, x::Any)
    @ Serialization /buildworker/worker/package_linux64/build/usr/share/julia/stdlib/v1.6/Serialization/src/Serialization.jl:657
  [4] serialize(s::Distributed.ClusterSerializer{Sockets.TCPSocket}, x::Any)
    @ Serialization /buildworker/worker/package_linux64/build/usr/share/julia/stdlib/v1.6/Serialization/src/Serialization.jl:636
  [5] serialize_any(s::Distributed.ClusterSerializer{Sockets.TCPSocket}, x::Any)
    @ Serialization /buildworker/worker/package_linux64/build/usr/share/julia/stdlib/v1.6/Serialization/src/Serialization.jl:657
  [6] serialize(s::Distributed.ClusterSerializer{Sockets.TCPSocket}, x::Any)
    @ Serialization /buildworker/worker/package_linux64/build/usr/share/julia/stdlib/v1.6/Serialization/src/Serialization.jl:636
  [7] serialize_any(s::Distributed.ClusterSerializer{Sockets.TCPSocket}, x::Any)
    @ Serialization /buildworker/worker/package_linux64/build/usr/share/julia/stdlib/v1.6/Serialization/src/Serialization.jl:657
  [8] serialize
    @ /buildworker/worker/package_linux64/build/usr/share/julia/stdlib/v1.6/Serialization/src/Serialization.jl:636 [inlined]
  [9] serialize(s::Distributed.ClusterSerializer{Sockets.TCPSocket}, t::Tuple{SpinnakerCameras.Camera, SpinnakerCameras.RemoteCamera{UInt8}})
    @ Serialization /buildworker/worker/package_linux64/build/usr/share/julia/stdlib/v1.6/Serialization/src/Serialization.jl:201
 [10] serialize_msg(s::Distributed.ClusterSerializer{Sockets.TCPSocket}, o::Distributed.RemoteDoMsg)
    @ Distributed /buildworker/worker/package_linux64/build/usr/share/julia/stdlib/v1.6/Distributed/src/messages.jl:78
 [11] #invokelatest#2
    @ ./essentials.jl:708 [inlined]
 [12] invokelatest
    @ ./essentials.jl:706 [inlined]
 [13] send_msg_(w::Distributed.Worker, header::Distributed.MsgHeader, msg::Distributed.RemoteDoMsg, now::Bool)
    @ Distributed /buildworker/worker/package_linux64/build/usr/share/julia/stdlib/v1.6/Distributed/src/messages.jl:174
 [14] send_msg
    @ /buildworker/worker/package_linux64/build/usr/share/julia/stdlib/v1.6/Distributed/src/messages.jl:122 [inlined]
 [15] remote_do(::Function, ::Distributed.Worker, ::SpinnakerCameras.Camera, ::Vararg{Any, N} where N; kwargs::Base.Iterators.Pairs{Union{}, Union{}, Tuple{}, NamedTuple{(), Tuple{}}})
    @ Distributed /buildworker/worker/package_linux64/build/usr/share/julia/stdlib/v1.6/Distributed/src/remotecall.jl:461
 [16] remote_do(::Function, ::Distributed.Worker, ::SpinnakerCameras.Camera, ::Vararg{Any, N} where N)
    @ Distributed /buildworker/worker/package_linux64/build/usr/share/julia/stdlib/v1.6/Distributed/src/remotecall.jl:461
 [17] remote_do(::Function, ::Int64, ::SpinnakerCameras.Camera, ::Vararg{Any, N} where N; kwargs::Base.Iterators.Pairs{Union{}, Union{}, Tuple{}, NamedTuple{(), Tuple{}}})
    @ Distributed /buildworker/worker/package_linux64/build/usr/share/julia/stdlib/v1.6/Distributed/src/remotecall.jl:486
 [18] remote_do(::Function, ::Int64, ::SpinnakerCameras.Camera, ::Vararg{Any, N} where N)
    @ Distributed /buildworker/worker/package_linux64/build/usr/share/julia/stdlib/v1.6/Distributed/src/remotecall.jl:486
 [19] top-level scope
    @ none:1

```

Can anyone explain what the error means?

Best,

Sitthichat

---

<div class="post-metadata">

**Author:** ![lawless-m](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lawless-m/32/30869_2.png) [@lawless-m](https://discourse.julialang.org/u/lawless-m)\
**Post date:** [December 9, 2021, 4:41pm UTC](https://discourse.julialang.org/t/task-serialization-error-running-a-task-on-a-remote-process-fails-with-cannot-serialize-a-running-task/72812/2 "2021-12-09T16:41:56Z")

</div>

Can you ssh into the same machine and run it inside a REPL there ?

Sometimes that error message means the remote process crashed

---

<div class="post-metadata">

**Author:** ![Bossax](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bossax/32/29872_2.png) [@Bossax](https://discourse.julialang.org/u/Bossax)\
**Post date:** [December 21, 2021, 4:59am UTC](https://discourse.julialang.org/t/task-serialization-error-running-a-task-on-a-remote-process-fails-with-cannot-serialize-a-running-task/72812/3 "2021-12-21T04:59:16Z")

</div>

I found out that it has to do with the data types that are passed to the function. The Camera and RemoteCamera types are composite types which are comprised of arrays, pointers, other composite types. When I pass only fields of the composite types; eg. passing only a pointer of the camera device within the Camera type, this error is solved.
