From 0b641ba581134aacd9f29077d6c9e89f2af1efca Mon Sep 17 00:00:00 2001
From: Tomasz Gajewski <tomga@wp.pl>
Date: Tue, 18 May 2021 00:56:07 +0200
Subject: [PATCH] ssh_terminal: fixed race condition during startup

Moved creating ssh loop thread after initialization of wake up server
file descriptors to make sure that they will be properly handled even in
first loop run.

Issue #4095
---
 repos/gems/src/server/ssh_terminal/server.cc | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)

diff --git a/repos/gems/src/server/ssh_terminal/server.cc b/repos/gems/src/server/ssh_terminal/server.cc
index 3d5882c0d9..311aedce8f 100644
--- a/repos/gems/src/server/ssh_terminal/server.cc
+++ b/repos/gems/src/server/ssh_terminal/server.cc
@@ -117,11 +117,6 @@ Ssh::Server::Server(Genode::Env &env,
 			throw Init_failed();
 		}
 
-		if (pthread_create(&_event_thread, nullptr, _server_loop, this)) {
-			Genode::error("could not create event thread");
-			throw Init_failed();
-		}
-
 		/* add pipe to wake up loop on late connecting terminal */
 		if (pipe(_server_fds) ||
 			ssh_event_add_fd(_event_loop,
@@ -133,6 +128,11 @@ Ssh::Server::Server(Genode::Env &env,
 			throw -1;
 		}
 
+		if (pthread_create(&_event_thread, nullptr, _server_loop, this)) {
+			Genode::error("could not create event thread");
+			throw Init_failed();
+		}
+
 		Genode::log("Listen on port: ", _port);
 	}); /* Libc::with_libc */
 }