tapio / live-server

A simple development http server with live reload capability.
http://tapiov.net/live-server/
4.4k stars 484 forks source link

Current use of `opn` prevents graceful shutdown. #196

Open typhonrt opened 7 years ago

typhonrt commented 7 years ago

Greets... live-server is great; good work...

I'm embedding live-server into a Node app and there is an issue with opn preventing graceful shutdown. After dealing with unref / destroying sockets to attempt graceful shutdown as noted in #169 there is still an issue with opn. Basically to get live-server to shutdown gracefully wait: false needs to be added to opn invocation. Setting wait to false will resolve the promise in opn immediately including a call to cp.unref(). This separates the browser child process from the calling Node app.

Since I am already wrapping live-server to deal with "unreffing" sockets I simply handle launching the browser / usage of opn directly in my wrapper as necessary always passing open: false to live-server.

It seems reasonable to add optional user input into how opn is invoked. This includes extra options and perhaps also could address #154.

In the short term I don't see any problems with adding wait: false to opn invocation though I haven't run the live-server tests with this change.

Things would change from current to:

if (openPath !== null)
   if (typeof openPath === "object") {
      openPath.forEach(function(p) {
         open(openURL + p, {app: browser, wait: false});
      });
   } else {
      open(openURL + openPath, {app: browser, wait: false});
   }
typhonrt commented 7 years ago

Just a FYI... If anyone is interested in the wrapper I have created it's on NPM: typhonjs-live-server. It contains the workaround necessary to use live-server and terminate it gracefully when embedded in a Node app in addition to providing a few other utility functions like being able to open the browser at any time. Not that this is important to most, but it also is typhonjs-plugin-manager enabled providing event bindings to control live server. I'll post more documentation soon, but just check out the source code for now: LiveServer.js.