ErrorException("Task cannot be serialized") with TaskWrapper struct

I’ve got a TaskWrappers.jl package whose source file looks like this.

module TaskWrappers
struct TaskWrapper
    task::Task
end
t = @async sleep(10)
example = TaskWrapper(t)
end

My directory layout is simple:

|- Project.toml
|- src/
    |- TaskWrappers.jl

See https://github.com/Jasha10/TaskWrappers.jl for reference.

There is no problem at all if I run the following from the command line:
julia -ie 'include("src/TaskWrappers.jl"); using .TaskWrappers'

However, I get major issues if I run this command:
julia --project -ie 'using TaskWrappers'

The program crashes horribly:

Jashas-MBP-2:TaskWrappers.jl lappy486$ julia --project -ie 'using TaskWrappers'
[ Info: Precompiling TaskWrappers [8aa0bef4-ac27-11e9-2b40-fd2da0855edc]
fatal: error thrown and no exception handler available.
ErrorException("Task cannot be serialized")
rec_backtrace at /Users/julia/buildbot/worker/package_macos64/build/src/stackwalk.c:94
record_backtrace at /Users/julia/buildbot/worker/package_macos64/build/src/task.c:217
jl_throw at /Users/julia/buildbot/worker/package_macos64/build/src/task.c:417
jl_error at /Users/julia/buildbot/worker/package_macos64/build/src/rtutils.c:41
jl_serialize_value_ at /Users/julia/buildbot/worker/package_macos64/build/src/dump.c:865
jl_serialize_module at /Users/julia/buildbot/worker/package_macos64/build/src/dump.c:427 [inlined]
jl_serialize_value_ at /Users/julia/buildbot/worker/package_macos64/build/src/dump.c:862
jl_serialize_value_ at /Users/julia/buildbot/worker/package_macos64/build/src/dump.c:663
jl_save_incremental at /Users/julia/buildbot/worker/package_macos64/build/src/dump.c:2706
jl_write_compiler_output at /Users/julia/buildbot/worker/package_macos64/build/src/precompile.c:66
jl_atexit_hook at /Users/julia/buildbot/worker/package_macos64/build/src/init.c:222
main at /Applications/Julia-1.1.app/Contents/Resources/julia/bin/julia (unknown line)
ERROR: Failed to precompile TaskWrappers [8aa0bef4-ac27-11e9-2b40-fd2da0855edc] to /Users/lappy486/.julia/compiled/v1.1/TaskWrappers/4gSkB.ji.
Stacktrace:
 [1] error(::String) at ./error.jl:33
 [2] compilecache(::Base.PkgId, ::String) at ./loading.jl:1197
 [3] _require(::Base.PkgId) at ./loading.jl:960
 [4] require(::Base.PkgId) at ./loading.jl:858
 [5] require(::Module, ::Symbol) at ./loading.jl:853

Anybody know what’s going on here?
I’m running Julia version 1.1.1 on macOS 10.14.5.

1 Like

You need to disable precompilation for that type of code in a package.

If you know that a module is not safe to precompile your module (for example, for one of the reasons described below), you should put __precompile__(false) in the module file (typically placed at the top).

1 Like

Ok, thanks.

As far as I can tell from reading the Modules section of the Julia docs, there are four options for avoiding this problem:

  1. Add __precompile__(false) at the top of the module,
  2. Use the --compiled-modules=no command line flag when starting julia,
  3. remove the t = @async sleep(10) and example = TaskWrapper(t) lines from the module, or
  4. wrap those lines in an __init__ function to obtain the following:
module TaskWrappers
struct TaskWrapper
    task::Task
end
function __init__()
    global t
    global example
    t = @async sleep(10)
    example = TaskWrapper(t)
end
end