Skip to content

Commit 36e914a

Browse files
committed
Use stable ZMQ identity for minion + syndic daemon ret-port REQ sockets
libzmq's ROUTER socket does not reclaim per-peer routing-id table entries when a TCP peer disconnects -- they live until the same routing-id reconnects (and ROUTER_HANDOVER=1 replaces them). Minions have been connecting without setting ZMQ_IDENTITY, so every reconnect generated a fresh random routing-id and a fresh leaked peer entry on the master's MWorkerQueue ROUTER. Under sustained stress that process was observed leaking +23 GB / 2 days on a 3-minion soak. Set a stable, per-AsyncReqMessageClient-instance identity on minion and syndic daemons (mirroring what was already done for the CLI): * a process-lifetime counter ensures siblings within one process get different identities -- the previous syndic CI failure came from giving every sibling the same identity, which triggered ROUTER_HANDOVER on the master to drop in-flight replies; * the same socket instance reuses its slot across libzmq reconnects, so the master's ROUTER replaces the prior peer entry instead of leaking one per reconnect; * on daemon restart, slots replay in construction order so the prior entries get overwritten cleanly.
1 parent 40febc8 commit 36e914a

1 file changed

Lines changed: 34 additions & 2 deletions

File tree

‎salt/transport/zeromq.py‎

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import datetime
66
import errno
77
import hashlib
8+
import itertools
89
import logging
910
import os
1011
import signal
@@ -48,6 +49,17 @@
4849
# Payload marker for AsyncReqMessageClient queue: stop _send_recv gracefully.
4950
_REQ_QUEUE_SHUTDOWN = object()
5051

52+
# Per-process counter used to give each AsyncReqMessageClient instance a
53+
# stable, unique routing-id slot. Long-lived daemons (minions, syndics)
54+
# multiplex multiple concurrent REQ sockets over one process, so each
55+
# socket must claim a distinct identity -- otherwise the master's
56+
# ROUTER_HANDOVER=1 would drop in-flight replies when a sibling socket
57+
# reconnected with the same identity. Within a single socket instance the
58+
# identity is reused across ZMQ-level reconnects, which is what lets the
59+
# master's ROUTER replace the previous peer table entry instead of
60+
# leaking one per reconnect.
61+
_REQ_IDENTITY_SLOT = itertools.count()
62+
5163

5264
def _get_master_uri(master_ip, master_port, source_ip=None, source_port=None):
5365
"""
@@ -631,8 +643,10 @@ def _init_socket(self):
631643
# own REQ churn is bounded anyway (one peer per daemon), so they
632644
# can keep using libzmq's default per-connection random
633645
# routing-ids.
634-
if not self.opts.get("__role"):
635-
role = self.opts.get("id") or "clir"
646+
_role = self.opts.get("__role")
647+
_minion_id = self.opts.get("id")
648+
if not _role:
649+
role = _minion_id or "clir"
636650
try:
637651
uid = os.getuid()
638652
except AttributeError: # Windows
@@ -644,6 +658,24 @@ def _init_socket(self):
644658
slot=os.getpid() % 256,
645659
)
646660
self.socket.setsockopt(zmq.IDENTITY, identity.encode("utf-8"))
661+
elif _role in ("minion", "syndic") and _minion_id:
662+
# Long-lived minion / syndic daemon. Each AsyncReqMessageClient
663+
# instance gets its own slot from a process-lifetime counter so
664+
# concurrent siblings differ (avoiding the ROUTER_HANDOVER drop
665+
# that caused the earlier syndic regression), while the slot is
666+
# reused across ZMQ-level reconnects so the master's ROUTER
667+
# replaces the prior peer entry instead of leaking one per
668+
# reconnect. Without this, ``MWorkerQueue`` was observed
669+
# leaking ~23 GB / 2 days under sustained stress as libzmq
670+
# never reclaims routing-id table entries. On daemon restart
671+
# slots replay in construction order and overwrite the prior
672+
# master-side entries cleanly.
673+
identity = "salt-req/{role}/{minion_id}/{slot}".format(
674+
role=_role,
675+
minion_id=_minion_id,
676+
slot=next(_REQ_IDENTITY_SLOT),
677+
)
678+
self.socket.setsockopt(zmq.IDENTITY, identity.encode("utf-8"))
647679

648680
_set_tcp_keepalive(self.socket, self.opts)
649681
if self.addr.startswith("tcp://["):

0 commit comments

Comments
 (0)