# Julia gets stuck reading CSV when followed by threaded loop

**URL:** <https://discourse.julialang.org/t/julia-gets-stuck-reading-csv-when-followed-by-threaded-loop/118316>\
**Category:** Data\
**Tags:** question\
**Created:** [August 17, 2024, 8:17pm UTC](https://discourse.julialang.org/t/julia-gets-stuck-reading-csv-when-followed-by-threaded-loop/118316 "2024-08-17T20:17:33Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![cdqb](https://avatars.discourse-cdn.com/v4/letter/c/c2a13f/32.png) [@cdqb](https://discourse.julialang.org/u/cdqb)\
**Post date:** [August 17, 2024, 8:17pm UTC](https://discourse.julialang.org/t/julia-gets-stuck-reading-csv-when-followed-by-threaded-loop/118316/1 "2024-08-17T20:17:34Z")

</div>

I am seeing the following strange behavior where Julia seems to get stuck and becomes unresponsive.

Generate some data and save as .csv:

```julia
using CSV, DataFrames
d = DataFrame(rand(10000,10), :auto)
CSV.write("test.csv", d)

```

Now when I execute the following code Julia gets stuck after 3-4 times on my machine. This seems to be related to the threaded loop - when I remove the threads macro the code executes fine many times.

```julia
df = CSV.read("test.csv", DataFrame)

Threads.@threads for n in 1:300
end

```

Can anyone reproduce this? I am on Julia 1.10.4 and CSV v0.10.14

---

<div class="post-metadata">

**Author:** ![George9000](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/george9000/32/23619_2.png) [@George9000](https://discourse.julialang.org/u/George9000)\
**Post date:** [August 17, 2024, 8:39pm UTC](https://discourse.julialang.org/t/julia-gets-stuck-reading-csv-when-followed-by-threaded-loop/118316/2 "2024-08-17T20:39:29Z")

</div>

Assuming you meant

> [@cdqb](#):
>
> ```julia
> Threads.@threads for n in 1:300
> df = CSV.read("test.csv", DataFrame)
> end
> 
> ```

this runs fine for me.

```julia
julia> versioninfo()
Julia Version 1.10.4
Commit 6f77e2ad37 (2024-08-13 14:35 UTC)
Platform Info:
  OS: macOS (arm64-apple-darwin23.6.0)
  CPU: 16 × Apple M3 Max
  WORD_SIZE: 64
  LIBM: libopenlibm
  LLVM: libLLVM-15.0.7 (ORCJIT, apple-m1)
Threads: 1 default, 0 interactive, 1 GC (on 12 virtual cores)

```

---

<div class="post-metadata">

**Author:** ![cdqb](https://avatars.discourse-cdn.com/v4/letter/c/c2a13f/32.png) [@cdqb](https://discourse.julialang.org/u/cdqb)\
**Post date:** [August 17, 2024, 8:52pm UTC](https://discourse.julialang.org/t/julia-gets-stuck-reading-csv-when-followed-by-threaded-loop/118316/3 "2024-08-17T20:52:31Z")

</div>

As weird as it may sound, the original version with the empty loop at the end was what I intended. If I execute that second cell multiple times, Julia usually gets stuck after 3-4 times. Would you mind giving that a try?

---

<div class="post-metadata">

**Author:** ![abraemer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/abraemer/32/51403_2.png) [@abraemer](https://discourse.julialang.org/u/abraemer)\
**Post date:** [August 17, 2024, 8:53pm UTC](https://discourse.julialang.org/t/julia-gets-stuck-reading-csv-when-followed-by-threaded-loop/118316/4 "2024-08-17T20:53:48Z")

</div>

> [@cdqb](#):
>
> that second cell

Are you using jupyter or Pluto.jl or something like that? Might be relevant

---

<div class="post-metadata">

**Author:** ![cdqb](https://avatars.discourse-cdn.com/v4/letter/c/c2a13f/32.png) [@cdqb](https://discourse.julialang.org/u/cdqb)\
**Post date:** [August 17, 2024, 9:02pm UTC](https://discourse.julialang.org/t/julia-gets-stuck-reading-csv-when-followed-by-threaded-loop/118316/5 "2024-08-17T21:02:04Z")

</div>

No, I’m running directly in VS Code.

---

<div class="post-metadata">

**Author:** ![cdqb](https://avatars.discourse-cdn.com/v4/letter/c/c2a13f/32.png) [@cdqb](https://discourse.julialang.org/u/cdqb)\
**Post date:** [August 17, 2024, 9:11pm UTC](https://discourse.julialang.org/t/julia-gets-stuck-reading-csv-when-followed-by-threaded-loop/118316/6 "2024-08-17T21:11:51Z")

</div>

Actually, when I put the same code into a script and execute it directly in the terminal it seems to work fine. So perhaps a VS Code issue?

---

<div class="post-metadata">

**Author:** ![George9000](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/george9000/32/23619_2.png) [@George9000](https://discourse.julialang.org/u/George9000)\
**Post date:** [August 19, 2024, 1:35am UTC](https://discourse.julialang.org/t/julia-gets-stuck-reading-csv-when-followed-by-threaded-loop/118316/7 "2024-08-19T01:35:21Z")

</div>

Running in the terminal, I too am not seeing Julia get stuck despite multiple calls of the empty loop.

---

<div class="post-metadata">

**Author:** ![abraemer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/abraemer/32/51403_2.png) [@abraemer](https://discourse.julialang.org/u/abraemer)\
**Post date:** [August 19, 2024, 3:45am UTC](https://discourse.julialang.org/t/julia-gets-stuck-reading-csv-when-followed-by-threaded-loop/118316/8 "2024-08-19T03:45:42Z")

</div>

Maybe it’s just the visual in VSCode that gets stuck?

---

<div class="post-metadata">

**Author:** ![cdqb](https://avatars.discourse-cdn.com/v4/letter/c/c2a13f/32.png) [@cdqb](https://discourse.julialang.org/u/cdqb)\
**Post date:** [August 20, 2024, 4:25pm UTC](https://discourse.julialang.org/t/julia-gets-stuck-reading-csv-when-followed-by-threaded-loop/118316/9 "2024-08-20T16:25:34Z")

</div>

It’s certainly something within VS code but I don’t think it is just the visual. In a more complex example I am saving information to disk, and once VS code gets stuck the creating of files stops.

---

<div class="post-metadata">

**Author:** ![eldee](https://avatars.discourse-cdn.com/v4/letter/e/b5a626/32.png) [@eldee](https://discourse.julialang.org/u/eldee)\
**Post date:** [August 20, 2024, 5:39pm UTC](https://discourse.julialang.org/t/julia-gets-stuck-reading-csv-when-followed-by-threaded-loop/118316/10 "2024-08-20T17:39:39Z")

</div>

Your code snippet also runs fine for me, both in the REPL, and in VS Code.

> **versioninfo()**
>
> ```julia-repl
> Julia Version 1.10.4
> Commit 48d4fd4843 (2024-06-04 10:41 UTC)
> Build Info:
> Official https://julialang.org/ release
> Platform Info:
> OS: Windows (x86_64-w64-mingw32)
> CPU: 8 × Intel(R) Core(TM) i7-7700K CPU @ 4.20GHz
> WORD_SIZE: 64
> LIBM: libopenlibm
> LLVM: libLLVM-15.0.7 (ORCJIT, skylake)
> Threads: 8 default, 0 interactive, 4 GC (on 8 virtual cores)
> Environment:
> JULIA_NUM_THREADS = 8
> JULIA_EDITOR = code
> 
> ```

> **VS Code v1.92.1**
>
> Version: 1.92.1 (user setup)  
> Commit: eaa41d57266683296de7d118f574d0c2652e1fc4  
> Date: 2024-08-07T20:16:39.455Z  
> Electron: 30.1.2  
> ElectronBuildId: 9870757  
> Chromium: 124.0.6367.243  
> Node.js: 20.14.0  
> V8: 12.4.254.20-electron.0  
> OS: Windows\_NT x64 10.0.19045

VS Code Julia extension v1.105.2

---

<div class="post-metadata">

**Author:** ![cdqb](https://avatars.discourse-cdn.com/v4/letter/c/c2a13f/32.png) [@cdqb](https://discourse.julialang.org/u/cdqb)\
**Post date:** [August 20, 2024, 8:37pm UTC](https://discourse.julialang.org/t/julia-gets-stuck-reading-csv-when-followed-by-threaded-loop/118316/11 "2024-08-20T20:37:49Z")

</div>

Thanks for checking! Does it also work fine when executing multiple (\>4) times in VS Code (e.g., pressing Alt+Enter multiple times with the second part of the code in a script)?

---

<div class="post-metadata">

**Author:** ![eldee](https://avatars.discourse-cdn.com/v4/letter/e/b5a626/32.png) [@eldee](https://discourse.julialang.org/u/eldee)\
**Post date:** [August 21, 2024, 9:14am UTC](https://discourse.julialang.org/t/julia-gets-stuck-reading-csv-when-followed-by-threaded-loop/118316/12 "2024-08-21T09:14:33Z")

</div>

- As a Julia script (.jl) it always executes fine, no matter how often I spam alt/ctrl + enter.
- As a Jupyter notebook (.ipynb),
  - When repeatedly running the second cell (`CSV.read`, `@threads`) at a normal pace using ctrl+enter, ctrl+alt+enter, or the run icon, everything works fine.
  - When spamming ctrl(+alt)+enter, say 10 times per second during 10 seconds, everything is still fine.
  - When spamming the run icon, I can get VS Code stuck. I’m not entirely sure if I am in fact clicking run, or stop (which the run icon turns into when a cell is running), as the cell normally finishes execution basically immediately, but in any case the cell execution certainly does not stop. I can still run other cells (i.e. the first cell with the `CSV.write`), but to run the second cell again, I need to restart VS Code.
