# Strange error when enabling multiple threads

**URL:** <https://discourse.julialang.org/t/strange-error-when-enabling-multiple-threads/72208>\
**Category:** General Usage\
**Tags:** multithreading\
**Created:** [November 28, 2021, 6:50pm UTC](https://discourse.julialang.org/t/strange-error-when-enabling-multiple-threads/72208 "2021-11-28T18:50:43Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![ufechner7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ufechner7/32/51363_2.png) [@ufechner7](https://discourse.julialang.org/u/ufechner7)\
**Post date:** [November 28, 2021, 6:50pm UTC](https://discourse.julialang.org/t/strange-error-when-enabling-multiple-threads/72208/1 "2021-11-28T18:50:43Z")

</div>

I have some code that fails with " ERROR: UndefVarError: T not defined" when I run julia with multiple threads and works fine otherwise.  
The complete details can be found in [https://github.com/invenia/Impute.jl/issues/124](https://github.com/invenia/Impute.jl/issues/124) , but I am not so sure any longer if this is a bug in the package Impute or in Julia itself.

The error does NOT happen when I launch Julia with the command:  
julia --project  
It DOES happen when I launch it with the command:  
julia -t auto --project

Any hints?

The problem does not appear with small data sets, only with large data sets. I am providing the complete data set needed to reproduce the bug in a gist .

---

<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:** [November 28, 2021, 6:59pm UTC](https://discourse.julialang.org/t/strange-error-when-enabling-multiple-threads/72208/2 "2021-11-28T18:59:56Z")

</div>

Just looks like a bug in the package.

---

<div class="post-metadata">

**Author:** ![ufechner7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ufechner7/32/51363_2.png) [@ufechner7](https://discourse.julialang.org/u/ufechner7)\
**Post date:** [November 28, 2021, 7:34pm UTC](https://discourse.julialang.org/t/strange-error-when-enabling-multiple-threads/72208/3 "2021-11-28T19:34:13Z")

</div>

Question: Is it possible to change the number of threads that Julia uses at runtime?

If that would be possible I could reduce the number of threads before calling the problematic function to one and increase them afterwards.

Or is there another way to force a function to use only one thread?

---

<div class="post-metadata">

**Author:** ![ufechner7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ufechner7/32/51363_2.png) [@ufechner7](https://discourse.julialang.org/u/ufechner7)\
**Post date:** [November 28, 2021, 7:58pm UTC](https://discourse.julialang.org/t/strange-error-when-enabling-multiple-threads/72208/4 "2021-11-28T19:58:24Z")

</div>

Additional finding: If I add the parameter ntasks=1 to the CSV.read call like this:

```julia
df_new = CSV.read("data/" * logfile, DataFrame; ntasks=1)

```

no error occurs even if Julia itself has 8 threads available.

So perhaps the bug is in the CSV package and not in the Impute package?

For me it looks as if CSV.read creates different output depending on the number of tasks it is using, and the different output causes Impute to crash.

---

<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:** [November 28, 2021, 8:09pm UTC](https://discourse.julialang.org/t/strange-error-when-enabling-multiple-threads/72208/5 "2021-11-28T20:09:48Z")

</div>

> [@ufechner7](#):
>
> Question: Is it possible to change the number of threads that Julia uses at runtime?

Not right now.

> [@ufechner7](#):
>
> So perhaps the bug is in the CSV package and not in the Impute package?

The stacktrace you posted shows that it is very much in Impute.

---

<div class="post-metadata">

**Author:** ![jpsamaroo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jpsamaroo/32/46804_2.png) [@jpsamaroo](https://discourse.julialang.org/u/jpsamaroo)\
**Post date:** [November 28, 2021, 8:22pm UTC](https://discourse.julialang.org/t/strange-error-when-enabling-multiple-threads/72208/6 "2021-11-28T20:22:40Z")

</div>

This bug can happen when [https://github.com/invenia/Impute.jl/blob/d08c5069f71507e4fee5e49e90f97c9527586a69/src/imputors/interp.jl#L41](https://github.com/invenia/Impute.jl/blob/d08c5069f71507e4fee5e49e90f97c9527586a69/src/imputors/interp.jl#L41) is called with `data::AbstractVector{Missing}`, in which case `T` would not be defined because there is no other eltype than `Missing` for that vector type. This is thus definitely not a multithreading bug, but a bug in how that function is defined; if the `eltype(data) === Missing`, then something else needs to happen than trying to call `T(...)`.
