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

SendPort.send should only accept "literal-like" objects. #21398

Closed
lrhn opened this issue Oct 23, 2014 · 10 comments
Closed

SendPort.send should only accept "literal-like" objects. #21398

lrhn opened this issue Oct 23, 2014 · 10 comments
Assignees
Labels
area-vm Use area-vm for VM related issues, including code coverage, FFI, and the AOT and JIT backends. library-isolate

Comments

@lrhn
Copy link
Member

lrhn commented Oct 23, 2014

When sending objects through send-ports, only literal-like objects should be sent.
That is:

  • number
  • String
  • bool
  • null
  • List of literal-like objects
  • Map of literal-like keys and values.
    I should handle object cycles.

It's not clear whether a fixed-length List should preserve it's fixed-lengthness (it doesn't in dart2js), or how a user-defined class implementing List should be handled (dart2js converts to normal List).

Similar for maps - what happens for an identity-HashMap, SplayTreeMap or a user defined map isn't clear. The simplest solution is to convert all to a basic LinkedHashMap.

(When sending to isolates with the same classes - as created by Isolate.spawn from the same root - we also want to extend this with top-level/static functions).

@DartBot
Copy link

DartBot commented Oct 23, 2014

This comment was originally written by @zoechi


Currently it's also possible to send arbitrary Dart objects at least on the server (when they don't import native libraries). Should this be removed? I thought this should be added on the client too instead.

@lrhn
Copy link
Member Author

lrhn commented Oct 28, 2014

This only applies to ports between isolates that haven't been created using Isolate.spawn from the same root isolate.

@a-siva
Copy link
Contributor

a-siva commented Nov 14, 2014

Set owner to @a-siva.

@sethladd
Copy link
Contributor

When we do this, let's also update the dartdocs on SendPort (which currently implies you in "special circumstances" you can send over objects). Thanks!

@sethladd
Copy link
Contributor

There's a current bug, which fails silently. If we change SendPort.send to only send literal-like objects, then this bug will be addressed (I assume). In the meantime, the following code will NOT print 'here', and no exception or error is thrown or detected:

import 'dart:isolate';
import 'dart:async';
//import 'shared.dart';

class FromMainIsolate {
  String toString() => 'from main isolate';
}

main() {
  new Timer(const Duration(seconds: 1), () => print('timer done'));
  var receive = new ReceivePort();

  Isolate.spawnUri(Uri.parse('loadme.dart'), [], receive.sendPort).then((isolate) {
    print('isolate started');
    receive.listen((msg) {
      print('here');
      print("$msg");
    }, onError: (e) => print('$e'));
  })
  .catchError(print);

}


import 'dart:isolate';

//import 'shared.dart';

class FromChildIsolate {
  String toString() => 'from child isolate';
}

main(List<String> args, message) {
  print('in child isolate: $message');
  var sendPort = message as SendPort;
  sendPort.send(new FromChildIsolate());
}

@sethladd
Copy link
Contributor

FWIW it's very confusing if SendPort's behavior/capability is different if it comes in via either spawn or spawnUri. The user of the SendPort doesn't know if it came from spawn or spawnUri. There's no way (afaict) to ask "can I send an arbitrary object via this SendPort?"

@lrhn
Copy link
Member Author

lrhn commented Nov 14, 2014

It is confusing, but the difference is already there - currently it just crashes if you send objects to an isolate where the class isn't loaded.

We could add a boolean getter on SendPort that tells you whether the receiving port is in an equivalent isolate (spawned from the same isolate through Isolate.spawn calls). It might even be a good idea, and it can be done fairly efficiently.

It's actually not clear from this issue's description what the VM should do when sending to a non-equivalent isolate. Dart2js does something similar to JSON encoding, so any List becomes a default (Growable)List, any Map becomes a default (LinkedHash)Map when deserialized, no matter what kind they were before.
It's not clear if this is desired behavior when, e.g., serializing an IdentityHashMap or a fixed-length List. It's also not clear what a good alternative is.

@iposva-google
Copy link
Contributor

Added Accepted label.

@a-siva
Copy link
Contributor

a-siva commented Jan 13, 2015

@iposva-google
Copy link
Contributor

Issue #15983 has been merged into this issue.

@lrhn lrhn added Type-Defect area-vm Use area-vm for VM related issues, including code coverage, FFI, and the AOT and JIT backends. library-isolate labels Feb 17, 2015
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-vm Use area-vm for VM related issues, including code coverage, FFI, and the AOT and JIT backends. library-isolate
Projects
None yet
Development

No branches or pull requests

6 participants