NOTE: Join() can happen before Stop().
| 1303 | |
| 1304 | // NOTE: Join() can happen before Stop(). |
| 1305 | int Server::Join() { |
| 1306 | if (_status != RUNNING && _status != STOPPING) { |
| 1307 | return -1; |
| 1308 | } |
| 1309 | if (_am) { |
| 1310 | _am->Join(); |
| 1311 | } |
| 1312 | if (_internal_am) { |
| 1313 | _internal_am->Join(); |
| 1314 | } |
| 1315 | |
| 1316 | if (_session_local_data_pool) { |
| 1317 | // We can't delete the pool right here because there's a bvar watching |
| 1318 | // this pool in _derivative_thread which does not quit yet. |
| 1319 | _session_local_data_pool->Reset(NULL); |
| 1320 | } |
| 1321 | |
| 1322 | if (_keytable_pool) { |
| 1323 | // Destroy _keytable_pool to delete keytables inside. This has to be |
| 1324 | // done here (before leaving Join) because it's legal for users to |
| 1325 | // delete bthread keys after Join which makes related objects |
| 1326 | // in KeyTables undeletable anymore and leaked. |
| 1327 | CHECK_EQ(0, bthread_keytable_pool_destroy(_keytable_pool)); |
| 1328 | // TODO: Can't delete _keytable_pool which may be accessed by |
| 1329 | // still-running bthreads (created by the server). The memory is |
| 1330 | // leaked but servers are unlikely to be started/stopped frequently, |
| 1331 | // the leak is acceptable in most scenarios. |
| 1332 | _keytable_pool = NULL; |
| 1333 | } |
| 1334 | |
| 1335 | // Delete tls_key as well since we don't need it anymore. |
| 1336 | if (_tl_options.tls_key != INVALID_BTHREAD_KEY) { |
| 1337 | CHECK_EQ(0, bthread_key_delete(_tl_options.tls_key)); |
| 1338 | _tl_options.tls_key = INVALID_BTHREAD_KEY; |
| 1339 | } |
| 1340 | |
| 1341 | // Have to join _derivative_thread, which may assume that server is running |
| 1342 | // and services in server are not mutated, otherwise data race happens |
| 1343 | // between Add/RemoveService after Join() and the thread. |
| 1344 | if (_derivative_thread != INVALID_BTHREAD) { |
| 1345 | bthread_stop(_derivative_thread); |
| 1346 | bthread_join(_derivative_thread, NULL); |
| 1347 | _derivative_thread = INVALID_BTHREAD; |
| 1348 | } |
| 1349 | |
| 1350 | g_running_server_count.fetch_sub(1, butil::memory_order_relaxed); |
| 1351 | _status = READY; |
| 1352 | return 0; |
| 1353 | } |
| 1354 | |
| 1355 | int Server::AddServiceInternal(google::protobuf::Service* service, |
| 1356 | bool is_builtin_service, |