hxcpp icon indicating copy to clipboard operation
hxcpp copied to clipboard

EReg is not thread safe.

Open barisyild opened this issue 1 year ago • 2 comments

https://github.com/HaxeFoundation/hxcpp/blob/96875093ec43e48dc2e1da95b8730e0c122ae017/project/thirdparty/pcre2-10.42/src/pcre2_match.c#L7289

Rarely segmentation fault occurs on the line below, but usually match returns incorrectly.

var regex = new EReg("^[0-9]+", "");
for(i in 0...8)
{
    Thread.create(() -> {
        for (i in 0...1000000)
        {
            var str:String = "12312";
            if(regex.match(str))
            {
                // OK
            }else{
                // Wrong Result
                trace("Wrong Result");
            }
        }
    });
}

barisyild avatar Jan 02 '25 18:01 barisyild

This is entirely expected, EReg.match modifies internal state and is therefore not thread safe.

You should either create a new regex on each thread or use a mutex. I don't think there's anything that can be done in the standard library or in hxcpp.

Apprentice-Alchemist avatar Jan 02 '25 18:01 Apprentice-Alchemist

Can't make an hxcpp conditional macro that locks everything that isn't thread safe, so that if it can't lock it, can throw an exception and create a safer development environment?

For example, in languages ​​like Java, ConcurrentModificationException is returned in such cases.

barisyild avatar Jan 02 '25 22:01 barisyild