-
Notifications
You must be signed in to change notification settings - Fork 1.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
withContext should copy CopyableThreadContextElement #3414
Comments
See Kotlin/kotlinx.coroutines#3411 See Kotlin/kotlinx.coroutines#3414 GitOrigin-RevId: 234f21de1899b48425f97193ca63769246b54173
The rationale behind the current behaviour is the following:
Also, if we are going to change the behaviour of It seems like the best solution is to provide your own clearly-named alias to I would also like to hear feedback regarding this API and this particular behaviour from @yorickhenning if he is ready to elaborate on that matter |
I think I share @qwwdfsad's understanding of what a coroutine "is". I'd expect three copied
Those being the three pseudo-lightweight-thread blocks that execute parallelisable statement lists. I double checked a prototype and added these: @Test
fun testWithContextCopies() = runTest {
assertNull(coroutineContext[IntegerContextElement.Key])
launch(AddElementInterceptor()) {
withContext(Dispatchers.IO) {
assertEquals(
1,
coroutineContext[IntegerContextElement.Key]!!.number
)
}
}
}
@Test
fun testCoroutineScopeCopies() = runTest {
assertNull(coroutineContext[IntegerContextElement.Key])
launch(AddElementInterceptor()) {
coroutineScope {
assertEquals(
1,
coroutineContext[IntegerContextElement.Key]!!.number
)
}
}
} They fail with NPE at IIRC the reason Given how things are, if measuring an undispatched launch were interesting, I'd add a span. I think that's what @qwwdfsad suggested as
Intended semantics aside, I think calling An API that intercepts all At that level though, I think
In the systems I work on, both sides are consistently about four elements long. They both have a constant upper bound on length. I don't know whether those systems are typical. I believe we're paying a linear search cost in every context in our production coroutine systems by including a Benchmarks would certainly show a difference. |
I'd be happy with |
I know that
withContext {}
is not equivalent toasync {}.await()
, but it does create a child coroutine (althoughUndispatchedCoroutine
, but it still a separate instance) and introduces a CoroutineScope (it can also have children).async(CoroutineName("task 3"), start = UNDISPATCHED) {}.await()
kind of does what I want, but I also want the production code to look idiomatic since the measurer might or might not be present in the context.The text was updated successfully, but these errors were encountered: