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 !
- Attempt to improve error handling.
- Fix file descriptor leaks after detaching an ApplicationPoolServer that 
already has clients.
- Add test:valgrind task.
Hongli Lai (Phusion) (author)
Thu Feb 21 13:57:27 -0800 2008
commit  634b9bd3014debdba987176cf4725e9fa54e472b
tree    f2a6ee0051a74981d0818a69f4dbbfca1e84c54b
parent  ce3a193bb4865ccfb7489ec7e346161eb05a4fff
...
182
183
184
 
 
 
 
 
185
186
187
...
189
190
191
192
193
 
 
 
194
195
196
...
182
183
184
185
186
187
188
189
190
191
192
...
194
195
196
 
 
197
198
199
200
201
202
0
@@ -182,6 +182,11 @@ subdir 'test' do
0
     sh "./Apache2ModuleTests"
0
   end
0
   
0
+ desc "Run unit tests for the Apache 2 module in Valgrind"
0
+ task 'test:valgrind' => 'Apache2ModuleTests' do
0
+ sh "valgrind #{ENV['ARGS']} ./Apache2ModuleTests"
0
+ end
0
+
0
   desc "Run unit tests for the Ruby libraries"
0
   task 'test:ruby' => ['../ext/mod_rails/native_support.so'] do
0
     sh "spec -f s *_spec.rb"
0
@@ -189,8 +194,9 @@ subdir 'test' do
0
 
0
   file 'Apache2ModuleTests' => TEST::AP2_OBJECTS.keys +
0
    ['../ext/boost/src/libboost_thread.a',
0
- '../ext/mod_rails/native_support.so'] do
0
- objects = TEST::AP2_OBJECTS.keys.join(' ')
0
+ '../ext/mod_rails/native_support.so',
0
+ '../ext/apache2/Utils.o'] do
0
+ objects = TEST::AP2_OBJECTS.keys.join(' ') << " ../ext/apache2/Utils.o"
0
     create_executable "Apache2ModuleTests", objects,
0
       "#{LDFLAGS} #{APR_LIBS} ../ext/boost/src/libboost_thread.a -lpthread"
0
   end
...
278
279
280
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
281
282
283
...
339
340
341
 
342
343
344
...
348
349
350
351
 
352
353
354
355
356
 
 
 
 
357
358
359
360
 
361
362
363
364
365
366
367
 
 
 
368
369
370
...
372
373
374
375
 
376
377
 
378
379
380
...
397
398
399
400
401
402
403
404
 
 
 
405
406
407
408
409
410
 
 
 
411
412
413
...
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
...
354
355
356
357
358
359
360
...
364
365
366
 
367
368
 
 
 
 
369
370
371
372
373
374
375
 
376
377
378
379
380
 
 
 
381
382
383
384
385
386
...
388
389
390
 
391
392
 
393
394
395
396
...
413
414
415
 
 
 
 
 
416
417
418
419
 
 
 
 
 
420
421
422
423
424
425
0
@@ -278,6 +278,21 @@ private:
0
     detached = true;
0
   }
0
   
0
+ void handleConnectException(const exception &e, const ApplicationPtr &app,
0
+ ApplicationList &appList) {
0
+ string message("Cannot connect to an existing application instance for '");
0
+ message.append(app->getAppRoot());
0
+ message.append("': ");
0
+ message.append(e.what());
0
+ appList.remove(app);
0
+ if (appList.empty()) {
0
+ apps.erase(app->getAppRoot());
0
+ }
0
+ count--;
0
+ active--;
0
+ throw IOException(message);
0
+ }
0
+
0
 public:
0
   /**
0
    * Create a new StandardApplicationPool object.
0
@@ -339,6 +354,7 @@ public:
0
      * of the algorithm.
0
      */
0
     ApplicationPtr app;
0
+ ApplicationList *appList;
0
     mutex::scoped_lock l(lock);
0
     
