diff --git a/LFtid1056/client2.cpp b/LFtid1056/client2.cpp index a507cd6..412be7b 100644 --- a/LFtid1056/client2.cpp +++ b/LFtid1056/client2.cpp @@ -73,7 +73,7 @@ void ClientContext::stop_timers() { void ClientContext::close_handles() { if (!uv_is_closing((uv_handle_t*)&client)) { - uv_close((uv_handle_t*)&client, nullptr); + uv_close((uv_handle_t*)&client, on_close); } if (!uv_is_closing((uv_handle_t*)&timer)) { uv_close((uv_handle_t*)&timer, nullptr); @@ -775,8 +775,19 @@ void ClientManager::remove_device(const std::string& device_id) { return; } - // 关闭连接并移除 + auto& ctx = it->second; it->second->shutdown = true; + + // 确保所有定时器都停止 + ctx->stop_timers(); + { + std::lock_guard state_lock(ctx->state_mutex_); + std::queue empty; + std::swap(ctx->action_queue_, empty); + ctx->current_state_ = DeviceState::IDLE; + } + + // 关闭连接并移除 it->second->close_handles(); clients_.erase(it); std::cout << "[Device " << device_id << "] Removed successfully\n";