salt icon indicating copy to clipboard operation
salt copied to clipboard

[Bug]: state.apply queue=<int> deadlocks when three or more state runs start at once on a minion (regression from #69826)

Open feinom opened this issue 1 month ago • 0 comments

What happened?

Since the fix for #69825 (PR #69826, shipped in 3008.3), salt.utils.state.check_prior_running_states() treats every other running state.* job that has a real PID as blocking, regardless of JID order. But a state run that is waiting in _wait() (the queue=<int> path in salt.modules.state._check_queue()) is itself a running state.* job with a PID. So with three runs A, B, C started at the same time on one minion:

  1. A runs. B and C wait on A (correct).
  2. A finishes. B now sees C (running PID) and C sees B. Both wait forever.
  3. Every later state.* run on that minion blocks on B and C.

Two concurrent runs are fine, because B only ever waits on A. The deadlock starts at three. _wait() has no timeout, so the jobs never return and never remove their flag files.

The queue flag that _set_queue_flag() writes (<state_queue_dir>/<jid>, containing the PID) is only ever read by _clear_queue_flag(). The blocking check has no way to tell a waiting job from a running one. master has the same logic today.

In production this hit us when an orchestration ran three salt.state steps with queue: 10 against the same minion within one second. Six later runs queued behind the two deadlocked ones, until we killed all nine with saltutil.kill_job and deleted the flag files by hand. (We use an integer instead of queue: True because on 3008.3 queue: True persists the job to the disk queue and returns __no_return__, which an orchestration reports as "Minion did not return a state result".)

Steps to reproduce (any sls works; sleep 8 just makes the overlap visible):

salt --async minion1 state.single cmd.run name="sleep 8" queue=10
salt --async minion1 state.single cmd.run name="sleep 8" queue=10
salt --async minion1 state.single cmd.run name="sleep 8" queue=10
sleep 60
salt minion1 saltutil.running

Actual behaviour: the first job returns. The other two stay in saltutil.running indefinitely, with no return and no error, and /var/cache/salt/minion/queues/<master>/state_queue/ keeps their <jid> flag files. Every later state.apply ... queue=<int> on the minion hangs as well.

Expected behaviour: the three runs execute one at a time, in JID order, as the queue argument is documented to do.

Possible fix: in check_prior_running_states(), treat a running job whose queue flag file exists like a queued placeholder: it blocks only when its JID sorts before ours.

pid = data.get("pid")
if pid and not os.path.exists(
    os.path.join(state_queue_dir(opts), str(data_jid))
):
    # A running state blocks us, unless it is itself waiting in
    # the queue (flag written by state._set_queue_flag). A waiting
    # state blocks only younger jobs, otherwise two waiting states
    # wait for each other forever.
    ret.append(data)
elif str(data_jid) < str(jid):
    ret.append(data)

With this change three and four simultaneous queue=10 runs completed back to back on our minions, with no overlap and no leftover flag files. We run it as a local patch on 3008.3.

Type of salt install

Official rpm

Major version

3008.x

What supported OS are you seeing the problem on? Can select multiple. (If bug appears on an unsupported OS, please open a GitHub Discussion instead)

almalinux-9

salt --versions-report output

Salt Version:
          Salt: 3008.3
 
Python Version:
        Python: 3.14.7 (main, Sep 30 2026, 03:35:11) [GCC 11.2.0]
 
Dependency Versions:
          cffi: 2.0.0
      cherrypy: 18.10.0
  cryptography: 50.0.1
      dateutil: 2.9.0.post0
     docker-py: Not Installed
         gitdb: 4.0.12
     gitpython: 3.2.0
        Jinja2: 3.1.6
       libgit2: Not Installed
  looseversion: 1.3.0
      M2Crypto: Not Installed
          Mako: Not Installed
       msgpack: 1.2.1
  msgpack-pure: Not Installed
  mysql-python: Not Installed
     packaging: 24.0
     pycparser: 3.00
      pycrypto: Not Installed
  pycryptodome: 3.23.0
        pygit2: Not Installed
  python-gnupg: 0.5.6
        PyYAML: 6.0.3
         PyZMQ: 27.1.0
        relenv: 0.22.27
         smmap: 5.0.2
       timelib: 0.3.0
       Tornado: 6.5.10
           ZMQ: 4.3.5
 
Salt Package Information:
  Package Type: onedir
 
System Versions:
          dist: almalinux 9.8 Olive Jaguar
        locale: utf-8
       machine: x86_64
       release: 5.14.0-687.20.1.el9_8.x86_64
        system: Linux
       version: AlmaLinux 9.8 Olive Jaguar

feinom avatar Oct 27 '26 13:10 feinom