I think I have a bug that I want to report. GitHub discourages making issues and points this way.
Repo: elastic/fleet-server (internal/pkg/policy/monitor.go)
Version: Fleet Server, Elastic Agent, Elasticsearch and Kibana 9.5.5. Fleet Server runs as the
elastic-agent container; a single Fleet Server, default limits (run policy monitor burst=1 rate=200).
What happens
A fresh deployment enrolls 9 Windows agents into one agent policy within about a minute of each
other. Six receive the policy normally. The other three enroll, keep checking in, and stay at
fleet: (HEALTHY) Connected
elastic-agent: (STARTING) Waiting for initial configuration and composable variables
indefinitely (observed for more than nine hours). Kibana shows them as updating with
policy_revision: null. They run no inputs and ship no data. Neither Fleet Server nor the agents
log a warning or an error about it.
The same deployment was brought up four times from identical images and configuration. It
happened in one of the four.
Timeline
Times are relative to the first Windows enrollment. "First check-in" is the first
applying new components data line for the agent; "dispatched" is generating output API key.
| Agent | Enrolled | First check-in | Dispatched |
|---|---|---|---|
| A | +0:00.0 | +1:20.0 | +1:20.2 |
| B | +0:00.0 | +1:20.2 | +1:20.5 |
| C | +0:06.0 | +1:26.0 | +1:26.3 |
| D | +0:32.7 | +1:38.3 | +1:38.6 |
| E | +0:51.2 | +1:56.1 | +1:56.3 |
| F | +1:08.2 | +2:13.099 | +2:13.353 |
| G | +1:00.2 | +2:13.099 | never |
| H | +1:05.1 | +2:28.4 | never |
| I | +6:05.5 | +7:10.7 | never |
Every healthy agent is dispatched about 250 ms after its first check-in. Agents F and G have
first check-ins logged in the same millisecond; F is served, G is not. After that point no agent
is served, including H and I, which check in alone 15 seconds and 5 minutes later. So this is not
three agents each losing a race: dispatch for the policy stops after F and does not resume.
Log lines for a healthy agent (F) and a stuck one (G), complete for each:
+2:13.099 info F applying new components data
+2:13.099 info F applying new rollback data
+2:13.353 info F generating output API key <id>:default
+2:13.383 info F Updating agent record to pick up default output key.
+2:13.657 info F Action delivered to agent on checkin
+2:14.149 info F ack event
+2:14.423 info F ack policy
+2:13.099 info G applying new components data
+2:13.099 info G applying new rollback data
(nothing further for G)
None of the early-exit messages in dispatchPending appear anywhere in the log (Policy limit error, logic error: policy missing on dispatch, logic error: should never block on policy channel). Debug logging was not enabled, so deploy pending on subscribe, dispatch policy change and policy monitor dispatch complete are not available.
Recovery
Saving the agent policy unchanged in Kibana (revision 6 to 7) fixes it immediately. All three
stuck agents log generating output API key within 10 ms of each other, acknowledge the policy and
go online. Nothing was restarted.
Analysis
Subscribe() only wakes the dispatcher when the pending queue was empty before the push:
case s.isUpdate(&p.pp.Policy):
empty := m.pendingQ.isEmpty()
m.pendingQ.pushBack(s)
...
if empty {
m.kickDeploy()
}
That makes any stranded subscription permanent. If one subscription is ever left in pendingQ
with no drain running and no signal in deployCh, the queue is non-empty for every later
Subscribe(), so none of them kicks. Agents re-subscribe at the end of each long poll, but with
more than one agent stranded their polls overlap and the queue never empties. The only remaining
wake-ups are the ones in Run() after loadPolicies and processHits, which is why a new policy
revision clears it.
This matches everything observed: the stall begins at one instant, every later subscriber to that
policy is affected, nothing is logged, and a new revision drains all of them at once.
What we have not identified is how the first subscription (G) was stranded. dispatchPending
releases m.mut between popFront() and the next queue access (per the comment, so that
Subscribe/Unsubscribe/updatePolicy are not blocked during rate limiting and the channel send),
and F and G subscribed within the same millisecond while a drain was in progress. We could not
construct the exact interleaving from reading the code, and we have not reproduced it on demand.
Suggested fix
- Make the wake-up unconditional in
Subscribe()(kickDeploy()is already non-blocking and
coalesces), or haverunDispatcherre-checkpendingQon a timer. Either removes the
"stranded forever" property regardless of the triggering race. - Log at warn level when a subscription has been pending longer than some threshold, so the
condition is visible without debug logging.