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

**URL:** <https://discourse.julialang.org/t/errorexception-task-cannot-be-serialized-with-taskwrapper-struct/26617>\
**Category:** General Usage\
**Created:** [July 22, 2019, 2:40am UTC](https://discourse.julialang.org/t/errorexception-task-cannot-be-serialized-with-taskwrapper-struct/26617 "2019-07-22T02:40:06Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jasha10](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jasha10/32/7508_2.png) [@Jasha10](https://discourse.julialang.org/u/Jasha10)\
**Post date:** [July 22, 2019, 2:40am UTC](https://discourse.julialang.org/t/errorexception-task-cannot-be-serialized-with-taskwrapper-struct/26617/1 "2019-07-22T02:40:06Z")

</div>

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

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

```

My directory layout is simple:

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

```

See [https://github.com/Jasha10/TaskWrappers.jl](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:

```julia
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.

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [July 22, 2019, 2:51am UTC](https://discourse.julialang.org/t/errorexception-task-cannot-be-serialized-with-taskwrapper-struct/26617/2 "2019-07-22T02:51:12Z")

</div>

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).

---

<div class="post-metadata">

**Author:** ![Jasha10](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jasha10/32/7508_2.png) [@Jasha10](https://discourse.julialang.org/u/Jasha10)\
**Post date:** [July 22, 2019, 2:54am UTC](https://discourse.julialang.org/t/errorexception-task-cannot-be-serialized-with-taskwrapper-struct/26617/3 "2019-07-22T02:54:24Z")

</div>

Ok, thanks.

---

<div class="post-metadata">

**Author:** ![Jasha10](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jasha10/32/7508_2.png) [@Jasha10](https://discourse.julialang.org/u/Jasha10)\
**Post date:** [July 22, 2019, 3:39am UTC](https://discourse.julialang.org/t/errorexception-task-cannot-be-serialized-with-taskwrapper-struct/26617/4 "2019-07-22T03:39:49Z")

</div>

As far as I can tell from reading the [Modules](https://docs.julialang.org/en/v1/manual/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:

```julia
module TaskWrappers
struct TaskWrapper
    task::Task
end
function __init__ ()
    global t
    global example
    t = @async sleep(10)
    example = TaskWrapper(t)
end
end

```
