question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

"An error occurred while pushing a context" error occurs when using RequestContextHooks.enable()

See original GitHub issue

Application using RequestContextHooks.enable() for log tracking, and if many requests come in at the same time, An error occurred while pushing a context error occurs.

In the service, it occurs in the action of querying with r2dbc jooq. I’m not sure sample code below is the same cause, but the same error occurs.

version info

kotlin 1.7.20
armeria-logback : 1.20.1
armeria-reactor3: 1.20.1
kotlinx-coroutines-core-jvm: 1.6.4
kotlinx-coroutines-reactive: 1.6.4
kotlinx-coroutines-reactor: 1.6.4

Error log

Caused by: java.lang.IllegalStateException: Trying to call object wrapped with context [sreqId=8d41eecd, chanId=96523267, raddr=127.0.0.1:57795, laddr=127.0.0.1:8080][h1c://gimnamjung-ui-macbookpro.local/v1/players/test/%ED%95%9C%EB%8F%99%EA%B7%B8%EB%9D%BC%EB%AF%B8#GET], but context is currently set to [sreqId=673c702e, chanId=7a3518ba, raddr=127.0.0.1:57796, laddr=127.0.0.1:8080][h1c://gimnamjung-ui-macbookpro.local/v1/players/test/%ED%95%9C%EB%8F%99%EA%B7%B8%EB%9D%BC%EB%AF%B8#GET]. This means the callback was called from unexpected thread or forgetting to close previous context.

Sample Code

// TestAnnotatedService
@Get("/test")
suspend fun test(): Long? {
        return testService.test()
}
// TestService
private val mutex = Mutex() // kotlinx.coroutines.sync

suspend fun test(): Long? {
        return mutex.withLock {
            Mono.just(1L)
                .awaitSingle()
        }
}

Issue Analytics

  • State:open
  • Created a year ago
  • Comments:12 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
minwooxcommented, Nov 3, 2022

I found one possible workaround. It’s using ArmeriaRequestCoroutinContext explicitly with GlobalScope.async:

@Service
class TestService {
    private val mutex = Mutex()

    suspend fun runWithMutex(): String {
        val requestContext = ServiceRequestContext.current()
        return GlobalScope.async(ArmeriaRequestCoroutineContext(requestContext)) {
            mutex.withLock {
                Mono.just("test").awaitSingle()
            }
        }.await()
    }
}
1reaction
minwooxcommented, Nov 3, 2022

Sorry about the late response. 😓

kotlin coroutine doesn’t guarantee that the block executes on the same thread.

Yes, exactly. When there’re two concurrent requests A and B, the thread for A also runs the block for the request B which then:

  • the Mono is created with the aContext by the hook
  • the Mono raises an exception when it subscribed with the bContext.

I guess not using the RequestContextHooks and push the context manually where it needs is the best way we can do for now. 😢 I’m thinking of adding a global hook that is removing the RequestContext in thread-local and pushing it back when the continuation is resumed but I’m not sure if it’s really possible. Let me get back to this issue when I find the solution. 🙇

Read more comments on GitHub >

github_iconTop Results From Across the Web

Reactor should use context for when subscriber is created, not ...
Our hooks store the context when the publisher is created. So the thread that calls publisher.subscribe() does not have to have ctx in...
Read more >
Index (Armeria 1.7.2 API reference) - Javadoc.io
Enables cookies to be added to CORS requests. allowedRequestHeaders() - Method in annotation type com.linecorp.armeria.server.annotation.decorator.
Read more >
Allow to bind KafkaClientMetrics for each decaton consumer
Always bind KafkaClientMetrics , tagged with the right subscription id. This should not be a breaking change since there is no reasonable way...
Read more >
Line Armeria Statistics & Issues - Codesti
"An error occurred while pushing a context" error occurs when using RequestContextHooks.enable(), open, 12, 2022-10-22, 2022-11-20, -.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found