Skip to content

Register tracing log hook before signal handling - #13411

Merged
mxpv merged 1 commit into
containerd:mainfrom
moosepass:moosepass/fix-tracing-hook-order
Jun 25, 2026
Merged

mxpv merged 1 commit into
containerd:mainfrom
moosepass:moosepass/fix-tracing-hook-order

Conversation

@moosepass

@moosepass moosepass commented May 15, 2026 •

Copy link
Copy Markdown
Contributor

Description

Move tracing logrus hook registration earlier in daemon startup, immediately after config loading and CLI flags have been applied.

This avoids a startup ordering race where logrus hooks can be mutated after startup goroutines may already be logging.

Root Cause

This is not a logrus race that "shouldn't be possible". It is a real race that can happen when hook registration is delayed until after startup work begins.

The tracing hook used to be registered from init(), before daemon startup spawned goroutines. That ordering was safe because the hook was installed before concurrent startup logging could begin.

That changed when tracing hook registration moved into cmd/containerd/command/main.go. The move was needed because the hook now depends on config.Debug.LogTraceID, which is only known after config loading and CLI flag application. So the hook can no longer be registered from init().

However, the current placement registers the hook after signal handling setup has started.

On Windows, handleSignals calls setupDumpStacks. setupDumpStacks starts a goroutine whose first action is to log that it is waiting for the stackdump event: log.L.Debugf("Stackdump - waiting signal at %s", event).

That creates this startup ordering:

  1. Daemon config is loaded and flags are applied.
  2. Signal handling starts.
  3. On Windows, stackdump setup starts a goroutine.
  4. The stackdump goroutine logs immediately.
  5. Logging reads/copies logrus hooks.
  6. Main startup continues and adds the tracing hook.
  7. Adding the tracing hook mutates logrus hooks.

Normally logrus serializes hook access with its logger mutex. AddHook locks while mutating hooks, and the logging path locks while copying hooks before firing them.

However, users may run containerd with logrus locking disabled. One example is the containerd/log slog bridge, where UseSlog() calls SetNoLock(). Once locking is disabled, those Lock/Unlock calls become no-ops. At that point, logging can read/copy the hook map while AddHook mutates it, which can panic with a concurrent map iteration/write.

This is easiest to trigger on Windows because the stackdump goroutine logs immediately during signal handling setup. Unix signal handling does not start the same immediate stackdump logging goroutine, but the broader startup invariant still applies: hooks should be registered before startup goroutines that may log are started.

Fix

Register the tracing hook immediately after applyFlags, which is the earliest point where config.Debug.LogTraceID is available.

This preserves the LogTraceID configurability while restoring the previous ordering property: known hooks are registered before later startup steps may spawn goroutines that log.

The change is intentionally small: it only moves the existing tracing hook registration earlier in startup and documents why the placement matters.

Testing

  • go test ./cmd/containerd/command/... -count=1
  • go test ./cmd/containerd/... -count=1

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Moves registration of the tracing logrus hook to immediately after config load/flag application, so the hook is in place before later startup steps spawn goroutines that may emit log entries.

Changes:

  • Adds tracing hook registration right after applyFlags returns, gated on config.Debug.LogTraceID.
  • Removes the previous, later hook registration that ran after signal/cleanup setup.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@moosepass
moosepass marked this pull request as ready for review May 15, 2026 15:03
@austinvazquez

Copy link
Copy Markdown
Member

/ok-to-test

@moosepass
moosepass force-pushed the moosepass/fix-tracing-hook-order branch 2 times, most recently from 271e92f to cea539a Compare May 15, 2026 15:19
Copilot AI review requested due to automatic review settings June 2, 2026 20:40
@austinvazquez
austinvazquez force-pushed the moosepass/fix-tracing-hook-order branch from cea539a to 125c15b Compare June 2, 2026 20:40

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 1 out of 1 changed files in this pull request and generated no new comments.

@eginez

eginez commented Jun 2, 2026

Copy link
Copy Markdown
Contributor

LGTM

@austinvazquez austinvazquez moved this from Needs Triage to Merge on Green in Pull Request Review Jun 3, 2026

@austinvazquez austinvazquez left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Throwing up my LGTM. I just rebased it to kick CI.

@github-project-automation github-project-automation Bot moved this from Merge on Green to Review In Progress in Pull Request Review Jun 3, 2026
@samuelkarp
samuelkarp added this pull request to the merge queue Jun 3, 2026
@github-merge-queue
github-merge-queue Bot removed this pull request from the merge queue due to failed status checks Jun 3, 2026
@samuelkarp
samuelkarp added this pull request to the merge queue Jun 3, 2026
@github-merge-queue
github-merge-queue Bot removed this pull request from the merge queue due to failed status checks Jun 3, 2026
@samuelkarp
samuelkarp added this pull request to the merge queue Jun 3, 2026
@github-merge-queue
github-merge-queue Bot removed this pull request from the merge queue due to failed status checks Jun 4, 2026
@dmcgowan
dmcgowan added this pull request to the merge queue Jun 11, 2026
@github-merge-queue
github-merge-queue Bot removed this pull request from the merge queue due to failed status checks Jun 11, 2026
@austinvazquez

Copy link
Copy Markdown
Member

I opened #13353 to resolve an underlying issue that prevented the last merge queue run (https://github.com/containerd/containerd/actions/runs/27363955475/job/80859027083) from succeeding.

@mxpv
mxpv added this pull request to the merge queue Jun 25, 2026
Merged via the queue into containerd:main with commit fc5a32e Jun 25, 2026
82 of 88 checks passed
@github-project-automation github-project-automation Bot moved this from Review In Progress to Done in Pull Request Review Jun 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

Archived in project

Development

Successfully merging this pull request may close these issues.

8 participants