Dart Client -> Connection leak

Hi

We have an issue when using dart client to access our api.
After our app is running a while, with calls against the api in the background, we get errors like “too many files open” which means the File Descriptors are running out.

During investigations on the ipad simulator i found out that a lot of connections are in state “established” but are not being closed by the service client.

Result of lsof (The list is much longer but it’s always the same entry)

In the code we have an instance of the jsonserviceclient, which we then use with “normal” get requests

var result = await client.get(request)

I tried to set the Connection Header to close but it didn’t help.
It looks like either the Servicestack client or the HttpClient of Dart is not closing the connection.

Are you reusing the same JsonServiceClient instance or creating different instances per request?

yes we use a single instance for the background worker

Don’t know then, the only place you can close a HttpClientRequest is before you get a response so I’m not seeing where the leak is if you’re using reusing the same JsonServiceClient instance.

Can you provide a small repro example that causes a connection leak?

I tried now to close the http client after each run of multiple requests and now it doesn’t happen anymore but i have now to create a new instance before making some requests.

To close i use
serviceClient.client.close(force: true)

I got that approach while searching the bug:
https://api.flutter.dev/flutter/dart-io/HttpClient-class.html
Section: Closing the HttpClient

Without the force: true it leaks as well

So the connections seem to stay active, that’s why without the force parameter it leaks


I created a simple repo which shows the connection leak

ok this should now be resolved with the latest v1.0.23 that’s now available on pub.

There’s also now a close({bool force = false}) API on IServiceClient should you also need to terminate the persistent connection.

Thank you for fixing it. I wanted to try it out now but i have a dependency issue.
Servicestack is using dart 2.12 which is not stable yet

Because servicestack >=1.0.23 depends on http >=0.13.0-nullsafety.0 which requires SDK version >=2.12.0-0 <3.0.0, servicestack >=1.0.23 is forbidden.


Still has 2.10.5 as stable. Is it possible to lower the required version to 2.10.5?

The latest servicestack 1.0.25 has now downgraded to use http 0.12.2.

Great thank you for solving this so fast. The issue is now fixed.

1 Like