Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

regex not threadsafe #31521

Closed
StefanKarpinski opened this issue Mar 28, 2019 · 5 comments
Closed

regex not threadsafe #31521

StefanKarpinski opened this issue Mar 28, 2019 · 5 comments
Assignees
Labels
multithreading Base.Threads and related functionality

Comments

@StefanKarpinski
Copy link
Member

There's a race condition for creating the regex JIT cache from different threads.

@StefanKarpinski StefanKarpinski added the multithreading Base.Threads and related functionality label Mar 28, 2019
@NHDaly
Copy link
Member

NHDaly commented Mar 28, 2019

context: #31309 (comment)

@non-Jedi
Copy link
Contributor

Am I correct that this is why the program at https://salsa.debian.org/benchmarksgame-team/benchmarksgame/issues/143 only gives correct output if the Threads.@threads macro is removed? Code in question:

const variants = (
      "agggtaaa|tttaccct",
      "[cgt]gggtaaa|tttaccc[acg]",
      "a[act]ggtaaa|tttacc[agt]t",
      "ag[act]gtaaa|tttac[agt]ct",
      "agg[act]taaa|ttta[agt]cct",
      "aggg[acg]aaa|ttt[cgt]ccct",
      "agggt[cgt]aa|tt[acg]accct",
      "agggta[cgt]a|t[acg]taccct",
      "agggtaa[cgt]|[acg]ttaccct"
)

const subs = (
    (r"tHa[Nt]", "<4>"),
    (r"aND|caN|Ha[DS]|WaS", "<3>"),
    (r"a[NSt]|BY", "<2>"),
    (r"<[^>]*>", "|"),
    (r"\|[^|][^|]*\|", "-")
)

function perf_regex_dna(io)
    seq = read(stdin, String)
    l1 = length(seq)

    seq = replace(seq, r">.*\n|\n" => "")
    l2 = length(seq)

    variant_counts = zeros(Int64, length(variants))
    @inbounds Threads.@threads for i in 1:length(variants)
        variant_counts[i] = length(collect(eachmatch(Regex(variants[i]), seq)))
    end
    for (v, k) in zip(variants, variant_counts)
        write(io, v, ' ', string(k), '\n')
    end#for

    for (u, v) in subs
        seq = replace(seq, u => v)
    end

    write(io, '\n', string(l1), '\n', string(l2), '\n', string(length(seq)), '\n')
end

perf_regex_dna(stdout)

@NHDaly
Copy link
Member

NHDaly commented Jun 24, 2019

😁♥️ thanks Jeff!

@JeffBezanson
Copy link
Member

@non-Jedi Yes, I would say so. Please try it and see if it works now.

@non-Jedi
Copy link
Contributor

non-Jedi commented Dec 5, 2019

Sorry for the delay in replying. Just tested with 1.3 (where I assume this commit made it into), and the bug is gone.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
multithreading Base.Threads and related functionality
Projects
None yet
Development

No branches or pull requests

4 participants