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.

`RequestContextAwareCompletableFuture` callback not invoked when context mismatches.

See original GitHub issue

Let’s say we have the following code:

ServiceRequestContext ctx1 = ServiceRequestContext.of(...);
ServiceRequestContext ctx2 = ServiceRequestContext.of(...);
try (SafeCloseable sc1 = ctx1.push()) {
    CompletableFuture f = ctx2.makeContextAware(new CompletableFuture());
    AtomicBoolean callbackCalled = new AtomicBoolean();
    f.whenComplete((res, cause) -> callbackCalled.set(true));

    f.complete(null);    

    // The callback will never be invoked and
    // the following assertion will fail.
    assert callbackCalled.get(); // <-- Failure!
}

It seems like everything is working as expected. The callback (BiConsumer) was not invoked because it was decorated by ctx2.makeContextAware(BiConsumer) and the callback is invoked only when ctx2.pushIfAbsent() succeeds (see), to protect our users from the accidental context conflict like the above example.

The problem is that a user does not have a way to know this happened. It is really confusing because the callback will never be invoked. It is neither possible to handle the IllegalStateException raised by pushIfAbsent():

f.whenComplete((res, cause) -> callbackCalled.set(true))
 .exceptionally(cause -> {
     // We never reach here, either, because this callback
     // will be decorated again and fail at `pushIfAbsent()` again.
     ...
 });

What would be the best way to let a user know about this problem?

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:1
  • Comments:14 (5 by maintainers)

github_iconTop GitHub Comments

3reactions
trustincommented, Nov 29, 2019

By the way, once we introduce RequestContext.parent() we might not need to copy the attributes. When not found, could just check the parent.

1reaction
minwooxcommented, Jan 2, 2020

How about logging at WARN level instead of raising an exception for the future callbacks? If a user wraps something explicitly via ctx.makeContextAware(), we could raise an IllegalStateException as before, though, i.e.

Had a chat with @trustin and decided to leave warning logs multiple times to let the users know something is going wrong. We will still throw an exception because otherwise some critical resources can be shared such as access token, etc.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Message "Async callback was not invoked within the 5000 ms ...
Sometimes, when I run the tests, everything works as expectedly. Other times, I get an error: Timeout - Async callback was not invoked...
Read more >
Async callback was not invoked within timeout specified by ...
The error message tells you that the done() was not called within the timeout period. This means that the async expectation was not...
Read more >
AWS Lambda function handler in Node.js
Non -async handlers. The following example function checks a URL and returns the status code to the invoker. Example index.js file – HTTP...
Read more >
Converting Callbacks to Promises in Node.js - Stack Abuse
Let's take an example, we'll write a callback function which will be executed when the program successfully reads a file from our hard...
Read more >
What to do when “this” loses context - freeCodeCamp
This time the context is not lost when the method is used as a callback. let service = Service(); service.doSomething(); //callback on DOM...
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