JuliaIO / JSON.jl

JSON parsing and printing
Other
311 stars 100 forks source link

Julia crashes with multithreaded JSON.parse #341

Open anj00 opened 2 years ago

anj00 commented 2 years ago

I have an array of relatively large JSON messages. They take a while to process in a single threaded loop. However trying to run the loop in multithreaded mode crashes julia. Wonder if this is JSON or julia bug (or my bug).

using JSON

@assert Threads.nthreads() > 1 "please start julia in multithreaded mode \"julia --threads 8\" "

str = "{\"queryCount\":11218,\"resultsCount\":11218,\"adjusted\":true,\"results\":[" * 
       repeat("{\"T\":\"EWT\",\"v\":5.183782e+06,\"vw\":58.9362,\"o\":59.44,\"c\":58.73,\"h\":59.45,\"l\":58.72,\"t\":1649966400000,\"n\":28854},", 11218)
str = str[1:end-1] * "]}" #this is large JSON

parse_total = 1000
strings = repeat([str], parse_total) #now we have many large messages
results_tmp = Vector{Any}(nothing, parse_total)

println("parsing 1 thread")
for i in 1:parse_total
    results_tmp[i] = JSON.parse(strings[i])
    i % 100 == 0 && println(i)
end

println("parsing using $(Threads.nthreads()) threads")
results_tmp = Vector{Any}(nothing, parse_total)
Threads.@threads for i in 1:parse_total
    results_tmp[i] = JSON.parse(strings[i])
end
println("done parsing using $(Threads.nthreads()) threads")

calling this file like this I 100% crash in the multithreaded loop (single thread runs fine all the time). I don't get any message julia just exits julia --threads 16 debug_json.jl

I tried smaller json message and it looks like runnig fine. it is really once it gets big (both message itself and number of messages to parse) I seem to be able to repeat the crash

I run Win10, julia 1.7.2 and JSON v0.21.3

At this point I would be happy with a workaround if one exists.

fredrikekre commented 2 years ago

I tried smaller json message and it looks like runnig fine. it is really once it gets big (both message itself and number of messages to parse) I seem to be able to repeat the crash

Maybe you are running out of memory? Did you monitor RAM usage? I can't reproduce the crash, but running the example consumes a lot of RAM.

anj00 commented 2 years ago

Maybe, but I doubt. it crashes right away. I tried with "julia --threads 2" I don't see any memory spikes in the process monitor (but maybe because it crashes really fast). And I have 64GB total and 37GB available. In single treaded loop the available memory drops to 25GB. Then at the beginning of the multithreaded loop the available memory starts growing (gc?) and then crashes.

So if single threaded it went from 37 down to 25. then in really worst case 2 threads should be fine going down to 12GB. Should be plenty (and we don't yet count effects for swap, unloading unused apps and gc)

anj00 commented 2 years ago

I can't reproduce the crash,

Nice, so maybe it is related to os or julia version or some other setup. Just to double check you really got println("done parsing using $(Threads.nthreads()) threads") ? cause in my case the crash is silent. i.e. it looks like julia processed the file fine, but the above message just not present and this is the hint that it actually quit earlier than it finished the loop

fredrikekre commented 2 years ago

Just to double check you really got println("done parsing using $(Threads.nthreads()) threads")

Yes.

This was on Linux, 8 threads, 16GB RAM.

anj00 commented 2 years ago

Indeed, on the same windows pc running ubuntu linux as an app and running with julia 1.7.2, JSON v0.21.3 above code works all fine.

I also tried that code on another windows pc (clean julia/JSON install). And it crashes (silently) all the same

So it is really a Windows issue (not sure if it is JSON or deeper Julia's problem). Any idea how to move forward here?

anj00 commented 2 years ago

Any chance solving this for windows users?