fleet icon indicating copy to clipboard operation
fleet copied to clipboard

mdm: `apple_mdm_dep_profile_assigner` cron silently drops device events when context-cancelled mid-run

Open robbiet480 opened this issue 6 months ago β€’ 5 comments

Fleet versions

  • Discovered:
  • Reproduced:

Web browser and operating system:


πŸ’₯ Β Actual behavior

When sync phase of DEP sync cron returned an error, cursor was advanced, leading to some number of events being dropped See https://github.com/fleetdm/fleet/blob/6a481477e8ac4bef43b5826e6240b867ac3b2379/server/mdm/nanodep/sync/syncer.go#L184

πŸ› οΈ Expected behavior

If DEP sync phase returns an error the cron should not advance the cursor(similar to what happens if the DEP fetch returns an error). Advancing the cursor means those events that were on the page where the error occurred will not be processed again.

Check the upstream code to see if this behavior is unique to fleet or also broken upstream and if so, open an issue(and potentially a fix PR if it makes sense) or perhaps start a thread in nano's macadmins channel

πŸ§‘β€πŸ’» Β Steps to reproduce

These steps:

  • [ ] Have been confirmed to consistently lead to reproduction in multiple Fleet instances.
  • [x] Describe the workflow that led to the error, but have not yet been reproduced in multiple Fleet instances.

Reproduction steps are uncertain. In the customer's case they are running fleet in a runtime which pauses fleet between requests, meaning there are sometimes errors from the crons when fleet is unpaused, potentially several seconds later

At any rate, when this happens and we are within this code: https://github.com/fleetdm/fleet/blob/6a481477e8ac4bef43b5826e6240b867ac3b2379/server/mdm/nanodep/sync/syncer.go#L184

If an error is returned from the syncer we may still advance the cursor in the DB, meaning we may not have processed the prior list of events but would still move on to the next page of events, potentially skipping some hosts

πŸ•―οΈ More info (optional)

Original bug report below

Summary

When the apple_mdm_dep_profile_assigner cron is terminated mid-execution (context canceled), the nanodep sync cursor in nano_dep_names.syncer_cursor can advance without the corresponding device upserts being committed to host_dep_assignments. Devices that Apple emitted during the broken window are effectively dropped: they appear assigned to Fleet in Apple Business Manager but Fleet has no host record, and Apple's DEP backend will not re-emit those events without an out-of-band trigger.

The only recovery is to manually clear the cursor per docs/Contributing/.../resetting-apple-dep-sync-cursor.md.

Environment

  • Self-hosted Fleet on Google Cloud Run
  • Cloud Run config that triggered it: cpu-throttling=true, min-instances=0 (CPU frozen between requests + scale-to-zero). The cron's background goroutines get killed before completion.
  • Other Cloud Run deployments are likely vulnerable to the same pattern any time the container is terminated mid-tick. The root cause is the cron's non-transactional cursor handling, not the specific runtime.

Reproduction

  1. Deploy Fleet on Cloud Run with cpu-throttling=true and min-instances=0.
  2. Assign macOS devices to the Fleet ADE server in Apple Business Manager with a migration deadline.
  3. Observe Fleet logs: repeated unlock failed: context canceled, update cron stats: context canceled, and pending job might still be running, wait 1m0s.
  4. Fix the runtime (--no-cpu-throttling --min-instances=1) so the cron runs cleanly.
  5. Observe that the just-assigned devices never appear in Fleet. Apple's DEP API returns cursor returned all devices previously β€” the events were delivered against the advancing cursor but the upserts never landed.

Expected behavior

Either:

  • The cron is transactional: the cursor only advances after device upserts commit. A context-cancel mid-run results in the same events being replayed on the next tick.
  • Or: on startup, the cron detects "previous tick crashed without completing" and either replays or resets the cursor automatically. The pending job might still be running, wait 1m0s path already knows there's a stale lock; that signal could trigger recovery.

Actual behavior

Cursor advances; device upserts are lost; Apple does not re-emit the events. Operator-side recovery (cursor reset in the DB) is required.

Evidence

Representative log entries during the broken window:

err: "context canceled"
msg: "unlock failed"
cron: "apple_mdm_dep_profile_assigner"
err: "1 error occurred:\n\t* context canceled\n\n"
msg: "update cron stats apple_mdm_dep_profile_assigner"
schedule: "apple_mdm_dep_profile_assigner"
msg: "pending job might still be running, wait 1m0s"

Even after the runtime was fixed and the cron began completed-ing cleanly each tick, multiple devices remained invisible to Fleet. ABM-side re-assignment attempts (Fleet β†’ other MDM β†’ Fleet) emitted only deleted events to the Fleet token; Apple's DEP API never followed up with a re-emission, so a simple re-assignment did not recover. Only a cursor reset (per the existing troubleshooting doc) brought the missing devices into Fleet.

