https://kotlinlang.org logo
#javascript
Title
# javascript
a

ankur2037

11/24/2023, 5:23 PM
Nginx 403 errors Hello everyone, I am currently using KMP for making network calls on a JavaScript server, utilizing Ktor JS. My implementation involves creating a new
CoroutineScope
in each class instance (Wrapper class also created for each request), as shown in the following code snippet:
Copy code
class Wrapper {
    private var coroutineScope: CoroutineScope? = null

    fun getResponse(block: (response) -> Unit) {
        if (coroutineScope?.isActive != true) {
            coroutineScope = CoroutineScope(Dispatchers.Main + SupervisorJob())
        }
        coroutineScope?.launch {
            dataFlow.collect { response ->
                block(response)
            }
        }
    }
}
After making this change, I am frequently encountering Nginx 403 errors, specifically timeouts. Has anyone else experienced a similar issue or have insights into what might be causing these errors?
c

CLOVIS

11/27/2023, 10:37 AM
Creating a new job for each request is an anti-pattern that breaks structured concurrency. The job represents the lifetime of the request, or said otherwise "who cares about it". The job should always be provided by the caller (either explicitly as a parameter, or implicitly through the
suspend
keyword). I'm not sure what the problem is exactly in this case, but I wouldn't be surprised if the entire
Wrapper
instance got GC'd.