I have this nondeterministic bug in production and I can't pin it down with a minimal example
My setup is okhttp, which drives Retrofit, which uses coroutines adapter
I use Authenticator to refresh tokens and retry the requests, something like this
fun Authenticator.authenticate() {
...
synchronized(this) {
runBlocking { retrofitService.suspendingRefreshTokens() }<---
...
}
}
What however SOMETIMES happens is that the api call just doesn't finish, as if the coroutines were broken.
I searched the internets and there seem to be some folks affected by this as well
Using Coroutine runblock with the Authenticator to handle 401 response from retrofit
Any intuition on what could be happening? I was thinking it was some sort of a deadlock because of the synchronized, however runBlocking seems to switch back to the original context, so it should be the same thread (atleast it appears that way in testing)
This never happened before with rxjava, where I used the same blockingGet() way to keep it blocking inside Authenticator; However rxjava retrofit adapter didn't use the async parameter; which leads me to believe it possibly could be a issue with Okhttp's threadpools somehow; rather than runBlocking? (which I also read is buggy)
Related
Given a client request of the form
return webClient.get()
.uri(customersUrl + id)
.retrieve()
.bodyToMono(Customer.class)
.block();
I would like to have a scriptable breakpoint at some place in the outgoing request where I can obtain the http headers, including any span id headers added by sleuth (and before any SSL encryption takes place). Preferably this breakpoint should be somewhere on the same thread as the above request - if the breakpoint is on the async thread that actually does the socket write, there is a danger that the next breakpoint hit will be that initiated by another client request.
There is a real reason why I need to use breakpoints like this so I can't do anything that requires modifying the code.
I have found places on the async thread where the http headers are available - e.g. in reactor/netty/http/HttpOperations.then but I am struggling to find a similar place on the original client thread. I'm hoping this isn't all deferred via lambdas or something until the async thread consumes the subscription.
I have several OkHttp requests that I need to send and even if there is no internet for some time, in the end they have to be delivered. It seems that WorkManager is a good API that can handle this.
But how to integrate OkHttp requests and WorkManager?
And (second) be able to get notifications?
I have recently used same as you want:
You can do that like:
#NonNull
#Override
public Result doWork() {
// DO OKHTTP REQUEST HERE........
return Result.success();
}
Note : Do not use any UI thread here like progress bar.
Sometime we need send a message asynchronous and provide a callback when message result asynchronous returned.
Now,there is only blocking method sendAndReceive;why not provider a sendAndAsyncReceive method by pass a callback arg or return a listentablefuture?
Because nobody has asked for it.
You can use send() and configure an async consumer (SimpleMessageListenerContainer) to receive the replies.
Feel free to open a new feature JIRA issue or, even better, consider contributing.
EDIT:
Here's a gist with sample code.
Although HTTP is a stateless protocol, there's this PushStreamContent class that facilitates server-sent events, as you can read here. A Web API implementation may store client streams and periodically send push updates. However, it's a bit of a problem to detect when the client disconnects.
In this discussion, Henrik Nielsen states that:
Detecting that the TCP connection has been reset is something that the Host (ASP, WCF, etc.) monitors but in .NET 4 neither ASP nor WCF tells us (the Web API layer) about it. This means that the only reliable manner to detect a broken connection is to actually write data to it. This is why we have the try/catch around the write operation in the sample. That is, responses will get cleaned up when they fail and not before.
In .NET 4.5 there is a mechanism for detecting client disconnect but I haven't tried it out.
Fast forward two and a half years to today. Does anyone have any idea what this mechanism is, and whether it works?
An interesting response was post on this link : link to the article
this is a part of the answer
In .NET 4.5 there is a mechanism for detecting client disconnect but I haven't tried it out.
This (simplified) code works for me:
public HttpResponseMessage Get(HttpRequestMessage request)
{
// Register for client disconnect notifications
object clientId = ...; // this is the object passed to the callback
System.Web.HttpContext.Current.Response.ClientDisconnectedToken.Register(
delegate (object obj)
{
// Client has cleanly disconnected
// obj is the clientId passed in to the register
// handle client disconnection
//...
}
, clientId );
// Normal code from the sample continues
response.Content = new PushStreamContent(...);
}
The ClientDisconnect callback is called back if e.g. you close the client browser page cleanly, but if you pull the network cable out, it does not get notified. So for this unclean disconnection, we still need some other way of detecting this, such as monitoring failures during the timer callback.
I have created simple async controllers that call into async methods that then call PostAsync on the HttpClient to retrieve various REST service endpoints. All works well, but how can I test to insure that the controller is really calling a background thread and releasing the primary thread back to the pool? I want to insure that I do have all the async sweetness working correctly and that I am not inadvertently running synchronous methods despite all my work to make everything async.
I found that System.Threading.Thread.CurrentThread.IsThreadPoolThread will provide whether the current thread is still a threadpool thread or not.