{"record":{"id":"b9f24d86ab9e0358","repo":"java-native-access/jna","slug":"unable-to-pause-the-service","errorCode":null,"errorMessage":"Unable to pause the service","messagePattern":"Unable to pause the service","errorType":"exception","errorClass":"RuntimeException","httpStatus":null,"severity":"error","filePath":"contrib/platform/src/com/sun/jna/platform/win32/W32Service.java","lineNumber":294,"sourceCode":"        }\n    }\n\n    /**\n     * Pause service.\n     */\n    public void pauseService() {\n        waitForNonPendingState();\n        // If the service is already paused - return\n        if (queryStatus().dwCurrentState == Winsvc.SERVICE_PAUSED) {\n            return;\n        }\n        if (!Advapi32.INSTANCE.ControlService(_handle, Winsvc.SERVICE_CONTROL_PAUSE,\n                new Winsvc.SERVICE_STATUS())) {\n            throw new Win32Exception(Kernel32.INSTANCE.GetLastError());\n        }\n        waitForNonPendingState();\n        if (queryStatus().dwCurrentState != Winsvc.SERVICE_PAUSED) {\n            throw new RuntimeException(\"Unable to pause the service\");\n        }\n    }\n\n    /**\n     * do not wait longer than the wait hint. A good interval is one-tenth the\n     * wait hint, but no less than 1 second and no more than 10 seconds.\n     */\n    int sanitizeWaitTime(int dwWaitHint) {\n        int dwWaitTime = dwWaitHint / 10;\n\n        if (dwWaitTime < 1000) {\n            dwWaitTime = 1000;\n        } else if (dwWaitTime > 10000) {\n            dwWaitTime = 10000;\n        }\n        return dwWaitTime;\n    }\n","sourceCodeStart":276,"sourceCodeEnd":312,"githubUrl":"https://github.com/java-native-access/jna/blob/d036ad9781adad4b66693e8fa7098e4ac665e0a3/contrib/platform/src/com/sun/jna/platform/win32/W32Service.java#L276-L312","documentation":"pauseService() sends SERVICE_CONTROL_PAUSE, waits for the service to leave a pending state, and re-queries the status. If the state is not SERVICE_PAUSED, the library throws this RuntimeException because the pause did not take effect.","triggerScenarios":"Calling pauseService() on a service that accepts the PAUSE control but does not end in the PAUSED state — e.g. the service resumes running instead, or stops during the pause attempt.","commonSituations":"Services with broken or unimplemented pause handlers; control races with a concurrent start/stop; services that only nominally advertise pause support; timeouts in the service's pause path that drop it back to RUNNING.","solutions":["Verify the service genuinely supports pause (SERVICE_ACCEPT_PAUSE_CONTINUE) and implements it correctly.","Check the Windows Event Log for service-side failures during pause.","Catch the exception and re-query status to confirm actual state before retrying or taking corrective action (e.g. stop/start).","Serialize control operations so no other thread sends controls mid-pause."],"exampleFix":"// before\nservice.pauseService();\n// after\ntry {\n    service.pauseService();\n} catch (RuntimeException e) {\n    Winsvc.SERVICE_STATUS st = service.queryStatus();\n    // decide based on actual state (retry or restart)\n}","handlingStrategy":"try-catch","validationCode":"Winsvc.SERVICE_STATUS st = service.queryStatus();\nif ((st.dwControlsAccepted & Winsvc.SERVICE_ACCEPT_PAUSE_CONTINUE) == 0)\n    throw new IllegalStateException(\"service does not support pause\");","typeGuard":null,"tryCatchPattern":"try {\n    service.pauseService();\n} catch (RuntimeException e) {\n    Winsvc.SERVICE_STATUS st = service.queryStatus();\n    if (st.dwCurrentState != Winsvc.SERVICE_PAUSED) { /* retry or restart */ }\n}","preventionTips":["Only pause services known to implement pause correctly.","Avoid concurrent control requests from multiple threads.","Re-query status after any failed pause instead of blind retries."],"tags":["windows","service","state"],"backgroundTag":"invalid-state-transition","analyzedSha":"d036ad9781adad4b66693e8fa7098e4ac665e0a3","analyzedAt":"2026-09-12T06:50:59.239Z","contentChangedAt":"2026-09-12T06:50:59.239Z","schemaVersion":2},"datasetVersion":"2026-09-19T12:17:13.211Z"}