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

timing issue establishing a mailbox #11

Open
bsutton opened this issue Mar 29, 2024 · 2 comments
Open

timing issue establishing a mailbox #11

bsutton opened this issue Mar 29, 2024 · 2 comments

Comments

@bsutton
Copy link

bsutton commented Mar 29, 2024

DCli is attempting to use a mailbox to spawn process synchronously.

My primary isolate spawns a secondary isolate which runs a process.

The primary isolate needs to call take() to receive the secondary isolate's send port.

The issue is that we have a timing problem.

If I call take() in the primary isolate before the secondary isolate has a chance to spawn then take() essentially blocks the secondary isolate from spawning.

I can't use an await when spawning the secondary isolate as the core reason for using a mailbox is to spawn the process synchronously.

@bsutton
Copy link
Author

bsutton commented Mar 31, 2024

This code demonstrates the problem.
As you can see it requires main to be async, which means we don't have a sync solution dispite the use of mailbox.

void main() async {
  final mailbox = Mailbox();

  void request(String msg) {
    print('sending $msg');
    final response = ascii.decode(mailbox.sendRequest(ascii.encode(msg)));
    print('got back: $response');
  }

  final worker = await Isolate.spawn((mailboxAddr) {
    print('Isolate is running');
    Mailbox.fromAddress(mailboxAddr).messageLoop((mailbox, msg) {
      final str = ascii.decode(msg);
      if (str == 'exit') {
        mailbox.isRunning = false;
      }
      print('got $str from mailbox, responding');
      return ascii.encode('response($str)');
    });
    print('Isolate is done');
  }, mailbox._mailbox.address);

  for (var i = 0; i < 5; i++) {
    request('request #$i');
  }
  request('exit');
}

@ntkme
Copy link
Contributor

ntkme commented Jul 29, 2024

I think the issue is not really how mailbox works, but no way to synchronously spawn isolates.

@devoncarew devoncarew transferred this issue from dart-archive/native_synchronization Sep 26, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants