Commit e270cf9c (more than two years ago) was well-intentioned, but
introduced a new bug: we no longer wait for the background programs
to finish before stopping httpserver. So for example if the command
line is something like

       /cli/cli.so & finish_quickly.so

And finish_quickly.so finishes quickly, we do not wait for cli.so to
finish before killing httpserver.

One user discovered that because of that, the image created by
"./scripts/build image=java-example,cli" runs the short-lived java-example
but then the CLI doesn't work: It can't run any commands because the
httpserver it relies on is already dead.

So this patch partially reverts e270cf9c... We keep the "app registry"
introduced there, but we still have a separate list of background apps
started in the command line - and wait for them to finish before asking
httpserver to exit.

Signed-off-by: Nadav Har'El <[email protected]>
---
 loader.cc | 7 +++++++
 1 file changed, 7 insertions(+)

diff --git a/loader.cc b/loader.cc
index 43aba5f..82306a4 100644
--- a/loader.cc
+++ b/loader.cc
@@ -495,6 +495,7 @@ void* do_main_thread(void *_main_args)
     // empty otherwise, to run in this thread. '&!' is the same as '&', but
     // doesn't wait for the thread to finish before exiting OSv.
     std::vector<shared_app_t> detached;
+    std::vector<shared_app_t> bg;
     for (auto &it : commands) {
         std::vector<std::string> newvec(it.begin(), std::prev(it.end()));
         auto suffix = it.back();
@@ -505,6 +506,8 @@ void* do_main_thread(void *_main_args)
                 detached.push_back(app);
             } else if (!background) {
                 app->join();
+            } else {
+                bg.push_back(app);
             }
         } catch (const launch_error& e) {
             std::cerr << e.what() << ". Powering off.\n";
@@ -512,6 +515,10 @@ void* do_main_thread(void *_main_args)
         }
     }
 
+    for (auto app : bg) {
+        app->join();
+    }
+
     for (auto app : detached) {
         app->request_termination();
         debug("Requested termination of %s, waiting...\n", app->get_command());
-- 
2.9.3

-- 
You received this message because you are subscribed to the Google Groups "OSv 
Development" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to