Flutter NO_RENEGOTIATION(ssl_lib.cc:1725) error 268435638 - flutter

I'm not sure why this API I'm requesting is throwing this error for only dart. For some reason dart throws this error when I attempt to make a POST request:
Unhandled Exception:
NO_RENEGOTIATION(ssl_lib.cc:1725) error 268435638
#0 IOClient.send (package:http/src/io_client.dart:90:7)
I tried the same endpoint in both Python, Java and even manually and it worked fine. It worked fine with a local server, and then when I went to push to production I encountered this problem.

add this in main
void main() {
final context = SecurityContext.defaultContext;
context.allowLegacyUnsafeRenegotiation = true;
final httpClient = HttpClient(context: context);
runApp(const MyApp());
}

Related

embedding golang server with flutter

I have web server written in golang which uses graphql package gqlgen and gorm for database.
Since golang can be compiled and run on android I wanted to create offline version of my app where sqlite can be used for offline storage and import my whole server as an aar.
I have successfully built aar and add it on my flutter using gomobile by following instructions here
When I run my app, server is started on android it seems to work just fine and when opening http://localhost:8080/ on emulator's chrome app I am seeing graphql playground runs without any problem just like I see on browser in windows.
The only problem I face is that flutter app just shows a blank screen while server runs in background. The following are the logs printed when app is started
Launching lib\main.dart on sdk gphone64 x86 64 in debug mode...
Running Gradle task 'assembleDebug'...
√ Built build\app\outputs\flutter-apk\app-debug.apk.
Installing build\app\outputs\flutter-apk\app.apk...
Debug service listening on ws://127.0.0.1:62561/DyGpOhyuekw=/ws
Syncing files to device sdk gphone64 x86 64...
I/GoLog ( 6295): connect to http://localhost:8080/ for GraphQL playground
W/ux.offline( 6295): type=1400 audit(0.0:38): avc: denied { read } for name="somaxconn" dev="proc" ino=74990 scontext=u:r:untrusted_app:s0:c149,c256,c512,c768 tcontext=u:object_r:proc_net:s0 tclass=file permissive=0 app=com.nux.offline
I think maybe problem lies on the above logs avc: denied { read } for name="somaxconn" or something is causing the blocking of ui thread since its like flutter don't render a thing.
I am using flutter plugin to start server and this is ServerPlugin.kt
package com.mahesabu.server.server
import androidx.annotation.NonNull
import io.flutter.embedding.engine.plugins.FlutterPlugin
import io.flutter.plugin.common.MethodCall
import io.flutter.plugin.common.MethodChannel
import io.flutter.plugin.common.MethodChannel.MethodCallHandler
import io.flutter.plugin.common.MethodChannel.Result
import lib.Lib.startServer
/** ServerPlugin */
class ServerPlugin : FlutterPlugin, MethodCallHandler {
/// The MethodChannel that will the communication between Flutter and native Android
///
/// This local reference serves to register the plugin with the Flutter Engine and unregister it
/// when the Flutter Engine is detached from the Activity
private lateinit var channel: MethodChannel
override fun onAttachedToEngine(#NonNull flutterPluginBinding: FlutterPlugin.FlutterPluginBinding) {
channel = MethodChannel(flutterPluginBinding.binaryMessenger, "server")
channel.setMethodCallHandler(this)
}
override fun onMethodCall(#NonNull call: MethodCall, #NonNull result: Result) {
if (call.method == "startServer") {
try {
val port = startServer() //from golang bindings
result.success(port)
} catch (e: Exception) {
e.printStackTrace();
result.error("Error in starting server", "${e.message}", null);
}
} else {
result.notImplemented()
}
}
override fun onDetachedFromEngine(#NonNull binding: FlutterPlugin.FlutterPluginBinding) {
channel.setMethodCallHandler(null)
}
}
And this is is dart code
class Server {
static const MethodChannel _channel = MethodChannel('server');
static Future<String?> startServer() async {
try {
final String? port = await _channel.invokeMethod('startServer');
return port;
} catch (e) {
log('startServer error: ${e.toString()}');
return null;
}
}
}
and my app's main is as follows
import 'package:flutter/material.dart';
import 'package:server/server.dart';
void main() async {
WidgetsFlutterBinding.ensureInitialized();
final port = await Server.startServer(); //problem
print('port $port'); //This don't print
runApp(const MyApp());
}
On go side this how i start server
//start.go
package util
import (
"github.com/99designs/gqlgen/graphql/handler"
"github.com/99designs/gqlgen/graphql/playground"
"log"
"my-project/graph"
"my-project/graph/generated"
"net/http"
"os"
)
const defaultPort = "8080"
// StartServer This way so that it can be invoked via libs
func StartServer(Offline bool) string {
port := os.Getenv("PORT")
if port == "" {
port = defaultPort
}
db := InitDB(Offline)
config := generated.Config{Resolvers: &graph.Resolver{
DB: db,
}}
srv := handler.NewDefaultServer(generated.NewExecutableSchema(config))
http.Handle("/", playground.Handler("GraphQL playground", "/query"))
http.Handle("/query", srv)
log.Printf("connect to http://localhost:%s/ for GraphQL playground", port)
log.Fatal(http.ListenAndServe(":"+port, nil))
return port
}
and this is lib for generating bindings with gomobile
//lib.go
package lib
import "my-project/util"
// StartServer This way so that it can be invoked via libs
func StartServer() string {
return util.StartServer(true)
}
Any help on fixing this will be appreciated.
Edit
I think problem occurs when embedded server tries to create a new port. I don't know if it is possible for an app to open a new port in android just like nodejs, golang open things like http://localhost:8080.
Now I think if there is a way to create port then I can run my app successfully but I don't know how exactly.
I was thinking if I can find any available port on android and use to start server maybe this stack could be possible. In kotlin something like this may work in finding port.
import java.net.ServerSocket
fun main() {
val serverPort = ServerSocket(0)
print(serverPort.toString())
}
but it crashes on android app when I try similar thing.
I have uploaded a repository on GitHub showing what I intend to do. It's just a simple golang server using gin and android app (with no flutter) it is available here.
"I don't know if it is possible for an app to open a new port in android just like nodejs, golang open things like localhost:8080"
To find out the root cause, try to run an HTTP server in android, such as How to create a HTTP server in Android?. If that succeeds, try to find the differences about how they deal with ports.
In addition, please be sure you have correct permission in androidmanifest.xml.
(Rephrased from my comments)

