About 50% of the time, the library I am working on freezes on a certain network call that is making use of HttpClient.SendAsync(). I am working on getting a smaller test case to submit, but this problem only happens on Unity iOS (Mono2x has different problems with EXC_BAD_ACCESS at the moment so cannot confirm if it works there or not, but on Standalone this works fine). When I pause to get the thread stack traces I notice a similar pattern each time:
There are two threads labeled Async I/O Thread that are both blocking on socket receive functionality.
I have one thread of my own blocking on receiving data from a hanging HTTP connection (this is on purpose)
No other threads go through any of my code, which suggests that the problem is between SendAsync and ContinueWith
Before I get an actual reproduction case ready, is there anything anyone can think of that could be causing this? Does IL2CPP hard limit to two HTTP connections the way that Mono does by default? Can this be altered?
I’m not aware of what might be causing this, and I don’t think that it is something we’ve seen yet with IL2CPP. The behavior of IL2CPP should be the same as it is with Mono in this case, and IL2CPP does not expose any way to modify it.
I have probably similar issue with IL2CPP and Unity 2019.1.0f2. Some await HttpClient.SendAsync() works and some get stuck. Probably the problem is when await HttpClient.SendAsync() is called from non-main thread, i.e. without Unity SynchronizationContext. The process (window) cannot be closed, probably waiting for SendAsync() to finish.
I’ll try to isolate the issue to some example project.
Another problem is, that the HttpClient ignores Windows proxy settings. I have found a workaround for the Mono build:
var handler = new HttpClientHandler();
handler.UseDefaultCredentials = true;
handler.Proxy = System.Net.WebRequest.DefaultWebProxy;
var client = new HttpClient(handler);
I’m using 2020.3.30 and encountered an issue about HttpClient. The process may become freezing for a while when it calls Task.Delay in main thread and receive response from HttpClient in a background thread.
Actually, I found the root cause: the timers created in the background thread leaks and it has been fixed in 2021.2 and above. Hope the fix can be backported to 2020.
@JoshPeterson - this issue is still evident in unity 2023.1.16f1:-
try
{
HttpClientHandler handler = new HttpClientHandler();
HttpClient client = new HttpClient(handler);
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, "https://127.0.0.1:9976/services/test");
request.Headers.Add("Authorization", "Basic " + Convert.ToBase64String(Encoding.ASCII.GetBytes("user:pass")));
request.Content = new StringContent("user=user345");
request.Content.Headers.ContentType = new MediaTypeHeaderValue("application/x-www-form-urlencoded");
HttpResponseMessage response = await client.SendAsync(request); //Freeze happens here
response.EnsureSuccessStatusCode();
return await response.Content.ReadAsStringAsync();
}
catch
{
return null;
}
Any workarounds for this? Symptoms are that when the SendAsync function executes, it hangs the unity editor with the progress dialog stuck. VS will exit fine and return to development mode.
@xmedeko - incorrect assumption, this is related to the HttpClient.