33using namespace std ;
44
55static bool RunServer = true ;
6+ static std::thread _FDPassingThread;
7+
68Configuration ConfigRef = Configuration();
79
810std::vector<pid_t > Server::ChildPIDs;
@@ -41,7 +43,7 @@ void Server::init()
4143 // - set client handler namespaces
4244 setClientHandlerConfig ();
4345
44- // - TODO: just set if exists in config, else default
46+ // - TODO: only set if exists in config, else default
4547 SocketListenAddress = ConfigRef.ServerAddress ;
4648 SocketListenPort = ConfigRef.ServerPort ;
4749
@@ -67,7 +69,7 @@ void Server::init()
6769
6870 // - fork result processor process
6971 ResultProcessor::setVHostOffsets (ASRequestHandlerRef.getOffsetsPrecalc ());
70- pid_t resultProcessorPID = ResultProcessor::forkProcessResultProcessor ( { _SHMStaticFS, _SHMPythonASMeta, _SHMPythonASRequests, _SHMPythonASResults } );
72+ const pid_t resultProcessorPID = ResultProcessor::forkProcessResultProcessor ( { _SHMStaticFS, _SHMPythonASMeta, _SHMPythonASRequests, _SHMPythonASResults } );
7173 if (resultProcessorPID > 0 ) {
7274 addChildPID (resultProcessorPID);
7375 }
@@ -77,7 +79,7 @@ void Server::init()
7779 forkProcessASHandler ( { _SHMPythonASMeta, _SHMPythonASRequests, _SHMPythonASResults } );
7880
7981 // - check interpreter count
80- uint ASInterpreterCount = getASInterpreterCount ();
82+ const uint ASInterpreterCount = getASInterpreterCount ();
8183 DBG (50 , " Sum AS Interpreters:" << ASInterpreterCount);
8284
8385 // - apply cpu bound processing
@@ -116,8 +118,23 @@ void Server::terminateChildren()
116118void Server::terminate (int _ignored)
117119{
118120 DBG (-1 , " SIGTERM Main Server received, shutting down" );
119- terminateChildren ();
120121 RunServer = false ;
122+ terminateChildren ();
123+
124+ if (_FDPassingThread.joinable ()) {
125+ _FDPassingThread.join ();
126+ }
127+
128+ std::this_thread::sleep_for (chrono::milliseconds (100 ));
129+
130+ if (_FDPassingThread.joinable ()) {
131+ _FDPassingThread.join ();
132+ }
133+
134+ std::this_thread::sleep_for (chrono::milliseconds (100 ));
135+ if (_FDPassingThread.joinable ()) {
136+ _FDPassingThread.join ();
137+ }
121138}
122139
123140void Server::setupSocket ()
@@ -266,7 +283,7 @@ void Server::setupFDPassingServer()
266283
267284 DBG (120 , " FD Passing Server socket created at:" << socket_path);
268285
269- // Start thread to handle FD passing requests
286+ // start thread to handle FD passing requests
270287 _FDPassingThread = std::thread (&Server::handleFDPassingRequests, this );
271288}
272289
@@ -278,35 +295,36 @@ void Server::handleFDPassingRequests()
278295 int flags = fcntl (_FDPassingServerFD, F_GETFL, 0 );
279296 fcntl (_FDPassingServerFD, F_SETFL, flags | O_NONBLOCK);
280297
281- std::vector<int > client_fds ;
298+ std::vector<int > ClientFDs ;
282299
283300 while (RunServer) {
284301
285302 struct sockaddr_un client_addr;
286303 socklen_t client_len = sizeof (client_addr);
304+ int NewClientFD = accept (_FDPassingServerFD, (struct sockaddr *)&client_addr, &client_len);
305+
306+ if (NewClientFD >= 0 ) {
307+ DBG (120 , " FD passing new client connected, fd:" << NewClientFD);
287308
288- int new_client_fd = accept (_FDPassingServerFD, (struct sockaddr *)&client_addr, &client_len);
309+ // set client_fd non-blocking
310+ int flags = fcntl (NewClientFD, F_GETFL, 0 );
311+ fcntl (NewClientFD, F_SETFL, flags | O_NONBLOCK);
289312
290- if (new_client_fd >= 0 ) {
291- DBG (120 , " FD passing new client connected, fd:" << new_client_fd);
292- client_fds.push_back (new_client_fd);
313+ ClientFDs.push_back (NewClientFD);
293314 }
294315
295- auto it = client_fds .begin ();
296- while (it != client_fds .end ()) {
316+ auto it = ClientFDs .begin ();
317+ while (it != ClientFDs .end ()) {
297318 int client_fd = *it;
298319 uint16_t requested_fd;
299-
300320 ssize_t n = read (client_fd, &requested_fd, sizeof (requested_fd));
301-
302321 if (n == sizeof (requested_fd)) {
303322 DBG (120 , " FD passing request for FD:" << requested_fd);
304-
305323 // send the requested FD to the client
306324 if (Syscall::sendFD (client_fd, requested_fd) < 0 ) {
307325 ERR (" Failed to send FD:" << requested_fd);
308326 close (client_fd);
309- it = client_fds .erase (it);
327+ it = ClientFDs .erase (it);
310328 } else {
311329 DBG (120 , " Successfully sent FD:" << requested_fd);
312330 ++it;
@@ -315,25 +333,38 @@ void Server::handleFDPassingRequests()
315333 // connection closed
316334 DBG (120 , " FD passing client disconnected, fd:" << client_fd);
317335 close (client_fd);
318- it = client_fds .erase (it);
336+ it = ClientFDs .erase (it);
319337 } else if (n < 0 ) {
320338 if (errno != EAGAIN && errno != EWOULDBLOCK) {
321339 ERR (" Failed to read requested FD number:" << strerror (errno));
322340 close (client_fd);
323- it = client_fds .erase (it);
341+ it = ClientFDs .erase (it);
324342 } else {
325343 ++it;
326344 }
327345 }
328346 }
329-
330347 std::this_thread::sleep_for (chrono::microseconds (IDLE_SLEEP_MICROSECONDS));
331348 }
332349
350+ DBG (120 , " FD Passing handler thread exiting" );
351+
352+ std::this_thread::sleep_for (chrono::milliseconds (50 ));
353+
354+ DBG (120 , " FD Passing handler thread closing client connections" );
355+
333356 // clean up
334- for (int fd : client_fds) {
357+ // close(client_fd);
358+ for (const int & fd : ClientFDs) {
335359 close (fd);
336360 }
337361
338- DBG (120 , " FD Passing handler thread exiting" );
362+ std::this_thread::sleep_for (chrono::milliseconds (50 ));
363+
364+ DBG (120 , " FD Passing handler thread closing serve unix domain socket" );
365+
366+ close (_FDPassingServerFD);
367+ unlink (" /tmp/falcon-fd-passing.sock" );
368+
369+ DBG (120 , " FD Passing handler thread finally exiting" );
339370}
0 commit comments