SocketException: Failed host lookup: ‘...com’ (OS Error: nodename nor servname provided, or not known, errno = 8)

We are in a situation where the production app is facing the following socket exception and not able to perform any other network operation after this. 
DioError [DioErrorType.DEFAULT]: SocketException: Failed host lookup: ‘xyz.abc.com’ (OS Error: nodename nor servname provided, or not known, errno = 8)
Note: Encountered repetitively with one user having iPhone X, iOS 14.4
We are using Dio as a network client, with Retrofit, which internally uses the HttpClient from the dart. With Dio the exception is not reproducible with the simulated environment but using HttpClient directly, the same exception can be reproduced with the following code in iOS simulator.
HttpClient userAgent = new HttpClient();
bool run = true;
while (run) {
try {
await userAgent.getUrl(Uri.parse('https://www.google.com'));
print('Number of api executed');
} catch (e) {
print(e);
if (e is SocketException) {
if ((e as SocketException).osError.errorCode == 8)
print('***** Exception Caught *****');
}
}
}
Once the exception was thrown, the HttpClient was not able to recover from that stale state and all other API requests were started failing with the same error.
We were able to recover from that stale state by force closing all the previous connections and opening up a new HttpClient.
HttpClient userAgent = new HttpClient();
bool run = true;
while (run) {
try {
await userAgent.getUrl(Uri.parse('https://www.google.com'));
print('Number of api executed');
} catch (e) {
print(e);
if (e is SocketException) {
if ((e as SocketException).osError.errorCode == 8)
print('***** Exception Caught *****');
}
userAgent.close(force: true);
print('Force closing previous connections');
userAgent = HttpClient();
print('Creating new HttpClient instance');
}
}
One interesting fact is after every 236 requests the exception is raising. It could be because of file descriptors over usage but iOS has a limit of 256. 🙄
With a stable internet connection, this issue reproducible every time in iOS simulator.
Although I am not able to reproduce the issue with Dio client but as in production it is occurring. So I am seeking help to understand the root cause of this issue, also how we can prevent it?
Anyone who has come across this kind of situation and how you have overcome it, please help me.
Thanks in advance.
That's a strange error.
This might not answer your question, but may push us towards figuring out what's going on.
The code snippet (copied from question) will open up a new stream with each .getUrl() call and will not close them. (I'm assuming this is intentional to create the socket exception?)
HttpClient userAgent = new HttpClient();
bool run = true;
while (run) {
try {
await userAgent.getUrl(Uri.parse('https://www.google.com'));
print('Number of api executed');
} catch (e) {
print(e);
if (e is SocketException) {
if ((e as SocketException).osError.errorCode == 8)
print('***** Exception Caught *****');
}
}
}
At some point, a limit (of open streams) is hit. I guess that magic number is 236 in your case.
So at that point, is when you're seeing the nodename or servname provided exception?
(Btw, as an aside, I think that error is coming from the underlying host operating system's DNS service, although I'm not sure if it's due to the request spam, the number of open connections, etc. This may not be relevant info.)
So, if you used the HttpClient in a typical way, making requests & closing those open streams, such as this:
var request = await userAgent.getUrl(Uri.parse('http://example.com/'));
var response = await request.close(); // ← close the stream
var body = await response.transform(utf8.decoder).join();
// ↑ convert results to text
// rinse, repeat...
... Are you still seeing the same nodename or servname provided error pop up?
With this "typical usage" code immediately above, the userAgent can be reused until a userAgent.close() call is made (and the HttpClient is permanently closed.
Trying to use it again would throw a Bad State exception).
I'd be interested to hear if the nodename error still occurs with this modified code.
Re: the second code snippet from the question.
In the catch block, the HttpClient is closed, then a new HttpClient is created. This effectively closes all the open streams that were opened in the try block (and I assume, resetting the limit of open streams.)
If you adjusted the 2nd code example to use:
var req = await userAgent.getUrl(Uri.parse('https://www.google.com'));
userAgent.close(force: true);
userAgent = HttpClient();
print('Number of api executed');
Could you run that indefinitely?
i have same issue resolve with this code:-
Exmaple
//Add This Class
class MyHttpOverrides extends HttpOverrides{
#override
HttpClient createHttpClient(SecurityContext? context){
return super.createHttpClient(context)
..badCertificateCallback = (X509Certificate cert, String host, int port)=> true;
}
}
Future<void> main() async {
HttpOverrides.global = MyHttpOverrides(); //call here
runApp(const MyApp());
}
1:Obtain the current limit of file descriptors
ulimit -n
An example output: “256” or “10032”.
PROTIP: On MacOS, the maximum number that can be specified is 12288.
Obtain the current limit of processes
ulimit -u
An example output: “1418”.
sudo launchctl limit maxfiles 65536 200000
I got exactly the same errors in production, it happens intermittently. Like Baker said, close the connections:
import 'package:http/http.dart' as http;
Future<http.Response> get(String url) async {
var httpClient = http.Client() as http.BaseClient;
Map<String, String> headers = {};
headers['Content-Type'] = 'application/json; charset=UTF-8';
var result = await httpClient
.get(Uri.parse(url), headers: headers)
.timeout(
const Duration(seconds: 60),
onTimeout: () => http.Response('Request Timeout', 408),
);
httpClient.close();
return result;
}
I did 10x Future.Delayeds each doing a loop with 300 get requests at the same time, didn't find any issues.
The future delayeds was done like so:
Future.delayed(const Duration(milliseconds: 10), () async {
for (var i = 0; i < 300; i++) {
var pingResult = await Api.instance.ping();
print('Delayed 1 Result (${i}): ${pingResult.success}');
}
});
Future.delayed(const Duration(milliseconds: 10), () async {
for (var i = 0; i < 300; i++) {
var pingResult = await Api.instance.ping();
print('Delayed 2 Result (${i}): ${pingResult.success}');
}
});
//..

java.lang.RuntimeException: org.json.JSONException: JSONObject["_source"] not found. #Kuzzle

I am trying to update the document using java sdk, when I run my code kuzzle status is connected, it update the fields but neither comes in onSuccess() nor onError(). when I again request to updateDocument(), kuzzle state change from connected to error. I debug the issue and found this exception.
java.lang.RuntimeException: org.json.JSONException: JSONObject["_source"] not found.
as I am not getting "_source" in my code
kuzzle.collection("collection","testindex").updateDocument(obj.getDocumentId(), jsonObj, new ResponseListener<Document>() {
#Override
public void onSuccess(Document response) {
System.out.println("success"+response);
}
#Override
public void onError(JSONObject error) {
System.out.println("error"+error);
}
});
A fix has recently been released for this probleme. You should try with version 3.0.10 of the SDK: https://bintray.com/kuzzle/maven/kuzzle-sdk-android/3.0.10

flutterBlue.connect(device, timeout: const Duration(seconds: 10),).listen(null); in flutter

sorry,I am a beginner and my English is very bad
i have a trouble in Flutter.
i reference https://pub.dartlang.org/packages/flutter_blue
I want to use flutter to communicate with the ear thermometer device.
when i reopen bluetooth and it can work,but I reopen app and excute second it show error.
i guess bluetooth gatt cahce is causing a error.
how can i solve it?
my code
deviceConnection = flutterBlue.scan(timeout: const Duration(seconds: 5),).listen((scanResult) async {
device = scanResult.device;
deviceConnection2 = await flutterBlue.connect(device, timeout: const Duration(seconds: 10),).listen(null);//this line error is error 1
deviceStateSubscription = device.onStateChanged().listen((s) async {
await device.discoverServices().then((s) async {//this is error 2
.........
}
}
}
it show these error
error 1
Dart Error: Unhandled exception:
PlatformException(already_connected, connection with device already exists, null)
error 2
Dart Error: Unhandled exception:
PlatformException(get_services_error, no instance of BluetoothGatt, have you connected first?, null)
Error 1
Dart Error: Unhandled exception: PlatformException(already_connected, connection with device already exists, null)
Since the app detected that a bluetooth device is already connected, one workaround is check for any connected device disconnect it. You can create a function that does this like the one in this GitHub thread:
What you can do here is that you check for any connected devices and
then disconnect them. I have written a function that does just that,
here it is:
checkConnectedDevices() async{
connectedDevices = await flutterBlue.connectedDevices.whenComplete((){
print(connectedDevices);
for(int i = 0; i < connectedDevices.length; i++){
connectedDevices[i].disconnect();
}
});
}
Error 2
Dart Error: Unhandled exception: PlatformException(get_services_error, no instance of BluetoothGatt, have you connected first?, null)
Since there is an error connecting to the device, you will not be able to call the discoverServices(). Because you need to be connected to a device before you can call this function. Check the following GitHub discussions related to this error:
connected ' discoverServices ' is't get #426
discoverServices is broken #535

org.apache.http.NoHttpResponseException: XX.XX.XX.XX:443 failed to respond

Currently I am using Apache http components client V4.3.5. In my case, I can upload small file(1kb), but it is not working on large file(100kb) when I run the code and get the exception "org.apache.http.NoHttpResponseException: 192.168.128.109:443 failed to respond". Can anyone take a look at my code and let me know what causes my issue?
Here is my code:
public static void main(String[] args) throws IOException,
KeyStoreException {
try {
SSLContext sslContext = new SSLContextBuilder().loadTrustMaterial(
null, new TrustStrategy() {
public boolean isTrusted(X509Certificate[] chain,
String authType) throws CertificateException {
return true;
}
}).build();
SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(
sslContext,
SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
HttpClientBuilder builder = HttpClientBuilder.create();
builder.disableContentCompression();
builder.setSSLSocketFactory(sslsf);
SocketConfig config = SocketConfig.custom().setSoKeepAlive(true).setSoTimeout(300000).build();
builder.setDefaultSocketConfig(config);
CloseableHttpClient httpclient = builder.build();
HttpPost httppost = new HttpPost("https://192.168.128.109/upload");
String encodedAuthorization = DatatypeConverter
.printBase64Binary("admin:itronitr".getBytes());
httppost.setHeader("Authorization", "Basic " + encodedAuthorization);
FileBody bin = new FileBody(new File("c:\\test.txt"));
String boundary = "hK1oPL5_XSfbm6lkCNlKI63rltrew5Bqik0ul";
HttpEntity reqEntity = MultipartEntityBuilder.create()
.setBoundary(boundary).addPart("upfile", bin).build();
httppost.setEntity(reqEntity);
System.out.println(httppost.getEntity().getContentLength());
System.out
.println(httppost.getEntity().getContentType().toString());
CloseableHttpResponse response = httpclient.execute(httppost);
HttpEntity resEntity = response.getEntity();
System.out.println(response.getStatusLine());
String content = EntityUtils.toString(resEntity);
System.out.println(content);
} catch (Exception exc) {
exc.printStackTrace();
}
}
Thanks,
Bill
Finally I fix the issue and it is caused by buffer size. By default, buffer size of httpclient is 8k. So I change it to 4k and my code works well.
Here is the code that changes buffer size:
ConnectionConfig connectionConfig = ConnectionConfig.custom()
.setBufferSize(4128)
.build();
CloseableHttpClient httpclient = HttpClients.custom()
.setDefaultConnectionConfig(connectionConfig)
.build();
This is what worked for me; may or may not work for you!!
I recently encountered the same issue and tried all the suggestions whatever I was able to find on internet i.e upgrading httpClient to latest version and adding a re-try handler ; but none fixed it for me.
I already had a re-try handler built in my code and was running on the latest Apache client, but it was still failing with the exception Caused by: org.apache.http.NoHttpResponseException: xxxxx:443 failed to respond
So, took me almost 2 days to debug this issue and find the root cause (at-least in my case)
There seems to be a bug in older Java versions up to Java 11.0.3 included that prevents Apache HTTP Client from sending payloads bigger than 16368 bytes caused by https://bugs.java.com/bugdatabase/view_bug.do?bug_id=JDK-8214339.
I was running on java 11.0.2 and when I upgraded to 11.0.10, it worked for me and I was able to send the bigger payload without any code changes
I also faced the similar problem. I went through many blogs and forums and tried various things but none worked for me. So, I tried a workaround. I added retry handler as below. And it worked for me:
HttpClientBuilder.create()
.setDefaultCredentialsProvider(provider)
.setRetryHandler(new DefaultHttpRequestRetryHandler() {
#Override
public boolean retryRequest(final IOException exception, final int executionCount, final HttpContext context) {
if (exception instanceof NoHttpResponseException) {
return true;
}
return super.retryRequest(exception, executionCount, context);
}
})
.build();
Although it is not a correct fix and just a workaround but it is working for me for now. I'll stick to this solution till I won't get any permanent solution. Sharing it here in case someone might get benefit from it.