Skip to content

NRI deadlock between plugin-initiated Runtime.UpdateContainers and CRI lifecycle/update events #13733

Closed
containerd/nri
#301

Description

@Yukielz

Description

We observed a containerd deadlock involving NRI when an already-registered NRI plugin actively calls Runtime.UpdateContainers from its own reconciliation loop while CRI container lifecycle/update events are being processed.

This looks related to the lock-order problem discussed in #10085, but the trigger path is different. #10085 was about external plugin registration / initial sync. This issue is about a registered plugin calling Runtime.UpdateContainers after it has already been synchronized.

In our setup, the NRI plugin has a reconciliation loop. After registration and synchronization, it periodically computes the desired resource configuration for running containers and actively calls Runtime.UpdateContainers to apply container resource updates. This call is not a response to containerd's UpdateContainer hook; it is initiated by the plugin itself.

For the same container resource update, two NRI paths may run concurrently:

  1. Kubelet calls CRI UpdateContainerResources because it wants to update the container resources. containerd handles this as a CRI-initiated update. In this path, containerd calls the NRI plugin's UpdateContainer hook first, applies the runtime resource update, and then calls the NRI PostUpdateContainer event after the update succeeds.

  2. Independently, the already-registered NRI plugin may notice from its own reconciliation loop that the container should have updated resources, and it actively calls runtime UpdateContainers to push the desired resources into containerd.

These two paths can target the same container and the same resource update. They are logically about the same update, but they enter NRI from opposite directions:

CRI-initiated path:      containerd -> NRI plugin
plugin-initiated path:   NRI plugin -> containerd

In the observed case, kubelet kept retrying UpdateContainerResources because the runtime-reported resources still differed from the desired resources. At the same time, the NRI plugin's reconciliation loop was also trying to apply its desired container resources via Runtime.UpdateContainers.

The deadlock involves two locks:

  1. pkg/nri.local mutex in pkg/nri/nri.go
  2. Adaptation mutex in vendor/github.com/containerd/nri/pkg/adaptation/adaptation.go

The problematic lock order is:

  • CRI event path:

    • pkg/nri.(*local).PostUpdateContainer holds local.Lock
    • then calls l.nri.PostUpdateContainer
    • which enters Adaptation.StateChange and waits for Adaptation.Lock
  • Plugin-initiated update path:

    • the NRI plugin calls Runtime.UpdateContainers
    • containerd receives it in plugin.UpdateContainers
    • Adaptation.updateContainers holds Adaptation.Lock
    • then calls runtime updateFn
    • runtime updateFn is pkg/nri.(*local).updateFromPlugin, which waits for local.Lock

This creates an AB-BA deadlock:

CRI event:          local.Lock      -> waits for Adaptation.Lock
plugin update:      Adaptation.Lock -> waits for local.Lock

Steps to reproduce the issue

  1. Run containerd with NRI enabled and an external NRI plugin registered.
  2. The plugin should have its own reconciliation loop and actively call Runtime.UpdateContainers for already-running containers after registration/synchronization.
  3. In parallel, trigger CRI UpdateContainerResources for a running container.
  4. After the runtime resource update succeeds, CRI calls PostUpdateContainerResources, which calls into NRI PostUpdateContainer.
  5. At the same time, the plugin reconciliation loop calls Runtime.UpdateContainers for the same container or another container.
  6. If the CRI post-update event path and the plugin-initiated Runtime.UpdateContainers path overlap, containerd can deadlock.

The overlap can happen more easily when kubelet repeatedly calls UpdateContainerResources for the same container, for example when the observed runtime resources do not match the desired resources and kubelet keeps retrying.

Describe the results you received and expected

Actual result:

  • containerd stopped returning CRI RPCs.
  • UpdateContainerResources, StopPodSandbox, RemoveContainer, and RemovePodSandbox started timing out from kubelet/crictl with DeadlineExceeded.
  • Restarting containerd was required to recover.

Expected result:

  • A plugin-initiated Runtime.UpdateContainers request should not deadlock CRI lifecycle or update events.
  • CRI RPCs should either complete or fail normally, but should not block indefinitely due to NRI lock inversion.

Relevant goroutine stacks from ctr pprof goroutines:

goroutine ... [sync.Mutex.Lock, 5 minutes]:
internal/sync.(*Mutex).lockSlow(...)
sync.(*Mutex).Lock(...)
github.com/containerd/containerd/pkg/nri.(*local).updateFromPlugin(...)
    .../containerd/pkg/nri/nri.go:488
