Flutter Android emulator timeout

I was testing a simple api in combination with the dart client in flutter.
I run my server locally on the same machine where the ios and android emulators are running.
The ios emulator has no issues; the android emulator has issues. A simple call is working, but from the moment I do some extra stuff in my api, there are timeout errors. Please note that the deployed version is working fine. So it’s a combination of using the (to be used as localhost).
So the post call executes fine, I also see in my logs that he has “responded” but then nothing happens; after a while I see following in flutter output:

E/flutter ( 1689): [ERROR:flutter/lib/ui/ui_dart_state.cc(148)] Unhandled Exception: Instance of 'WebServiceException'
E/flutter ( 1689): #0      JsonServiceClient.handleError (package:servicestack/client.dart:544:5)
E/flutter ( 1689): <asynchronous suspension>
E/flutter ( 1689): #1      JsonServiceClient.sendRequest (package:servicestack/client.dart:318:20)
E/flutter ( 1689): <asynchronous suspension>
E/flutter ( 1689): #2      JsonServiceClient.send (package:servicestack/client.dart:245:12)
E/flutter ( 1689): #3      JsonServiceClient.post (package:servicestack/client.dart:110:12)
E/flutter ( 1689): #4      main (package:scoryo/main.dart:21:39)
E/flutter ( 1689): <asynchronous suspension>
E/flutter ( 1689): #5      _runMainZoned.<anonymous closure>.<anonymous closure> (dart:ui/hooks.dart:189:25)
E/flutter ( 1689): #6      _rootRun (dart:async/zone.dart:1124:13)
E/flutter ( 1689): #7      _CustomZone.run (dart:async/zone.dart:1021:19)
E/flutter ( 1689): #8      _runZoned (dart:async/zone.dart:1516:10)
E/flutter ( 1689): #9      runZoned (dart:async/zone.dart:1500:12)
E/flutter ( 1689): #10     _runMainZoned.<anonymous closure> (dart:ui/hooks.dart:180:5)
E/flutter ( 1689): #11     _startIsolate.<anonymous closure> (dart:isolate/runtime/libisolate_patch.dart:300:19)
E/flutter ( 1689): #12     _RawReceivePortImpl._handleMessage (dart:isolate/runtime/libisolate_patch.dart:171:12)
E/flutter ( 1689): 

and at the same time this in my net core output:

[16:11:05 ERR] ServiceBase<TRequest>::Service Exception
System.Net.Http.HttpRequestException: Operation timed out ---> System.Net.Sockets.SocketException: Operation timed out
   at System.Net.Http.ConnectHelper.ConnectAsync(String host, Int32 port, CancellationToken cancellationToken)
   --- End of inner exception stack trace ---
   at System.Net.Http.ConnectHelper.ConnectAsync(String host, Int32 port, CancellationToken cancellationToken)
   at System.Threading.Tasks.ValueTask`1.get_Result()
   at System.Net.Http.HttpConnectionPool.CreateConnectionAsync(HttpRequestMessage request, CancellationToken cancellationToken)
   at System.Threading.Tasks.ValueTask`1.get_Result()
   at System.Net.Http.HttpConnectionPool.WaitForCreatedConnectionAsync(ValueTask`1 creationTask)
   at System.Threading.Tasks.ValueTask`1.get_Result()
   at System.Net.Http.HttpConnectionPool.SendWithRetryAsync(HttpRequestMessage request, Boolean doRequestAuth, CancellationToken cancellationToken)
   at System.Net.Http.RedirectHandler.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
   at System.Net.Http.DecompressionHandler.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
   at System.Net.Http.HttpClient.FinishSendAsyncUnbuffered(Task`1 sendTask, HttpRequestMessage request, CancellationTokenSource cts, Boolean disposeCts)
   at System.Net.HttpWebRequest.SendRequest()
   at System.Net.HttpWebRequest.GetResponse()

Any idea?

Not sure, searching for Operation timed out issues with flutter + Android loop back shows me this:

adb reverse tcp:5000 tcp:5000

Or use adb reverse --list to see what redirects are set up.

Thanks for the pointer, but it did not help; tried different ports as well, ended up with the same. The connection is maded, the call is executed, but it seems that it is not “returning” and waits for that; that’s where the timeout appears. As said, on a deployed service this is working, so I need to look into the localhost thing with android…

Ok I found the issue…

Long story short, I was trying to create a kind of auto-login for the app, basically creating a random user and pasword, then do a “login” in order to get the bearer and refresh tokens. I was using in my call the Register calls, but because I had no idea to get the bearer AND refresh token, I used a JsonServiceClient intsance on the same URL to do the Login. This was causing (for android) and issue.

I now am doing:

var service = ResolveService<AuthenticateService>();
			var authenticate = new Authenticate()
				provider = "credentials",
				UserName = userName,
				Password = password

			var loginResponse = service.Post(authenticate) as AuthenticateResponse;

Do get the same. Not sure this is a good practice?

I found in the documentation a way to create a JWT token manually, but I was unable to create a refresh token manually…

It’s a way to get the Bearer Access/Refresh Tokens for a specific user, although for internal service requests I prefer to use the Service Gateway which provides better decoupling and flexibility.

Although Authentication is an expensive way to fetch Auth/Bearer Tokens, ok if you just had to do it once per App Session, but not in a reoccurring hot path.

Did you mean these docs for creating JWT Tokens Manually? as that would be the most efficient way to create an adhoc JWT internally.

For creating a Refresh Token, in ServiceStack you can just do:

var jwtAuth = AuthenticateService.GetJwtAuthProvider();
var refreshToken = jwtAuth.CreateJwtRefreshToken(base.Requset, userAuthId, expireIn);

Ok, thanks, that seemed to work.
However, in order to get the correct class I needed to

var jwtAuth = (JwtAuthProvider) AuthenticateService.GetJwtAuthProvider();

Also there is a small error on the doc https://docs.servicestack.net/jwt-authprovider#creating-jwt-tokens-manually where audience is an array now -> audience: new [] { jwtProvider.Audience },

But all the rest is working. Thanks!

1 Like

updated JWT docs, thx.