http
http copied to clipboard
RetryClient doesn't work in isolates
We cannot use RetryClient in Isolate.run
because of the use of StreamController
in StreamedRequest
for making a copy of requests.
Example
main.dart
import 'dart:isolate';
import 'package:http/http.dart';
import 'package:http/retry.dart';
import 'test_server.dart';
void main() async {
final server = await startTestHttpServer();
Future<void> _testPost(Client Function() clientFactory, String host) async {
await Isolate.run(
() => clientFactory().post(
Uri.http(host, '/hello'),
body: 'Hello World!',
),
);
}
await _testPost(() => RetryClient(Client()), server.uri.authority);
server.server.close();
}
test_server.dart
import 'dart:io';
import 'package:shelf_router/shelf_router.dart';
import 'package:shelf/shelf.dart';
import 'package:shelf/shelf_io.dart' as io;
import 'package:http/http.dart' as http;
typedef TestServer = ({HttpServer server, Uri uri});
Future<TestServer> startTestHttpServer(
void Function(Router router) onRouter,
) async {
var app = Router();
app.post('/hello', (Request request) {
return Response.ok('ok');
});
final server = await io.serve(app, 'localhost', 8080);
final serverUri = Uri.http('${server.address.host}:${server.port}');
return (server: server, uri: serverUri);
}
Error Logs
Unhandled exception:
Invalid argument: <- Instance of '_SyncStreamController<List<int>>' (from dart:async)
<- Instance of 'StreamedRequest' (from package:http/src/streamed_request.dart)
<- Instance of 'Response' (from package:http/src/response.dart)
<- _List len:1 (from dart:core)
is unsendable object (see restrictions listed at`SendPort.send()` documentation for more information): Instance of 'Future<void>'
#0 Isolate._exit (dart:isolate-patch/isolate_patch.dart:647:75)
#1 Isolate.exit (dart:isolate-patch/isolate_patch.dart:650:5)
#2 _RemoteRunner._run (dart:isolate:1031:13)
<asynchronous suspension>