github.com/containerd/nri/pkg/adaptation.(*Adaptation).updateContainers(...)
    .../containerd/vendor/github.com/containerd/nri/pkg/adaptation/adaptation.go:318
github.com/containerd/nri/pkg/adaptation.(*plugin).UpdateContainers(...)
    .../containerd/vendor/github.com/containerd/nri/pkg/adaptation/plugin.go:377
github.com/containerd/nri/pkg/api.RegisterRuntimeService.func2(...)
    .../containerd/vendor/github.com/containerd/nri/pkg/api/api_ttrpc.pb.go:30
goroutine ... [sync.Mutex.Lock, 5 minutes]:
internal/sync.(*Mutex).lockSlow(...)
sync.(*Mutex).Lock(...)
github.com/containerd/nri/pkg/adaptation.(*Adaptation).StateChange(...)
    .../containerd/vendor/github.com/containerd/nri/pkg/adaptation/adaptation.go:299
github.com/containerd/nri/pkg/adaptation.(*Adaptation).PostUpdateContainer(...)
    .../containerd/vendor/github.com/containerd/nri/pkg/adaptation/adaptation.go:263
github.com/containerd/containerd/pkg/nri.(*local).PostUpdateContainer(...)
    .../containerd/pkg/nri/nri.go:369
github.com/containerd/containerd/pkg/cri/nri.(*API).PostUpdateContainerResources(...)
    .../containerd/pkg/cri/nri/nri_api_linux.go:204
github.com/containerd/containerd/pkg/cri/server.(*criService).UpdateContainerResources(...)
    .../containerd/pkg/cri/server/container_update_resources.go:70

Additional blocked CRI calls were waiting on the same pkg/nri.local mutex:

goroutine ... [sync.Mutex.Lock]:
github.com/containerd/containerd/pkg/nri.(*local).StopPodSandbox(...)
    .../containerd/pkg/nri/nri.go:194
github.com/containerd/containerd/pkg/cri/nri.(*API).StopPodSandbox(...)
    .../containerd/pkg/cri/nri/nri_api_linux.go:107
github.com/containerd/containerd/pkg/cri/server.(*criService).StopPodSandbox(...)
    .../containerd/pkg/cri/server/sandbox_stop.go:55
goroutine ... [sync.Mutex.Lock]:
github.com/containerd/containerd/pkg/nri.(*local).RemoveContainer(...)
    .../containerd/pkg/nri/nri.go:423
github.com/containerd/containerd/pkg/cri/nri.(*API).RemoveContainer(...)
    .../containerd/pkg/cri/nri/nri_api_linux.go:258
github.com/containerd/containerd/pkg/cri/server.(*criService).RemoveContainer(...)
    .../containerd/pkg/cri/server/container_remove.go:94

What version of containerd are you using?

Observed on a downstream build based on:

containerd v1.7.31
github.com/containerd/nri v0.8.0

I also checked upstream code and the same lock-order pattern appears to still exist in:

v1.7.33
v2.0.10
v2.1.9
v2.2.5
v2.3.2
main

Any other relevant information

Related prior issues/PRs:

However, BlockPluginSync only blocks plugin synchronization/registration. It does not block an already registered plugin from calling Runtime.UpdateContainers.

Potential fix direction:

Adaptation.updateContainers currently holds Adaptation.Lock while calling runtime updateFn:

func (r *Adaptation) updateContainers(ctx context.Context, req []*ContainerUpdate) ([]*ContainerUpdate, error) {
    r.Lock()
    defer r.Unlock()

    return r.updateFn(ctx, req)
}

Since updateFn calls back into the runtime and can acquire pkg/nri.local lock, holding Adaptation.Lock across this callback can deadlock with CRI event paths.

A minimal fix may be to avoid holding Adaptation.Lock while calling updateFn, or otherwise ensure all paths acquire locks in a single consistent order.

Activity

  1. added
    area/nri
    on Jul 8, 2026
  2. mikebrow commented on Jul 14, 2026

    @mikebrow
    Member
  3. fuweid commented on Jul 16, 2026

    @fuweid
    Member

    I think @Yukielz suggestion is quick fix for this deadlock issue. Based on my understanding, we can remove r.Lock() in Adaptation layer saftely because Adaptation.updateContainers.updateFn is kind of immutable. And we have local.Lock in CRI plugin so CRI plugin doesn't run NRI plugin concurrently. https://github.com/containerd/nri/pull/301/changes looks good to me.

    cc @klihub

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area/nrikind/bug

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions