Skip to content

http requests throw when called from Isolate.run() but work when called from compute() #52336

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Wizzel1 opened this issue May 10, 2023 · 6 comments

Comments

@Wizzel1
Copy link

Wizzel1 commented May 10, 2023

  • Dart v2.19.6
  • MacOS

I am trying to execute a http request from an Isolate.

Future<String> myRequest(String argument) async {
  final response = await http.get(
   ///request Code
  );
  return response.body;
}

when I try to run it with Isolate.run() like this

 final test = await Isolate.run(
        () => myRequest(argument),
      );

I am getting an error :
ArgumentError (Invalid argument(s): Illegal argument in isolate message: (object extends NativeWrapper - Library:'dart:ui' Class: Paragraph))
but when I run it with compute

 final test = await compute(myRequest, argument);

it works. Is this intended?

@Wizzel1 Wizzel1 changed the title http requests throw when called from Isolate.run but work when called from compute() http requests throw when called from Isolate.run() but work when called from compute() May 10, 2023
@julemand101
Copy link
Contributor

Are you running your code in a browser?
Also, it seems you forgot to paste the error message into your issue. Can you please add it?

@Wizzel1
Copy link
Author

Wizzel1 commented May 10, 2023

@julemand101
No, it is running on a mac. Sorry, I have edited the error message.

@lrhn
Copy link
Member

lrhn commented May 10, 2023

Definitely not running in a browser, since dart:isolate is not available on the web.

My best guess is that the closure () => myRequest(argument) closes over more state than necessary, and some of that state cannot be sent. (And the error message supports that.)

Try adding a top-level helper like:

R Function() curryFunction<R, T>(R Function(T) function, T argument) => () => function(argument);

and do:

final test = await Isolate.run(curryFunction(myRequest, argument));`

That should ensure that the ()=>myRequest(argument) closure is created in a scope with no extra variables to over-capture.

Q.v. #36983

@Wizzel1
Copy link
Author

Wizzel1 commented May 10, 2023

Yes, now it works. However, I don't quite understand the problem. Is this a mistake I made or a bug in dart?

@lrhn
Copy link
Member

lrhn commented May 10, 2023

I think it's a bug, which is why #36983 (and the duplicate #50462 that I filed) exist.

It's a gnarly problem for the VM implementation, because it goes to the heart of their closure implementation, but I hope it will be fixed eventually. (Preferably soon, because this is just the most visible way to see the problem, but overcapturing can also have a memory cost if large values are kept alive, even if nobody needs them.)

@Wizzel1
Copy link
Author

Wizzel1 commented May 10, 2023

I see. Thanks for your quick help!

@Wizzel1 Wizzel1 closed this as completed May 10, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants