{"record":{"id":"f56defcc59e016dd","repo":"hashicorp/nomad","slug":"could-not-stop-service-w","errorCode":null,"errorMessage":"could not stop service - %w","messagePattern":"could not stop service - %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"command/windows_service_install.go","lineNumber":236,"sourceCode":"\t\t\t\tDisplayName:    winsvc.WINDOWS_SERVICE_DISPLAY_NAME,\n\t\t\t\tDescription:    winsvc.WINDOWS_SERVICE_DESCRIPTION,\n\t\t\t\tBinaryPathName: cmd,\n\t\t\t},\n\t\t)\n\t\tif err != nil {\n\t\t\treturn fmt.Errorf(\"unable to create service - %w\", err)\n\t\t}\n\t\tdefer srvc.Close()\n\t}\n\n\t// Enable the service in the Windows eventlog\n\tif err := srvc.EnableEventlog(); err != nil {\n\t\treturn fmt.Errorf(\"could not configure eventlog - %w\", err)\n\t}\n\n\t// Ensure the service is stopped\n\tif err := srvc.Stop(); err != nil {\n\t\treturn fmt.Errorf(\"could not stop service - %w\", err)\n\t}\n\n\t// Start the service so the new binary is in use\n\tif err := srvc.Start(); err != nil {\n\t\treturn fmt.Errorf(\"could not start service - %w\", err)\n\t}\n\n\treturn nil\n}\n\nfunc (c *WindowsServiceInstallCommand) configInstall(opts *windowsInstallOpts) error {\n\t// If the config or data directory are unset, default them\n\tif opts.configDir == \"\" {\n\t\topts.configDir = filepath.Join(winsvc.WINDOWS_INSTALL_APPDATA_DIRECTORY, \"config\")\n\t}\n\tif opts.dataDir == \"\" {\n\t\topts.dataDir = filepath.Join(winsvc.WINDOWS_INSTALL_APPDATA_DIRECTORY, \"data\")\n\t}","sourceCodeStart":218,"sourceCodeEnd":254,"githubUrl":"https://github.com/hashicorp/nomad/blob/482b49bf1aec006f089bcfc7e632d8f6ac303e5e/command/windows_service_install.go#L218-L254","documentation":"Wraps the failure of `srvc.Stop` after the service is installed/updated. Install ensures the service is stopped so the next Start picks up the newly installed binary. If the SCM stop control fails, install aborts with this message.","triggerScenarios":"Sending SERVICE_CONTROL_STOP to the freshly installed/updated Nomad service fails: service not accepting stop controls, stop pending timeout, or access denied on the handle.","commonSituations":"Service entered a stop-pending state and SCM timed out; the service process hung during early startup; insufficient privileges on the service handle.","solutions":["Wait for the service to reach STOPPED (`sc query nomad`), then re-run install","Force-kill the nomad.exe process (`taskkill /f /im nomad.exe`) if stop-pending, then retry","Run the command from an elevated shell","Reboot the host if the service is stuck in a transitional state"],"exampleFix":"// before\nnomad windows install\n// after\ntaskkill /f /im nomad.exe; nomad windows install","handlingStrategy":"retry","validationCode":"out, _ := exec.Command(\"sc.exe\", \"query\", \"nomad\").CombinedOutput()\nif strings.Contains(string(out), \"STOP_PENDING\") {\n\tlog.Println(\"service is stopping; wait for STOPPED before installing\")\n}","typeGuard":null,"tryCatchPattern":"if err := srvc.Stop(); err != nil {\n\tif errors.Is(err, windows.ERROR_SERVICE_NOT_ACTIVE) {\n\t\treturn nil // already stopped — safe to proceed\n\t}\n\treturn fmt.Errorf(\"could not stop service - %w\", err)\n}","preventionTips":["Poll `sc query nomad` until STOPPED before reinstalling","Treat ERROR_SERVICE_NOT_ACTIVE as success","Force-kill a hung nomad.exe before reinstalling","Run elevated so stop controls are accepted"],"tags":["windows","service-control-manager","service-lifecycle"],"backgroundTag":"windows-service-stop-failed","analyzedSha":"482b49bf1aec006f089bcfc7e632d8f6ac303e5e","analyzedAt":"2026-09-04T07:54:14.808Z","contentChangedAt":"2026-09-04T07:54:14.808Z","schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}