Hi, I am running into an issue when calling Regex....
# coroutines
p
Hi, I am running into an issue when calling Regex.find() in a coroutine it will crash with no exception thrown (notably with a large regex pattern & string). I wrote some code for replication purposes (pattern & string obviously don’t match my actual use-case)
Copy code
@Test
    fun testRegex() {
        runBlocking{
            launch(Dispatchers.Default){
                var string = "hello"
                var regex = "(hello)"
                for(i in 1..10){
                    string += string
                    regex += regex
                }

                println(regex)
                println(string)
                Regex(regex).find(string) <--- Crash with no exception

            }
        }
    }
j
are you sure test isnt just finishing before the crash happens?
Copy code
@Test
    fun testRegex() {
        runBlocking{
            launch(Dispatchers.Default){
                var string = "hello"
                var regex = "(hello)"
                for(i in 1..10){
                    string += string
                    regex += regex
                }

                println(regex)
                println(string)
                Regex(regex).find(string) <--- Crash with no exception

            }.join()
        }
    }
p
Yeah, I’m sure. I just forgot the
.join()
in my comment.
m
Hi Patrick, It passed the test like this:
Copy code
@Test
fun testRegex() = runTest {
    var string = "hello"
    var regex = "(hello)"
    for (i in 1..10) {
        string += string
        regex += regex
    }

    println(regex)
    println(string)
    Regex(regex).find(string)
}
You may need to add this dependency to have the
runTest
.
Copy code
dependencies {
    testImplementation 'org.jetbrains.kotlinx:kotlinx-coroutines-test:1.7.3'
}
More info about testing coroutines: https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-test/
☝️ 1
p
I resolved the issue. What was happening is I was testing on iOS/apple and the regex would cause a segfault (without explicitly logging that). Had to dumb down my regex pattern to get it to work properly. Seems like a known issue: https://youtrack.jetbrains.com/issue/KT-46211 https://youtrack.jetbrains.com/issue/KT-53352/Native-iOS-Crash-when-using-regex-newSingleThreadContext
👏 2