RgxGen icon indicating copy to clipboard operation
RgxGen copied to clipboard

Nulls at SymbolSet

Open kirillsinyuk opened this issue 1 year ago • 5 comments

Describe the bug I tried to generate lots of random phone numbers locally and at one point noticed nulls in generated strings

To Reproduce Unfortunately, I can't tell the exact way to reproduce, after restart it came back to normal and I couldn't catch it again

Screenshots RgxGen props in debug: SCR-20240217-knia

Environment (please complete the following information):

  • OS: macOS
  • JDK/JRE version: 17
  • RgxGen Version: 1.4

kirillsinyuk avatar Feb 17 '24 08:02 kirillsinyuk

Hello!

The screenshot you provided indeed looks wierd: image

I just have run a test:

    @Test
    public void bug91_nullsReturnedSometimes() {
        String pattern = "7\\d{10}";
        for (int i = 0; i < 1000000; i++) {
            RgxGen rgxGen = new RgxGen(pattern);
            Node node = rgxGen.getaNode();
            Sequence sequence = (Sequence) node;
            Repeat repeat = (Repeat) sequence.getNodes()[1];
            SymbolSet symbolSet = (SymbolSet) repeat.getNode();
            for (Character symbol : symbolSet.getSymbols()) {
                if (symbol == null) {
                    System.out.println("ERROR");
                }
            }
        }
    }

Which did not print a single error.

I have also dived into code to see if anything could cause such a behavior

The array of characters is created like this:

    Character[] getDigits() {
        if (aDigits == null) {
            aDigits = IntStream.rangeClosed('0', '9')
                               .mapToObj(i -> (char) i)
                               .toArray(Character[]::new);
        }

        return aDigits;
    }

And i don't see how those extra nulls could get in there. image

Maybe you could run test on your machine to see if you get any errors.

curious-odd-man avatar Feb 17 '24 14:02 curious-odd-man

I have a hypothesis. It seems that RgxGen is not thread-safe and I tried to use it from several threads. This explains why I can't reproduce it easily. So, generally, it's not the problem of this lib but the problem of my usage of it) You can close the issue if you have no plans to work towards thread-safety. And thanks for response.

kirillsinyuk avatar Feb 19 '24 15:02 kirillsinyuk

Hi @curious-odd-man, Just experienced the same and found this issue. If you have no plans to make this thread safe, would you be able to call this out as non-ThreadSafe in the javadoc?

Avinm avatar Apr 25 '24 19:04 Avinm

Hi @Avinm!

Thanks for reporting this. May I ask you to give me more details? For example the pattern that you have used and maybe code example? Or, if you are working on open source project, you could share a link to a branch?

Additionally, could you please try out latest version 2.0 and confirm if the issue is still reproducible?

So far I wasn't able to reproduce this myself, but I will make a note in docs to highlight possible multithreading issues.

curious-odd-man avatar Apr 25 '24 19:04 curious-odd-man

@curious-odd-man : I doubt the pattern has any bearing on this, so I used the same pattern as @kirillsinyuk . I see a (mutable?) variable private final Node aNode; when looking through RgxGen class which doesn't look very thread safe:

@Test
@SneakyThrows
public void bug91_nullsReturnedSometimes() {
    String pattern = "7\\d{10}";
    val rgxGen = new RgxGen(pattern);
    val pool = Executors.newFixedThreadPool(64);
    val wrongStrings = pool.submit(() ->
        IntStream.range(0, 1000000)
                 .parallel()
                 .mapToObj(i -> rgxGen.generate())
                 .filter(s -> s.contains("null"))
                 .collect(Collectors.toList())
    ).get();
    assertEquals(Collections.emptyList(), wrongStrings);
}

image

Avinm avatar Apr 25 '24 20:04 Avinm