osv_execve() promises to return the new thread's id in the given pointer,
but it actually lets the new thread set the id itself - so osv_execve()
may return before the new thread got to run and set this id.

The fix in this patch is for osv_execve() to wait until this variable is
set before returning.

Signed-off-by: Nadav Har'El <n...@scylladb.com>
---
 core/osv_execve.cc | 11 ++++++++---
 1 file changed, 8 insertions(+), 3 deletions(-)

diff --git a/core/osv_execve.cc b/core/osv_execve.cc
index 5bd1d54..a7594b3 100644
--- a/core/osv_execve.cc
+++ b/core/osv_execve.cc
@@ -18,7 +18,8 @@ static int thread_run_app_in_namespace(std::string filename,
                                     const std::vector<std::string> args,
                                     const std::unordered_map<std::string, 
std::string> envp,
                                     long* thread_id,
-                                    int notification_fd)
+                                    int notification_fd,
+                                    sched::thread* parent)
 {
     int ret;
     const bool new_program = true; // run in new ELF namespace
@@ -26,7 +27,7 @@ static int thread_run_app_in_namespace(std::string filename,
 
     debugf_execve("thread_run_app_in_namespace... tid=%ld\n", tid);
     if (thread_id) {
-        *thread_id = tid;
+        parent->wake_with([&] { *thread_id = tid; });
     }
 
     // An additional new thread is created by osv::run and caller is blocked
@@ -114,9 +115,13 @@ int osv_execve(const char *path, char *const argv[], char 
*const envp[],
 
     std::thread th = std::thread(thread_run_app_in_namespace,
         std::move(filename), std::move(args), std::move(envp_map),
-        thread_id, notification_fd);
+        thread_id, notification_fd, sched::thread::current());
     // detach from thread so that no join needes to be called.
     th.detach();
+    // If need to set thread_id, wait until the newly created thread sets it
+    if (thread_id) {
+        sched::thread::wait_until([&] { return *thread_id != 0; });
+    }
 
     return 0;
 }
-- 
2.7.4

-- 
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 osv-dev+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to