{"action":"closed","issue":{"url":"https://api.github.com/repos/Autodesk/coroutineworker/issues/39","repository_url":"https://api.github.com/repos/Autodesk/coroutineworker","labels_url":"https://api.github.com/repos/Autodesk/coroutineworker/issues/39/labels{/name}","comments_url":"https://api.github.com/repos/Autodesk/coroutineworker/issues/39/comments","events_url":"https://api.github.com/repos/Autodesk/coroutineworker/issues/39/events","html_url":"https://github.com/Autodesk/coroutineworker/issues/39","id":538173440,"node_id":"MDU6SXNzdWU1MzgxNzM0NDA=","number":39,"title":"Ktor-client inside CoroutineWorker.execute - Uncaught Kotlin exception: kotlin.native.IncorrectDereferenceException: Trying to access top level value not marked as @ThreadLocal or @SharedImmutable from non-main thread","user":{"login":"khairilushan","id":1143117,"node_id":"MDQ6VXNlcjExNDMxMTc=","avatar_url":"https://avatars3.githubusercontent.com/u/1143117?v=4","gravatar_id":"","url":"https://api.github.com/users/khairilushan","html_url":"https://github.com/khairilushan","followers_url":"https://api.github.com/users/khairilushan/followers","following_url":"https://api.github.com/users/khairilushan/following{/other_user}","gists_url":"https://api.github.com/users/khairilushan/gists{/gist_id}","starred_url":"https://api.github.com/users/khairilushan/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/khairilushan/subscriptions","organizations_url":"https://api.github.com/users/khairilushan/orgs","repos_url":"https://api.github.com/users/khairilushan/repos","events_url":"https://api.github.com/users/khairilushan/events{/privacy}","received_events_url":"https://api.github.com/users/khairilushan/received_events","type":"User","site_admin":false},"labels":[],"state":"closed","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":12,"created_at":"2019-12-16T04:07:49Z","updated_at":"2019-12-17T16:07:38Z","closed_at":"2019-12-17T16:07:38Z","author_association":"NONE","body":"I just watched the talk that @benasher44 gave at droidCon NYC about Concurrency in Kotlin/Native and decided to try this `CoroutineWorker` library.  \r\nSo what I am trying to do is consuming some backend REST API using Ktor inside `CoroutineWorker.execute`. Basically, I am still not really sure if I am doing this correct but here is the code:\r\n\r\nSo I have a `NetworkProvider` class:\r\n```\r\ninternal class NetworkProvider<T : Endpoint>(private val engine: HttpClientEngine) {\r\n\r\n    private val client: HttpClient by lazy {\r\n        HttpClient(engine) {\r\n            install(JsonFeature) {\r\n                serializer = KotlinxSerializer()\r\n            }\r\n        }\r\n    }\r\n\r\n    @UnstableDefault\r\n    internal suspend fun <R> request(endpoint: T, strategy: DeserializationStrategy<R>): R {\r\n\r\n        val response: String = client.request {\r\n            method = endpoint.method\r\n            url.apply {\r\n                protocol = URLProtocol.HTTPS\r\n                host = endpoint.baseUrl\r\n\r\n                val query = endpoint.parameters\r\n                    .map { \"${it.key}=${it.value}\" }\r\n                    .joinToString(\"&\")\r\n\r\n                encodedPath = if (query.isNotEmpty()) {\r\n                    \"${endpoint.path}?$query\"\r\n                } else {\r\n                    endpoint.path\r\n                }\r\n            }\r\n        }\r\n\r\n        return Json.nonstrict.parse(strategy, response)\r\n    }\r\n\r\n}\r\n```\r\n\r\nAnd then I use this `NetworkProvider` inside my Repository class like this:\r\n```\r\ninternal class MovieRepositoryImpl(\r\n    private val provider: NetworkProvider<MovieEndpoint>\r\n) : MovieRepository {\r\n\r\n\r\n    override fun getMovieList(\r\n        year: Int,\r\n        page: Int,\r\n        sort: String,\r\n        completion: (Result<MovieListEntity>) -> Unit\r\n    ) {\r\n\r\n        CoroutineWorker.execute {\r\n\r\n            val result = try {\r\n                val response = provider.request(\r\n                    endpoint = MovieListEndpoint(year, page, sort),\r\n                    strategy = MovieListEntity.serializer()\r\n                )\r\n                Result.Success(response)\r\n            } catch (error: Throwable) {\r\n                Result.Failure(error)\r\n            }\r\n\r\n            CoroutineWorker.withContext(MainDispatcher) {\r\n                completion(result)\r\n            }\r\n        }\r\n    }\r\n\r\n}\r\n```\r\n\r\nBut it's end up gave me this error messages:\r\n```\r\nUncaught Kotlin exception: kotlin.native.IncorrectDereferenceException: Trying to access top level value not marked as @ThreadLocal or @SharedImmutable from non-main thread\r\n        at 0   MovieCore                           0x0000000109964fe7 kfun:kotlin.Throwable.<init>(kotlin.String?)kotlin.Throwable + 87\r\n        at 1   MovieCore                           0x000000010995e355 kfun:kotlin.Exception.<init>(kotlin.String?)kotlin.Exception + 85\r\n        at 2   MovieCore                           0x000000010995df15 kfun:kotlin.RuntimeException.<init>(kotlin.String?)kotlin.RuntimeException + 85\r\n        at 3   MovieCore                           0x0000000109990c95 kfun:kotlin.native.IncorrectDereferenceException.<init>(kotlin.String)kotlin.native.IncorrectDereferenceException + 85\r\n        at 4   MovieCore                           0x00000001099add29 ThrowIncorrectDereferenceException + 137\r\n        at 5   MovieCore                           0x0000000109cd3799 CheckIsMainThread + 25\r\n        at 6   MovieCore                           0x0000000109c5a893 kfun:utils.<get-MainDispatcher>$core()kotlinx.coroutines.CoroutineDispatcher + 35\r\n        at 7   MovieCore                           0x0000000109c3f7e4 kfun:repository.MovieRepositoryImpl.$getMovieList$lambda-1COROUTINE$1.invokeSuspend#internal + 1892\r\n        at 8   MovieCore                           0x0000000109c40020 kfun:repository.MovieRepositoryImpl.$getMovieList$lambda-1COROUTINE$1.invoke#internal + 256\r\n        at 9   MovieCore                           0x0000000109b069f3 kfun:kotlinx.coroutines.intrinsics.startUndispatchedOrReturn$kotlinx-coroutines-core@kotlinx.coroutines.internal.ScopeCoroutine<#GENERIC>.(#GENERIC;kotlin.coroutines.SuspendFunction1<#GENERIC,#GENERIC>)Generic + 899\r\n        at 10  MovieCore                           0x0000000109ab11bd kfun:kotlinx.coroutines.coroutineScope(kotlin.coroutines.SuspendFunction1<kotlinx.coroutines.CoroutineScope,#GENERIC>)Generic + 429\r\n        at 11  MovieCore                           0x0000000109c37f70 kfun:com.autodesk.coroutineworker.CoroutineWorker.Companion.WorkItem.$<init>$lambda-1COROUTINE$11.invokeSuspend#internal + 1520\r\n        at 12  MovieCore                           0x0000000109c386a0 kfun:com.autodesk.coroutineworker.CoroutineWorker.Companion.WorkItem.$<init>$lambda-1COROUTINE$11.invoke#internal + 256\r\n        at 13  MovieCore                           0x0000000109c33226 kfun:com.autodesk.coroutineworker.BackgroundCoroutineWorkQueueExecutor.$processWorkItems$lambda-0COROUTINE$7.invokeSuspend#internal + 726\r\n        at 14  MovieCore                           0x0000000109986618 kfun:kotlin.coroutines.native.internal.BaseContinuationImpl.resumeWith(kotlin.Result<kotlin.Any?>) + 712\r\n        at 15  MovieCore                           0x0000000109afb06c kfun:kotlinx.coroutines.DispatchedTask.run() + 2732\r\n        at 16  MovieCore                           0x0000000109ab606d kfun:kotlinx.coroutines.EventLoopImplBase.processNextEvent()ValueType + 813\r\n        at 17  MovieCore                           0x0000000109b097c6 kfun:kotlinx.coroutines.BlockingCoroutine.joinBlocking#internal + 1958\r\n        at 18  MovieCore                           0x0000000109b0886e kfun:kotlinx.coroutines.runBlocking(kotlin.coroutines.CoroutineContext;kotlin.coroutines.SuspendFunction1<kotlinx.coroutines.CoroutineScope,#GENERIC>)Generic + 1246\r\n        at 19  MovieCore                           0x0000000109b08de4 kfun:kotlinx.coroutines.runBlocking$default(kotlin.coroutines.CoroutineContext?;kotlin.coroutines.SuspendFunction1<kotlinx.coroutines.CoroutineScope,#GENERIC>;kotlin.Int)Generic + 372\r\n        at 20  MovieCore                           0x0000000109c32da9 kfun:com.autodesk.coroutineworker.BackgroundCoroutineWorkQueueExecutor.enqueueWork$<anonymous>_3#internal + 217\r\n        at 21  MovieCore                           0x0000000109c340fb kfun:com.autodesk.coroutineworker.BackgroundCoroutineWorkQueueExecutor.$enqueueWork$<anonymous>_3$FUNCTION_REFERENCE$7.invoke#internal + 59\r\n        at 22  MovieCore                           0x0000000109c3415b kfun:com.autodesk.coroutineworker.BackgroundCoroutineWorkQueueExecutor.$enqueueWork$<anonymous>_3$FUNCTION_REFERENCE$7.$<bridge-UNN>invoke()#internal + 59\r\n        at 23  MovieCore                           0x0000000109c3cca4 kfun:com.autodesk.coroutineworker.WorkerPool.performWork$lambda-1#internal + 308\r\n        at 24  MovieCore                           0x0000000109c3d0df kfun:com.autodesk.coroutineworker.WorkerPool.$performWork$lambda-1$FUNCTION_REFERENCE$17.invoke#internal + 63\r\n        at 25  MovieCore                           0x0000000109c3d13b kfun:com.autodesk.coroutineworker.WorkerPool.$performWork$lambda-1$FUNCTION_REFERENCE$17.$<bridge-UNN>invoke()#internal + 59\r\n        at 26  MovieCore                           0x00000001099927c1 WorkerLaunchpad + 177\r\n        at 27  MovieCore                           0x0000000109cd8069 _ZN6Worker19processQueueElementEb + 2569\r\n        at 28  MovieCore                           0x0000000109cd8616 _ZN12_GLOBAL__N_113workerRoutineEPv + 54\r\n        at 29  libsystem_pthread.dylib             0x00007fff51c04d36 _pthread_start + 125\r\n```\r\n\r\nAnyone know how to work around this error message? "}}
{"actor":{"display_login":"benasher44"}}