`RequestContextAwareCompletableFuture` callback not invoked when context mismatches.
See original GitHub issueLet’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:
- Created 4 years ago
- Reactions:1
- Comments:14 (5 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

By the way, once we introduce
RequestContext.parent()we might not need to copy the attributes. When not found, could just check the parent.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.