diff --git a/nbd/src/NBDTool.cpp b/nbd/src/NBDTool.cpp index 14ffc7fb8e..8d69bd4a27 100644 --- a/nbd/src/NBDTool.cpp +++ b/nbd/src/NBDTool.cpp @@ -111,8 +111,26 @@ int NBDTool::Connect(NBDConfig *cfg) { } int NBDTool::Disconnect(const std::string& devpath) { + pid_t devpid = -1; + std::vector devices; + + int ret = List(&devices); + for (const auto& device : devices) { + if (device.config.devpath == devpath) { + devpid = device.pid; + break; + } + } + NBDControllerPtr nbdCtrl = GetController(false); - return nbdCtrl->DisconnectByPath(devpath); + ret = nbdCtrl->DisconnectByPath(devpath); + if (ret != 0) { + return ret; + } + + ret = WaitForTerminate(devpath, devpid, 5000); + + return 0; } int NBDTool::List(std::vector* infos) { @@ -137,6 +155,8 @@ void NBDTool::RunServerUntilQuit() { } else { ctrl->RunUntilQuit(); } + + nbdWatchCtx_->StopWatch(); } ImagePtr g_test_image = nullptr; @@ -150,5 +170,31 @@ ImagePtr NBDTool::GenerateImage(const std::string& imageName) { return result; } +int NBDTool::WaitForTerminate(const std::string& devpath, pid_t pid, + uint64_t totalRetryMs) { + if (pid < 0) { + return 0; + } + + // TODO(wuhanqing): make this to NBDConfig + int times = 20; + uint64_t sleepMs = totalRetryMs / times; + + while (times-- > 0) { + if (kill(pid, 0) == -1) { + if (errno == ESRCH) { + return 0; + } + std::cerr << "curve-nbd test device failed, dev: " << devpath + << ", err = " << cpp_strerror(-errno) << std::endl; + return -errno; + } + + std::this_thread::sleep_for(std::chrono::milliseconds(sleepMs)); + } + + return -ETIMEDOUT; +} + } // namespace nbd } // namespace curve diff --git a/nbd/src/NBDTool.h b/nbd/src/NBDTool.h index 9769b8d4f4..a6f1390ef4 100644 --- a/nbd/src/NBDTool.h +++ b/nbd/src/NBDTool.h @@ -68,6 +68,10 @@ class NBDTool { // 生成image instance ImagePtr GenerateImage(const std::string& imageName); + // wait curve-nbd process to exit + int WaitForTerminate(const std::string& devpath, pid_t pid, + uint64_t totalRetryMs); + private: class NBDSocketPair { public: diff --git a/robot/Resources/keywords/test_curve_stability_nbd.py b/robot/Resources/keywords/test_curve_stability_nbd.py index bcb86ed6a6..e6ab1f52c5 100644 --- a/robot/Resources/keywords/test_curve_stability_nbd.py +++ b/robot/Resources/keywords/test_curve_stability_nbd.py @@ -705,6 +705,7 @@ def test_snapshot_all(vol_uuid): test_clone_iovol_consistency(lazy) # test_clone_vol_same_uuid(lazy) test_recover_snapshot(lazy) + config.snapshot_thrash.nbd_unmap() config.snapshot_thrash.nbd_delete() return "finally"