# Buggy code
def stop(name, timeout=90):
    """
    Stop the specified service

    Args:
        name (str): The name of the service to stop

        timeout (int):
            The time in seconds to wait for the service to stop before
            returning. Default is 90 seconds

            .. versionadded:: 2017.7.9, 2018.3.4

    Returns:
        bool: ``True`` if successful, otherwise ``False``

    CLI Example:

    .. code-block:: bash

        salt '*' service.stop <service name>
    """
    try:
        win32serviceutil.StopService(name)
    except pywintypes.error as exc:
        if exc[0] != 1062:
            raise CommandExecutionError("Failed To Stop {0}: {1}".format(name, exc[2]))

    srv_status = _status_wait(
        service_name=name,
        end_time=time.time() + int(timeout),
        service_states=["Running", "Stop Pending"],
    )

    return srv_status["Status"] == "Stopped"


# Traceback (TypeError)
c:\salt\salt-call -l debug --local service.restart postgresql-x64-9.6
winion-0:
[DEBUG   ] Reading configuration from c:\salt\conf\minion
[DEBUG   ] Including configuration from 'c:\salt\conf\minion.d\_schedule.conf'
[DEBUG   ] Reading configuration from c:\salt\conf\minion.d\_schedule.conf
[DEBUG   ] Using cached minion ID from c:\salt\conf\minion_id: winion-0
[DEBUG   ] Configuration file path: c:\salt\conf\minion
[WARNING ] Insecure logging configuration detected! Sensitive data may be logged.
[DEBUG   ] Grains refresh requested. Refreshing grains.
[DEBUG   ] Reading configuration from c:\salt\conf\minion
[DEBUG   ] Including configuration from 'c:\salt\conf\minion.d\_schedule.conf'
[DEBUG   ] Reading configuration from c:\salt\conf\minion.d\_schedule.conf
[DEBUG   ] Determining pillar cache
[DEBUG   ] LazyLoaded jinja.render
[DEBUG   ] LazyLoaded yaml.render
[DEBUG   ] LazyLoaded jinja.render
[DEBUG   ] LazyLoaded yaml.render
[DEBUG   ] LazyLoaded service.restart
[DEBUG   ] LazyLoaded cmd.run
[INFO    ] Executing command ['net', 'stop', '/y', 'postgresql-x64-9.6'] in directory 'C:\Windows\system32\config\systemprofile'
[ERROR   ] Command '['net', 'stop', '/y', 'postgresql-x64-9.6']' failed with return code: 2
[ERROR   ] stdout: The postgresql-x64-9.6 service is not started.



More help is available by typing NET HELPMSG 3521.
[ERROR   ] retcode: 2
[ERROR   ] Command '['net', 'stop', '/y', 'postgresql-x64-9.6']' failed with return code: 2
[ERROR   ] output: The postgresql-x64-9.6 service is not started.



More help is available by typing NET HELPMSG 3521.

Passed invalid arguments: 'error' object does not support indexing.

Usage:
Traceback (most recent call last):
File "c:\salt\bin\lib\site-packages\salt\modules\win_service.py", line 449, in stop
win32serviceutil.StopService(name)
File "c:\salt\bin\lib\site-packages\win32\lib\win32serviceutil.py", line 409, in StopService
return ControlService(serviceName, win32service.SERVICE_CONTROL_STOP, machine)
File "c:\salt\bin\lib\site-packages\win32\lib\win32serviceutil.py", line 320, in ControlService
status = win32service.ControlService(hs, code)
pywintypes.error: (1062, 'ControlService', 'The service has not been started.')

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "c:\salt\bin\lib\site-packages\salt\cli\caller.py", line 212, in call
ret['return'] = func(*args, **kwargs)
File "c:\salt\bin\lib\site-packages\salt\modules\win_service.py", line 488, in restart
return stop(name) and start(name)
File "c:\salt\bin\lib\site-packages\salt\modules\win_service.py", line 451, in stop
if exc[0] != 1062:
TypeError: 'error' object does not support indexing

Restart the named service. This issues a stop command followed by a start.

Args:
name: The name of the service to restart.

.. note::
If the name passed is ``salt-minion`` a scheduled task is created and
executed to restart the salt-minion service.

Returns:
bool: ``True`` if successful, ``False`` otherwise

CLI Example:

.. code-block:: bash

salt '*' service.restart <service name>

# Fixed code
def stop(name, timeout=90):
    """
    Stop the specified service

    Args:
        name (str): The name of the service to stop

        timeout (int):
            The time in seconds to wait for the service to stop before
            returning. Default is 90 seconds

            .. versionadded:: 2017.7.9, 2018.3.4

    Returns:
        bool: ``True`` if successful, otherwise ``False``. Also returns ``True``
            if the service is already stopped

    CLI Example:

    .. code-block:: bash

        salt '*' service.stop <service name>
    """
    try:
        win32serviceutil.StopService(name)
    except pywintypes.error as exc:
        if exc.winerror != 1062:
            raise CommandExecutionError(
                "Failed To Stop {0}: {1}".format(name, exc.strerror)
            )
        log.debug('Service "{0}" is not running'.format(name))

    srv_status = _status_wait(
        service_name=name,
        end_time=time.time() + int(timeout),
        service_states=["Running", "Stop Pending"],
    )

    return srv_status["Status"] == "Stopped"