In our case ~3 devices were affected. We confirmed by clearing the cursor and watching the next sync return a full enumeration including op_type=modified, profile_status=assigned β†’ pushed for the previously-stuck serials.

Suggested fix

  • Wrap the cursor update + device upsert in a single transaction (in nanodep or in Fleet's wrapper layer), so context-cancel does not leave them in inconsistent states.
  • Alternatively, when the pending job might still be running path detects a stale lock from a prior crash, treat that as a signal to reset the cursor automatically (with a log line so operators are aware).
  • Either way, surfacing the "previous run did not complete cleanly" state more visibly (a counter, an alertable log line, or a Fleet UI indicator) would help operators catch this before devices go missing.

Related

  • Existing troubleshooting doc: docs/Contributing/product-groups/mdm/troubleshooting/resetting-apple-dep-sync-cursor.md
  • fleetdm/fleet-terraform#242 / fleetdm/fleet-terraform#243 β€” tracks and patches the related GCP deployment-side trigger (the module did not set cpu_idle = false on the Cloud Run service). That PR mitigates the most common cause of context-cancellation on the GCP path but does not fix the underlying cursor-vs-upsert ordering bug described here; this issue remains valid for any other runtime where the container can be terminated mid-tick.

robbiet480 avatar May 26 '26 21:05 robbiet480

#38575 also would've made this easier to debug and reset the cursor so a πŸ‘ to that.

robbiet480 avatar May 26 '26 21:05 robbiet480

@robbiet480 What version of fleet did you see this on? You can check [your fleet server URL]/version to get the info. I think I see the potential issue in the code on latest just want to verify the code on the version you saw it on

JordanMontgomery avatar May 27 '26 15:05 JordanMontgomery

{
  "version": "4.85.0",
  "branch": "HEAD",
  "revision": "732ae4e1229bbc6be98a6fd46b0026d4977826e1",
  "go_version": "go1.26.3",
  "build_date": "2026-05-14",
  "build_user": "runner"
}

robbiet480 avatar May 27 '26 16:05 robbiet480

Verified

Verified the DEP syncer no longer advances the sync cursor when the device-write callback fails β€” so events on a failed page are replayed on the next sync instead of being silently dropped.

The real failure mode (a context-cancel landing between the device upsert and the cursor write) isn't reproducible on a released binary, so confidence comes from three angles: automated tests, a pre-fix reproduction, and a hands-on error-injection on a live server. Environment: Premium, 4 ADE hosts (2 macOS, 2 iOS); unit/integration run locally against an isolated test DB.

Automated coverage (Claude)

  • 7 unit tests (server/mdm/nanodep/sync) pass on the fix: cursor not advanced on callback error (fetch phase, sync phase, and mid-paging MoreToFollow for both); cursor advanced on success; not advanced on an Apple API error; expired-cursor still resets to empty + re-fetches; StoreCursor error propagates out of Run
  • Integration test TestDEPSyncCursorPersistedAfterSuccessfulSync passes: full RunAssigner β†’ callback β†’ MySQL persists Apple's cursor to nano_dep_names.syncer_cursor after a clean sync

Pre-fix reproduction (3.89, separate worktree)

  • Reverting the one-line guard (err == nil &&) in a throwaway worktree at the fix's parent flips the callback-error tests to FAIL β€” the cursor advances past the failed page (e.g. stored cursor jumps fetch-cursorβ†’sync-cursor, and to page-one-cursor where it should stay empty). The Apple-API-error control still passes. Confirms the tests pin the bug and the fix resolves it.
  • Same verification in 4.89 without reversion.

Live behavior (4.90)

  • Clean sync advances the cursor and drops nothing: triggering apple_mdm_dep_profile_assigner moved syncer_cursor_at forward with host_dep_assignments count unchanged
  • Cursor-reset recovery works: syncer_cursor = NULL β†’ next sync does a full fetch-devices, re-baselines assignments, stores a fresh cursor; no re-enrollment or duplicate hosts

Manual error-branch (hands-on, live)

  • Forced the callback to error on a real ABM delta: installed a BEFORE INSERT trigger on host_dep_assignments (fails the callback's device write), then re-assigned a macOS host in ABM to create an added delta
  • With the write blocked, the cursor held at baseline across 3 manual triggers + the periodic cron and the device stayed absent; dropping the trigger and re-syncing advanced the cursor and ingested the device. Same delta both times β€” only the callback outcome differed β€” isolating the fix's guarantee that the cursor advances only after the device write commits

chrstphr84 avatar Jul 24 '26 16:07 chrstphr84

Fleet's sync, like a cloud, Advances, but can leave a shroud. Fix brings light, no doubt.

fleet-release avatar Aug 06 '26 02:08 fleet-release