Skip to content

[BUG] watch/watch_in does not trigger a service restart if service is also being changed from disabled to enabled #59688

Closed

Description

@Orlando-Richards

Description

When a systemd managed service is running, but not "enabled", salt will change the state to enabled in a test run. However, any watch/watch_in relationships which would trigger in the same state run do not cause a service restart.

Test mode reports that the service will be restarted.

Setup

Have a service which is disabled, but running (here, I've picked salt-master):

Create the following state:

/tmp/servicerestart:
  file.touch

salt-master:
  service.running:
    - enable: True
    - watch:
      - /tmp/servicerestart

Steps to Reproduce the behavior

Ensure service is disabled, but running:

systemctl disable salt-master
systemctl start salt-master

Apply the state above, in test=false mode:

# salt-call state.apply servicerestart test=false
local:
----------
          ID: /tmp/servicerestart
    Function: file.touch
      Result: True
     Comment: Created empty file /tmp/servicerestart
     Started: 09:16:22.108305
    Duration: 11.975 ms
     Changes:
              ----------
              new:
                  /tmp/servicerestart
----------
          ID: salt-master
    Function: service.running
      Result: True
     Comment: Service salt-master has been enabled, and is in the desired state
     Started: 09:16:22.137798
    Duration: 342.0 ms
     Changes:
              ----------
              salt-master:
                  True

Summary for local
------------
Succeeded: 2 (changed=2)
Failed:    0
------------
Total states run:     2
Total run time: 353.975 ms

Applying the state while the service is "enabled" does, correctly, result in a restart of the service.

Expected behavior

Service will be restarted, and also be enabled.

Screenshots

n/a

Versions Report

salt --versions-report (Provided by running salt --versions-report. Please also mention any differences in master/minion versions.)
Salt Version:
          Salt: 3002

Dependency Versions:
          cffi: 1.14.5
      cherrypy: unknown
      dateutil: 2.8.1
     docker-py: 4.4.4
         gitdb: 4.0.5
     gitpython: 3.1.13
        Jinja2: 2.11.3
       libgit2: Not Installed
      M2Crypto: 0.37.1
          Mako: Not Installed
       msgpack: 0.6.1
  msgpack-pure: Not Installed
  mysql-python: Not Installed
     pycparser: 2.20
      pycrypto: Not Installed
  pycryptodome: 3.10.1
        pygit2: Not Installed
        Python: 3.6.8 (default, Nov 16 2020, 16:55:22)
  python-gnupg: Not Installed
        PyYAML: 5.4.1
         PyZMQ: 18.0.2
         smmap: 3.0.5
       timelib: Not Installed
       Tornado: 4.5.3
           ZMQ: 4.3.1

System Versions:
          dist: centos 7 Core
        locale: UTF-8
       machine: x86_64
       release: 3.10.0-1160.6.1.el7.x86_64
        system: Linux
       version: CentOS Linux 7 Core
</details>

**Additional context**

Activity

  1. added
    bug
    on Mar 3, 2021
  2. sagetherage commented on Mar 3, 2021

    @sagetherage
    Contributor

    @OrlandoArcapix Salt v3002 is vulnerable, have you been able to upgrade to 3002.5 and this is still an issue?

  3. Orlando-Richards commented on Mar 3, 2021

    @Orlando-Richards
    ContributorAuthor

    @OrlandoArcapix Salt v3002 is vulnerable, have you been able to upgrade to 3002.5 and this is still an issue?

    Yeah - i'm on 3002.5 - but it's a virtualenv install - not sure why it's showing 3002, rather than 3002.5 - will check into that.

    3002.5 definitely affected!

  4. Orlando-Richards commented on Mar 3, 2021

    @Orlando-Richards
    ContributorAuthor

    As a workaround, having a separate state for enabled works fine:

    # cat servicerestart.sls 
    /tmp/servicerestart:
      file.touch
    
    salt-master:
      service.running:
        - watch:
          - /tmp/servicerestart
    
    enable salt-master service:
      service.enabled:
        - name: salt-master
    
    # systemctl disable salt-master
    Removed symlink /etc/systemd/system/multi-user.target.wants/salt-master.service.
    # salt-call state.apply servicerestart test=false
    local:
    ----------
              ID: /tmp/servicerestart
        Function: file.touch
          Result: True
         Comment: Updated times on file /tmp/servicerestart
         Started: 22:50:07.681575
        Duration: 12.283 ms
         Changes:   
                  ----------
                  touched:
                      /tmp/servicerestart
    ----------
              ID: salt-master
        Function: service.running
          Result: True
         Comment: Service restarted
         Started: 22:50:07.765542
        Duration: 4012.299 ms
         Changes:   
                  ----------
                  salt-master:
                      True
    ----------
              ID: enable salt-master service
        Function: service.enabled
            Name: salt-master
          Result: True
         Comment: Service salt-master has been enabled, and is in the desired state
         Started: 22:50:11.778395
        Duration: 197.627 ms
         Changes:   
                  ----------
                  salt-master:
                      True
    
    Summary for local
    ------------
    Succeeded: 3 (changed=3)
    Failed:    0
    ------------
    Total states run:     3
    Total run time:   4.222 s
    
  5. Dr-Bone commented on Mar 21, 2021

    @Dr-Bone

    Hi
    I can confirm this bug with version 3002.5.
    First I assumed it has something to do with if the file was present and only modified or not present, but it is only affected by the enabled/disabled state of the systemd service.
    To be honest, I would rather not want to apply this work-around to all my states since this is only temporary and can be removed as soon as this bug is fixed. Is there any timeline/eta yet for this bug?
    thank you very much!

  6. added
    severity-high
    and removed on Mar 22, 2021
  7. added this to the Approved milestone on Mar 22, 2021
  8. sagetherage commented on Mar 22, 2021

    @sagetherage
    Contributor

    I will need to look into getting this fixed and when.

  9. added
    Phosphorus v3005.0
    on Jun 18, 2021
  10. removed
    Phosphorus v3005.0
    on Mar 30, 2022
  11. self-assigned this
    on May 23, 2023
  12. MKLeb commented on Jun 30, 2023

    @MKLeb
    Contributor

    Hi @OrlandoArcapix, after looking into this for a bit, I seem to have found some relevant documentation detailing that this is expected behavior: https://docs.saltproject.io/en/latest/ref/states/requisites.html#watch.

    Specifically...

    If the watching state changes key contains values, then mod_watch will not be called. If you're using watch or watch_in then it's a good idea to have a state that only enforces one attribute - such as splitting out service.running into its own state and have service.enabled in another.

    Closing this as expected behavior.

  13. mitar commented on Nov 27, 2026

    @mitar
    Contributor

    I made a PR to address this: #70974

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

bugseverity-high

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions