| 271 | } |
| 272 | |
| 273 | Status SingleMachine::CloseSession(bool use_timeout) { |
| 274 | if (!session_ || !thread_pool_) { |
| 275 | return Status::OK(); |
| 276 | } |
| 277 | |
| 278 | { |
| 279 | mutex_lock l(close_mu_); |
| 280 | |
| 281 | if (!closing_) { |
| 282 | closing_ = true; |
| 283 | } |
| 284 | } |
| 285 | |
| 286 | const bool executed_in_time = ExecuteWithTimeout( |
| 287 | [&]() { |
| 288 | if (this->coordinator_) { |
| 289 | this->coordinator_->RequestStop().IgnoreError(); |
| 290 | // Wait for all the runners to have closed their queues. |
| 291 | while (!this->coordinator_->AllRunnersStopped()) { |
| 292 | sleep(1); |
| 293 | } |
| 294 | // Now we can close the session. This should cancel any pending I/O |
| 295 | // operation. |
| 296 | this->session_->Close().IgnoreError(); |
| 297 | // Last but not least, we can delete the coordinator. |
| 298 | this->coordinator_.reset(); |
| 299 | } else { |
| 300 | this->session_->Close().IgnoreError(); |
| 301 | } |
| 302 | |
| 303 | mutex_lock l2(close_mu_); |
| 304 | closing_ = false; |
| 305 | }, |
| 306 | use_timeout ? timeout_s_ * 1000 : -1, thread_pool_.get()); |
| 307 | |
| 308 | if (!executed_in_time) { |
| 309 | // Let the caller know that we can't shutdown the session, and therefore |
| 310 | // can't process any further. |
| 311 | return errors::Unavailable("Failed to close the previous session after ", |
| 312 | timeout_s_, " seconds, aborting"); |
| 313 | } |
| 314 | |
| 315 | return Status::OK(); |
| 316 | } |
| 317 | |
| 318 | Status SingleMachine::ShutdownSession() { |
| 319 | TF_RETURN_IF_ERROR(CloseSession(true /*use_timeout*/)); |
nothing calls this directly
no test coverage detected