Skip to content
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

Explain implicit copying of variables on 'Isolates' page #5843

Open
1 task
mmcdon20 opened this issue May 18, 2024 · 0 comments
Open
1 task

Explain implicit copying of variables on 'Isolates' page #5843

mmcdon20 opened this issue May 18, 2024 · 0 comments
Labels
a.language Relates to the Dart language tour e2-days Can complete in < 5 days of normal, not dedicated, work from.page-issue Reported in a reader-filed concern st.triage.ltw Indicates Lead Tech Writer has triaged

Comments

@mmcdon20
Copy link

mmcdon20 commented May 18, 2024

Page URL

https://dart.dev/language/isolates

Page source

https://github.com/dart-lang/site-www/tree/main/src/content/language/isolates.md

Describe the problem

The current section titled "Sending closures with isolates" does not explain that variables captured by the closure are implicitly copied into the new isolate.

See the following example:

import 'dart:isolate';

void main() async {
  int i = 5;
  print(i); // 5
  await Isolate.run(() {
    print(i); // 5
    i = 10;
    print(i); // 10
  });
  print(i); // 5 "i" is unchanged in main isolate
}

Note that modifying i in the new isolate does not affect the value of i in the main isolate, because i was implicitly copied into the new isolate.


One issue that people sometimes run into is that when refencing a class property in a closure sent to an isolate, the rest of the class instance gets copied as well. This can especially be an issue if the class contains an object of a type that is not allowed to be sent to an isolate (see https://api.dart.dev/stable/3.4.0/dart-isolate/SendPort/send.html).

See the following example:

import 'dart:isolate';

class Example {
  ReceivePort port = ReceivePort();
  int i = 5;

  Future<void> run() async {
    await Isolate.run(() {
      print(i);
    });
  }
}

void main() async {
  await Example().run();
}

The above code fails with the following exception:

Unhandled exception:
Invalid argument(s): Illegal argument in isolate message: object is unsendable - Library:'dart:isolate' Class: _ReceivePortImpl@1026248 (see restrictions listed at `SendPort.send()` documentation for more information)

A ReceivePort is one of the types of objects restricted from being sent to another isolate. Even though the port variable was not explicitly referenced, it was still implicitly copied over due to i being referenced. I believe this happens because print(i); is shorthand for print(this.i); in the above example.

Expected fix

Add some explanation about implicit copying of variables, and potential issues.

Additional context

No response

I would like to fix this problem.

  • I will try and fix this problem on dart.dev.
@mmcdon20 mmcdon20 added the from.page-issue Reported in a reader-filed concern label May 18, 2024
@atsansone atsansone changed the title [PAGE ISSUE]: 'Isolates' - explain implicit copying of variables Explain implicit copying of variables on 'Isolates' page May 20, 2024
@atsansone atsansone added a.language Relates to the Dart language tour e2-days Can complete in < 5 days of normal, not dedicated, work st.triage.ltw Indicates Lead Tech Writer has triaged labels May 20, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
a.language Relates to the Dart language tour e2-days Can complete in < 5 days of normal, not dedicated, work from.page-issue Reported in a reader-filed concern st.triage.ltw Indicates Lead Tech Writer has triaged
Projects
None yet
Development

No branches or pull requests

2 participants