sendNativeMessage sometimes fails? Magic :/

I’m trying to write an WebExtension that sends a message to the native app. Unfortunately, something strange is going on. Things just do not work!

The app is very simple, it just gives back what it gets, here’s the code for Node.JS:

#!/usr/local/bin/node

let fs = require('fs');
let message = fs.readFileSync('/dev/stdin').toString();
fs.writeFileSync('/tmp/out', message);
process.stdout.write(message);

It works when run from shell, and /tmp/out is just for debugging.

The native app manifest at /Library/Application Support/Mozilla/NativeMessagingHosts/com.letterprinter.json:

{
  "name": "com.letterprinter",
  "description": "LetterPrinter",
  "path": "/Users/iliakan/bin/letterPrinter",
  "type": "stdio",
  "allowed_extensions": [ "letterPrinter@javascript.ru" ]
}

Sometimes the app works, so it seems that the manifest is not broken.

And now the extension:

browser.browserAction.onClicked.addListener(() => {
  let message = { data: Date.now() };

  console.log("Sending message", message);
  browser.runtime.sendNativeMessage('com.letterprinter', message).then(
    response => console.log("response", response),
    error => console.log("error", error)
  );

});

The manifest for the extension:

{
   ...
  "applications": {
    "gecko": {
      "id": "letterPrinter@javascript.ru",
      "strict_min_version": "50.0"
    }
  },
  "background": {
    "scripts": ["background.js"]
  },
  "permissions": [
    "nativeMessaging"
  ]
}

The problem is: sometimes that does not work: I see in console smth like “Sending message Object { data: 1496769055091 }”, but the app doesn’t run.

What’s wrong here?

I tried to run it in Chrome, but it fails too, so probably there’s something I miss.

P.S. I feel it should run the app, send it the message to stdin, get back the output and close the app – every time when browser_action runs. Is it correct?

P.P.S. The console doesn’t show neither “error” nor “response”, don’t know why. The promise does not settle? Ugh.

Thanks a lot!