title: Chrome message passing error : Attempting to use a disconnected port object
body: <p>My chrome extension uses long-lived 'Port' object for message passing between 'content script' and 'popup' page. The 'popup' is able to send a message to the 'content script' event listener. But, the 'Port' object in the 'content script' is unable to send message to the 'popup' page.</p>
  
  <pre><code>var port = chrome.extension.connect({"name":"swap"});
  
  // listener for incoming connections
  chrome.extension.onConnect.addListener(function( incomingPort ){
  
      // listener on incoming messages
      incomingPort.onMessage.addListener(function( msg ){
  
          if( msg.command === 'get_scripts' ){
              //do work
  
          }
  
          var scrs = { 'scripts' : 'name' };
          var result = port.postMessage( scrs );
  
      });
  });
  </code></pre>
  
  <p>When executing 'port.postMessage(Object obj)' , the plugin throws the following Error,</p>
  
  <pre><code>Error in event handler for 'undefined': Attempting to use a disconnected port object   Error: Attempting to use a disconnected port object
  at PortImpl.postMessage (miscellaneous_bindings:54:5)
  at chrome-extension://loiamkgdhfjdlkcpehnebipeinpcicfj/swap.js:27:31
  at [object Object].dispatch (event_bindings:203:41)
  at Object.&lt;anonymous&gt; (miscellaneous_bindings:250:22) event_bindings:207
  </code></pre>
  
  <p>I have tried using 'Port' object and 'incomingPort' object, both throw the same 'Error'.
  It feels like it has to do with the scope of the pre-created 'Port' object.</p>
  
  <p>The plugin code is available at this git repository <a href="https://github.com/snambi/chrome_plugin/tree/master/src/chrome" rel="nofollow">https://github.com/snambi/chrome_plugin/tree/master/src/chrome</a></p>
  
  <p>What is wrong in this plugin?</p>