0
     if (needsRestart(appRoot)) {
0
@@ -348,23 +364,23 @@ public:
0
     try {
0
       ApplicationMap::iterator it(apps.find(appRoot));
0
       if (it != apps.end()) {
0
- ApplicationList &appList(*it->second);
0
+ appList = it->second.get();
0
     
0
- if (appList.front()->getSessions() == 0) {
0
- app = appList.front();
0
- appList.pop_front();
0
- appList.push_back(app);
0
+ if (appList->front()->getSessions() == 0) {
0
+ app = appList->front();
0
+ appList->pop_front();
0
+ appList->push_back(app);
0
           active++;
0
         } else if (count < max) {
0
           app = spawnManager.spawn(appRoot, user, group);
0
- appList.push_back(app);
0
+ appList->push_back(app);
0
           count++;
0
           countOrMaxChanged.notify_all();
0
           active++;
0
         } else {
0
- app = appList.front();
0
- appList.pop_front();
0
- appList.push_back(app);
0
+ app = appList->front();
0
+ appList->pop_front();
0
+ appList->push_back(app);
0
           active++;
0
         }
0
       } else {
0
@@ -372,9 +388,9 @@ public:
0
           countOrMaxChanged.wait(l);
0
         }
0
         app = spawnManager.spawn(appRoot, user, group);
0
- ApplicationListPtr appList(new ApplicationList());
0
+ appList = new ApplicationList();
0
         appList->push_back(app);
0
- apps[appRoot] = appList;
0
+ apps[appRoot] = ptr(appList);
0
         count++;
0
         countOrMaxChanged.notify_all();
0
         active++;
0
@@ -397,17 +413,13 @@ public:
0
     try {
0
       return app->connect(SessionCloseCallback(data, app));
0
     } catch (const IOException &e) {
0
- string message("Cannot connect to an existing application instance for '");
0
- message.append(appRoot);
0
- message.append("': ");
0
- message.append(e.what());
0
- throw IOException(message);
0
+ handleConnectException(e, app, *appList);
0
+ // Never reached; shut up compiler warning
0
+ return Application::SessionPtr();
0
     } catch (const SystemException &e) {
0
- string message("Cannot connect to an existing application instance for '");
0
- message.append(appRoot);
0
- message.append("': ");
0
- message.append(e.what());
0
- throw IOException(message);
0
+ handleConnectException(e, app, *appList);
0
+ // Never reached; shut up compiler warning
0
+ return Application::SessionPtr();
0
     }
0
   }
0
   
...
241
242
243
 
 
 
 
 
 
 
 
 
244
245
246
247
 
 
 
 
 
 
 
 
248
249
250
...
473
474
475
476
477
478
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
479
 
480
481
482
...
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
...
489
490
491
 
 
 
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
0
@@ -241,10 +241,26 @@ private:
0
     int fd;
0
     /** The thread which handles the client. */
0
     thread *thr;
0
+ bool detached;
0
+
0
+ ClientInfo() {
0
+ detached = false;
0
+ }
0
+
0
+ void detach() {
0
+ detached = true;
0
+ }
0
     
0
     ~ClientInfo() {
0
       close(fd);
0
- delete thr;
0
+ // For some reason, joining or deleting (detaching)
0
+ // the thread after fork() will cause a segfault.
0
+ // I haven't figured out why that happens, so for now
0
+ // I'll just ignore the thread (which isn't running
0
+ // anyway).
0
+ if (!detached) {
0
+ delete thr;
0
+ }
0
     }
0
   };
0
   
0
@@ -473,10 +489,24 @@ public:
0
     detached = true;
0
     close(connectSocket);
0
     close(serverSocket);
0
- #ifdef VALGRIND_FRIENDLY
0
- delete serverThread;
0
- #endif
0
+ serverThread->join();
0
+ delete serverThread;
0
+
0
+ // A client thread might have a reference to a ClientInfo
0
+ // object. And because that thread doesn't run anymore after a
0
+ // fork(), the reference never gets removed and the ClientInfo
0
+ // object never gets destroyed. So we forcefully delete
0
+ // ClientInfo objects in order to close the client file
0
+ // descriptors.
0
+ set<ClientInfoPtr>::iterator it;
0
+ for (it = clients.begin(); it != clients.end(); it++) {
0
+ if (!it->unique()) {
0
+ (*it)->detach();
0
+ delete it->get();
0
+ }
0
+ }
0
     clients.clear();
0
+
0
     pool.detach();
0
   }
0
 };
...
335
336
337
338
339
340
341
...
335
336
337
 
338
339
340
0
@@ -335,7 +335,6 @@ public:
0
     /*
0
      * TODO:
0
      * - If the request handler dies, it does not get removed from the application pool. It should.
0
- * - Implement HTTP body forwarding
0
      */
0
     
0
     try {
...
1
2
 
3
4
5
...
1
 
2
3
4
5
0
@@ -1,5 +1,5 @@
0
 #include "tut.h"
0
-#include "Utils.cpp"
0
+#include "Utils.h"
0
 #include <unistd.h>
0
 #include <limits.h>
0
 

Comments

    No one has commented yet.