We got nominated! Help us out and vote for GitHub as Best Bootstrapped Startup of 2008. (You can vote once a day.) [ hide ]

public
Description: Phusion Passenger (mod_rails)
Homepage: http://www.modrails.com/
Clone URL: git://github.com/FooBarWidget/passenger.git
Click here to lend your support to: passenger and make a donation at www.pledgie.com !
Improve documentation. Add a timeout to the ApplicationPoolServer shutdown 
routine.
Hongli Lai (Phusion) (author)
Thu Apr 24 13:40:18 -0700 2008
commit  8ffb12bb53437c7c3aacb714d2f538bd2c2df3fb
tree    dfded107aa5f92c0f975fae0de9408ac132367e8
parent  c2512e8542966228f86e1e89726e4b765a14d2fa
...
221
222
223
 
 
 
 
 
 
 
 
224
 
 
 
 
 
 
 
 
 
225
226
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
227
228
 
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
 
255
256
257
...
342
343
344
345
346
347
 
348
349
350
...
388
389
390
391
392
393
 
 
 
394
395
396
 
 
 
 
397
398
399
 
 
 
400
401
402
...
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
 
284
285
286
287
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
288
289
290
291
...
376
377
378
 
 
 
379
380
381
382
...
420
421
422
 
 
 
423
424
425
426
 
 
427
428
429
430
431
 
 
432
433
434
435
436
437
0
@@ -221,37 +221,71 @@ private:
0
   string m_rubyCommand;
0
   string m_user;
0
   
0
+ /**
0
+ * The PID of the ApplicationPool server process. If no server process
0
+ * is running, then <tt>serverPid == 0</tt>.
0
+ *
0
+ * @invariant
0
+ * if serverPid == 0:
0
+ * serverSocket == -1
0
+ */
0
   pid_t serverPid;
0
+
0
+ /**
0
+ * The connection to the ApplicationPool server process. If no server
0
+ * process is running, then <tt>serverSocket == -1</tt>.
0
+ *
0
+ * @invariant
0
+ * if serverPid == 0:
0
+ * serverSocket == -1
0
+ */
0
   int serverSocket;
0
   
0
+ /**
0
+ * Shutdown the currently running ApplicationPool server process.
0
+ *
0
+ * @pre serverSocket != -1 && serverPid != 0
0
+ * @post serverSocket == -1 && serverPid == 0
0
+ */
0
+ void shutdownServer() {
0
+ time_t begin;
0
+ bool done;
0
+ int ret;
0
+
0
+ do {
0
+ ret = close(serverSocket);
0
+ } while (ret == -1 && errno == EINTR);
0
+
0
+ P_DEBUG("Waiting for existing ApplicationPoolServerExecutable to exit...");
0
+ begin = time(NULL);
0
+ while (!done && time(NULL) < begin + 5) {
0
+ done = waitpid(serverPid, NULL, WNOHANG) > 0;
0
+ usleep(100000);
0
+ }
0
+ if (done) {
0
+ P_DEBUG("ApplicationPoolServerExecutable exited.");
0
+ } else {
0
+ P_DEBUG("ApplicationPoolServerExecutable not exited in time. Killing it...");
0
+ kill(serverPid, SIGTERM);
0
+ waitpid(serverPid, NULL, 0);
0
+ }
0
+ serverSocket = -1;
0
+ serverPid = 0;
0
+ }
0
+
0
+ /**
0
+ * Start an ApplicationPool server process. If there's already one running,
0
+ * then the currently running one will be shutdown.
0
+ *
0
+ * @post serverSocket != -1 && serverPid != 0
0
+ * @throw SystemException Something went wrong.
0
+ */
0
   void restartServer() {
0
- int fds[2], ret;
0
+ int fds[2];
0
     pid_t pid;
0
     
0
     if (serverPid != 0) {
0
- time_t begin;
0
- bool done;
0
-
0
- // Shutdown existing server instance.
0
- do {
0
- ret = close(serverSocket);
0
- } while (ret == -1 && errno == EINTR);
0
-
0
- P_DEBUG("Waiting for existing ApplicationPoolServerExecutable to exit...");
0
- begin = time(NULL);
0
- while (!done && time(NULL) < begin + 5) {
0
- done = waitpid(serverPid, NULL, WNOHANG) > 0;
0
- usleep(100000);
0
- }
0
- if (done) {
0
- P_DEBUG("ApplicationPoolServerExecutable exited.");
0
- } else {
0
- P_DEBUG("ApplicationPoolServerExecutable not exited in time. Killing it...");
0
- kill(serverPid, SIGTERM);
0
- waitpid(serverPid, NULL, 0);
0
- }
0
- serverSocket = -1;
0
- serverPid = 0;
0
+ shutdownServer();
0
     }
0
     
0
     if (socketpair(AF_UNIX, SOCK_STREAM, 0, fds) == -1) {
0
@@ -342,9 +376,7 @@ public:
0
   
0
   ~ApplicationPoolServer() {
0
     if (serverSocket != -1) {
0
- close(serverSocket);
0
- // TODO: don't wait indefinitely
0
- waitpid(serverPid, NULL, 0);
0
+ shutdownServer();
0
     }
0
   }
0
   
0
@@ -388,15 +420,18 @@ public:
0
   }
0
   
0
   /**
0
- * Detach the server by freeing up some server resources such as file descriptors.
0
- * This should be called by child processes that wish to use a server, but do
0
- * not run the server itself.
0
+ * Detach the server, thereby telling it that we don't want to connect
0
+ * to it anymore. This frees up some resources in the current process,
0
+ * such as file descriptors.
0
    *
0
- * This method may only be called once. The ApplicationPoolServer object
0
- * will become unusable once detach() has been called.
0
+ * This method is particularily useful to Apache worker processes that
0
+ * have just established a connection with the ApplicationPool server.
0
+ * Any sessions that are opened prior to calling detach(), will keep
0
+ * working even after a detach().
0
    *
0
- * @warning Never call this method in the process in which this
0
- * ApplicationPoolServer was created!
0
+ * This method may only be called once. The ApplicationPoolServer object
0
+ * will become unusable once detach() has been called, so call connect()
0
+ * before calling detach().
0
    */
0
   void detach() {
0
     close(serverSocket);

Comments

    No one has commented yet.