{"record":{"id":"2ebf4a0a35b5139e","repo":"hashicorp/nomad","slug":"unable-to-stop-service-w","errorCode":null,"errorMessage":"unable to stop service - %w","messagePattern":"unable to stop service - %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"command/windows_service_uninstall.go","lineNumber":107,"sourceCode":"\t// Check that the nomad service is currently registered\n\texists, err := m.IsServiceRegistered(winsvc.WINDOWS_SERVICE_NAME)\n\tif err != nil {\n\t\treturn fmt.Errorf(\"unable to check for existing service - %w\", err)\n\t}\n\n\tif !exists {\n\t\treturn nil\n\t}\n\n\t// Grab the service and ensure the service is stopped\n\tsrvc, err := m.GetService(winsvc.WINDOWS_SERVICE_NAME)\n\tif err != nil {\n\t\treturn fmt.Errorf(\"could not get existing service - %w\", err)\n\t}\n\tdefer srvc.Close()\n\n\tif err := srvc.Stop(); err != nil {\n\t\treturn fmt.Errorf(\"unable to stop service - %w\", err)\n\t}\n\n\t// Remove the service from the event log\n\tif err := srvc.DisableEventlog(); err != nil {\n\t\treturn fmt.Errorf(\"could not remove eventlog configuration - %w\", err)\n\t}\n\n\t// Finally, delete the service\n\tif err := srvc.Delete(); err != nil {\n\t\treturn fmt.Errorf(\"could not delete service - %w\", err)\n\t}\n\n\treturn nil\n}\n","sourceCodeStart":89,"sourceCodeEnd":122,"githubUrl":"https://github.com/hashicorp/nomad/blob/482b49bf1aec006f089bcfc7e632d8f6ac303e5e/command/windows_service_uninstall.go#L89-L122","documentation":"performUninstall wraps the error from srvc.Stop(), which stops the running Nomad service before deleting it. The service could not be transitioned to the stopped state, so uninstall aborts to avoid deleting a running service. The wrapped error carries the SCM failure reason.","triggerScenarios":"srvc.Stop() fails because the service is already stopping but hung, Stop depends on other running services that fail, a timeout elapses waiting for the stop, or access is denied while performUninstall runs.","commonSituations":"Nomad service hung and unresponsive to stop requests; dependent services blocking the stop; non-elevated uninstall lacking SERVICE_STOP rights.","solutions":["Check the wrapped error; wait for in-progress shutdown to finish and retry","Kill the nomad process if it is hung (taskkill) and re-run the uninstall","Run elevated so SERVICE_STOP access is granted","Stop dependent services first, or reboot if the service cannot be stopped"],"exampleFix":"// before\nsc stop nomad   # hangs, service stuck in STOP_PENDING\n// after\ntaskkill /F /IM nomad.exe\nnomad windows service uninstall","handlingStrategy":"retry","validationCode":null,"typeGuard":null,"tryCatchPattern":"for i := 0; i < 3; i++ {\n    if err := uninstallCmd.Run(); err == nil { break }\n    time.Sleep(5 * time.Second) // allow STOP_PENDING to resolve\n}","preventionTips":["Gracefully stop Nomad workloads before uninstalling the service","Watch service state with `sc query nomad` for STOP_PENDING hangs","Run elevated to guarantee SERVICE_STOP rights"],"tags":["go","windows","service-control-manager","service-stop"],"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"}