# Why does logging (@info) create a race condition?

**URL:** https://discourse.julialang.org/t/why-does-logging-info-create-a-race-condition/118428
**Category:** General Usage
**Created:** [August 20, 2024, 7:47pm UTC](https://discourse.julialang.org/t/why-does-logging-info-create-a-race-condition/118428 "2024-08-20T19:47:11Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![evanfields](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/evanfields/32/1744_2.png) [@evanfields](https://discourse.julialang.org/u/evanfields)
#### Post date: [August 20, 2024, 7:47pm UTC](https://discourse.julialang.org/t/why-does-logging-info-create-a-race-condition/118428/1 "2024-08-20T19:47:11Z")

</div>

I have a directory with compressed CSVs that I’m reading in parallel. Roughly like so, simplifying away domain specific details not relevant to the question:

```julia
function munge_csv(stream)
    ...read a CSV with CSV.read, munge it, return...
end

function read_directory(dir)
    tasks = map(readdir(dir; join = true)) do filepath
        Threads.@spawn begin
            df = open(ZstdDecompressorStream, filepath) do stream
                return munge_csv(stream)
            end
            # this next lines producess a data race if included, wat?
            @info "hi"
            return df
        end
    end
    dfs = fetch.(tasks)
    @info "Finished fetching" n_dfs = length(dfs) df_sizes = sort(nrow.(dfs))
    df = vcat(dfs...)
    return df
end

```

With the flagged `@info "hi"` line, I seem to have a race condition; the final results are inconsistent and the sizes of the task-specific dataframes are inconsistent. Without that logging line, there’s either no data race or at the least it’s rare enough that I haven’t seen it in a few dozen repeat trials.

What’s going on here? Is the logging call actually creating a threading problem, or is that a red herring? Is something else about this construct inherently not threadsafe, and the logging just makes it worse?

---

<div class="post-metadata">

### Author: ![nhz2](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nhz2/32/44428_2.png) [@nhz2](https://discourse.julialang.org/u/nhz2)
#### Post date: [August 20, 2024, 7:57pm UTC](https://discourse.julialang.org/t/why-does-logging-info-create-a-race-condition/118428/2 "2024-08-20T19:57:47Z")

</div>

I would change:

```julia
df = open(ZstdDecompressorStream, filepath) do stream
                return munge_csv(stream)
            end

```

to:

```julia
local df = open(ZstdDecompressorStream, filepath) do stream
                return munge_csv(stream)
            end

```

to make sure `df` isn’t getting captured from an upper scope, when you do `df = vcat(dfs...)`.

---

<div class="post-metadata">

### Author: ![evanfields](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/evanfields/32/1744_2.png) [@evanfields](https://discourse.julialang.org/u/evanfields)
#### Post date: [August 21, 2024, 2:03pm UTC](https://discourse.julialang.org/t/why-does-logging-info-create-a-race-condition/118428/3 "2024-08-21T14:03:10Z")

</div>

Ah, an embarrassing facepalm, thank you! I got got by `df` appearing later in a hard local scope than the first `do` block closures.
