From 4b6bdf1f8134f9263b74ed36000b861e4aa82607 Mon Sep 17 00:00:00 2001 From: My Name Date: Thu, 2 Jul 2026 12:13:13 +0000 Subject: [PATCH] improve the LaunchSingleInstance bypass in cvd start --- .../host/commands/cvd/cli/commands/start.cpp | 54 ++++++++++++------- 1 file changed, 36 insertions(+), 18 deletions(-) diff --git a/base/cvd/cuttlefish/host/commands/cvd/cli/commands/start.cpp b/base/cvd/cuttlefish/host/commands/cvd/cli/commands/start.cpp index ff1468b5f59..d8050f0a40e 100644 --- a/base/cvd/cuttlefish/host/commands/cvd/cli/commands/start.cpp +++ b/base/cvd/cuttlefish/host/commands/cvd/cli/commands/start.cpp @@ -326,27 +326,47 @@ Result> GetCvdInternalStartFlags( return flags; } -} // namespace - -CvdStartCommandHandler::CvdStartCommandHandler( - InstanceManager& instance_manager) - : instance_manager_(instance_manager) { - own_flags_.daemon = true; -} +bool CanBypassToSingleInstance(const LocalInstance& instance, + const LocalInstanceGroup& group, + const std::vector& subcmd_args) { + if (instance.State() != cvd::INSTANCE_STATE_STOPPED) { + return false; + } + if (group.StartTime() == TimeStamp{}) { + return false; + } -static bool HasUnsafeFlagsForBypass(const std::vector& args) { - std::vector args_copy = args; + std::vector args_copy = subcmd_args; bool daemon = true; - std::string report_anonymous = ""; std::vector safe_flags = { GflagsCompatFlag("daemon", daemon), - GflagsCompatFlag("report_anonymous_usage_stats", report_anonymous), }; auto res = ConsumeFlags(safe_flags, args_copy); - if (!res.ok()) { - return true; + if (!res.ok() || !daemon || !args_copy.empty()) { + return false; + } + + auto instances = group.Instances(); + if (instances.empty()) { + return false; + } + const LocalInstance& main_instance = instances[0]; + if (instance.Id() == main_instance.Id()) { + return false; } - return !args_copy.empty(); + if (main_instance.State() != cvd::INSTANCE_STATE_RUNNING) { + return false; + } + + return true; +} + +} // namespace + +CvdStartCommandHandler::CvdStartCommandHandler( + InstanceManager& instance_manager) + : instance_manager_(instance_manager) { + own_flags_.daemon = true; } Result CvdStartCommandHandler::Handle(const CommandRequest& request) { @@ -364,13 +384,11 @@ Result CvdStartCommandHandler::Handle(const CommandRequest& request) { auto [instance, group] = CF_EXPECT(selector::SelectInstance(instance_manager_, request)); - if (instance.State() == cvd::INSTANCE_STATE_STOPPED && - group.StartTime() != TimeStamp{} && - !HasUnsafeFlagsForBypass(subcmd_args)) { + if (CanBypassToSingleInstance(instance, group, subcmd_args)) { CF_EXPECT(LaunchSingleInstance(instance, group, request)); return {}; } else { - VLOG(1) << "Instance is not in stopped state. Proceeding with " + VLOG(1) << "Cannot bypass to single instance start. Proceeding with " "normal group start."; } }