{"record":{"id":"fcaecc09b679d3a1","repo":"hashicorp/nomad","slug":"unable-to-get-existing-service-w","errorCode":null,"errorMessage":"unable to get existing service - %w","messagePattern":"unable to get existing service - %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"command/windows_service_install.go","lineNumber":203,"sourceCode":"\treturn nil\n}\n\nfunc (c *WindowsServiceInstallCommand) serviceInstall(m winsvc.WindowsServiceManager, opts *windowsInstallOpts) error {\n\tvar err error\n\tvar srvc winsvc.WindowsService\n\tcmd := fmt.Sprintf(\"%s agent -config %s\", opts.binaryPath, opts.configDir)\n\n\texists, err := m.IsServiceRegistered(winsvc.WINDOWS_SERVICE_NAME)\n\tif err != nil {\n\t\treturn fmt.Errorf(\"service registration check failed - %w\", err)\n\t}\n\n\t// If the service already exists, open it and update. Otherwise\n\t// create a new service.\n\tif exists {\n\t\tsrvc, err = m.GetService(winsvc.WINDOWS_SERVICE_NAME)\n\t\tif err != nil {\n\t\t\treturn fmt.Errorf(\"unable to get existing service - %w\", err)\n\t\t}\n\t\tdefer srvc.Close()\n\t\tif err := srvc.Configure(winsvc.WindowsServiceConfiguration{\n\t\t\tStartType:      winsvc.StartAutomatic,\n\t\t\tDisplayName:    winsvc.WINDOWS_SERVICE_DISPLAY_NAME,\n\t\t\tDescription:    winsvc.WINDOWS_SERVICE_DESCRIPTION,\n\t\t\tBinaryPathName: cmd,\n\t\t}); err != nil {\n\t\t\treturn fmt.Errorf(\"unable to configure service - %w\", err)\n\t\t}\n\t} else {\n\t\tsrvc, err = m.CreateService(winsvc.WINDOWS_SERVICE_NAME, opts.binaryPath,\n\t\t\twinsvc.WindowsServiceConfiguration{\n\t\t\t\tStartType:      winsvc.StartAutomatic,\n\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},","sourceCodeStart":185,"sourceCodeEnd":221,"githubUrl":"https://github.com/hashicorp/nomad/blob/482b49bf1aec006f089bcfc7e632d8f6ac303e5e/command/windows_service_install.go#L185-L221","documentation":"Returned by serviceInstall when GetService fails to open the existing Nomad Windows service for update. Registration was detected, but the service handle needed to reconfigure it could not be fetched.","triggerScenarios":"Service is registered but OpenService fails: insufficient rights, service registered under a name resolved differently, or the service handle cannot be opened due to SCM state.","commonSituations":"Race where the service was deleted between the existence check and open; running without elevation; corrupted service entry left by a failed prior install/uninstall.","solutions":["Re-run from an elevated shell","Run `sc.exe delete nomad`, reboot, then re-run install to take the CreateService path","Check the chained %w cause for the concrete SCM error code","Avoid concurrent installs/uninstalls of the Nomad service"],"exampleFix":"// before\nnomad windows install\n// after\nsc.exe delete nomad && nomad windows install","handlingStrategy":"retry","validationCode":"out, _ := exec.Command(\"sc.exe\", \"query\", \"nomad\").CombinedOutput()\nif strings.Contains(string(out), \"STOP_PENDING\") || strings.Contains(string(out), \"DELETING\") {\n\tlog.Println(\"service in transitional state; wait or reboot before installing\")\n}","typeGuard":null,"tryCatchPattern":"srvc, err = m.GetService(winsvc.WINDOWS_SERVICE_NAME)\nif err != nil {\n\tif errors.Is(err, windows.ERROR_SERVICE_MARKED_FOR_DELETE) {\n\t\t// wait for deletion to settle, then retry once\n\t}\n\treturn fmt.Errorf(\"unable to get existing service - %w\", err)\n}","preventionTips":["Don't uninstall and install the service concurrently","Reboot after `sc delete` if the service was running","Run elevated so the service handle opens with sufficient rights"],"tags":["windows","service-control-manager","race-condition"],"backgroundTag":"windows-service-open-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"}