title: How to fix Native Host Has Exited error in Chrome browser extension -- Native Messaging
body: <p>How to fix a "Native host has exited" error in my Chrome browser extension for Mac?  The docs here on <a href="https://developer.chrome.com/extensions/nativeMessaging" rel="noreferrer">Native Messaging</a> specifically mention this error towards the bottom, and suggest a likely cause, but it is stated quite generally ("This is most likely initiated from your native messaging host") and it's unclear what to attempt to do to solve this.</p>
  
  <p>The logging output in Terminal (if you launch Chrome from the Mac Terminal you can view logging info) has led me to believe that the process for the native host app is itself is never successfully started.  I keep getting:</p>
  
  <pre><code>LaunchProcess: failed to execvp:
  /Users/mycomputer1/Documents/myhost.app
  </code></pre>
  
  <p>However, the actual code that prints "Native host has exited" to the console for the background javascript page is:</p>
  
  <pre><code>function onDisconnected() {
    console.log("Inside onDisconnected(): " + chrome.runtime.lastError.message);
    port = null;
  }
  </code></pre>
  
  <p>The onDisconnected() listener is actually invoked as a result of calling connectNative(), which I've done here according to the documentation:</p>
  
  <pre><code>chrome.runtime.onMessageExternal.addListener(
  function(request, sender, sendResponse) {
  //var imgdata = JSON.stringify(request.imgdata);
  //process it somehow here
  
      port = chrome.runtime.connectNative("com.allinlearning.ailhost");
  
      if (port)
      {
         console.log("connectNative() returned a non-null port");
      }
  });
  </code></pre>
  
  <p>I know that connectNative() succeeds at some level bc it returns a valid port. However, connectNative() goes on to attempt to locate the native host app and try to launch it.  The docs suggests that the host app is the culprit, somehow initiating the breaking of a "pipe" between Chrome and the native app, although my host app has implemented no communication with stdout or stdin at all.  Besides, the Chrome log msg ("LaunchProcess: failed to execvp:") seems to indicate that my Mac app was never even launched successfully. </p>
  
  <p>There's more: some source code I found online (perhaps dated? <a href="http://src.chromium.org/svn/trunk/src/chrome/browser/extensions/api/messaging/native_message_process_host.cc" rel="noreferrer">source file</a>) reveals exactly where this "Native host has exited" msg originates, and an examination suggests why Google Chrome's code is closing things down. In HandleReadResult() a Close() is called supplying the very msg and then closes things in response to a Posix read() returning 0. But what in the wide, wide world of sports am I doing wrong (or not doing at all) in my Mac app to cause this the read to fail? My Mac app is not writing anything to stdout, so how is not-writing-anything causing the read error? Or is all of this misleading me, and execvp's failure to launch my host app the deeper cause?  If so, why?  It's a plain-jane, barebones Mac app.</p>