Repository navigation
Description
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:
-
Kubelet calls CRI
UpdateContainerResourcesbecause it wants to update the container resources. containerd handles this as a CRI-initiated update. In this path, containerd calls the NRI plugin'sUpdateContainerhook first, applies the runtime resource update, and then calls the NRIPostUpdateContainerevent after the update succeeds. -
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
UpdateContainersto 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:
pkg/nri.localmutex inpkg/nri/nri.goAdaptationmutex invendor/github.com/containerd/nri/pkg/adaptation/adaptation.go
The problematic lock order is:
-
CRI event path:
pkg/nri.(*local).PostUpdateContainerholdslocal.Lock- then calls
l.nri.PostUpdateContainer - which enters
Adaptation.StateChangeand waits forAdaptation.Lock
-
Plugin-initiated update path:
- the NRI plugin calls
Runtime.UpdateContainers - containerd receives it in
plugin.UpdateContainers Adaptation.updateContainersholdsAdaptation.Lock- then calls runtime
updateFn - runtime
updateFnispkg/nri.(*local).updateFromPlugin, which waits forlocal.Lock
- the NRI plugin calls
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
- Run containerd with NRI enabled and an external NRI plugin registered.
- The plugin should have its own reconciliation loop and actively call
Runtime.UpdateContainersfor already-running containers after registration/synchronization. - In parallel, trigger CRI
UpdateContainerResourcesfor a running container. - After the runtime resource update succeeds, CRI calls
PostUpdateContainerResources, which calls into NRIPostUpdateContainer. - At the same time, the plugin reconciliation loop calls
Runtime.UpdateContainersfor the same container or another container. - If the CRI post-update event path and the plugin-initiated
Runtime.UpdateContainerspath 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, andRemovePodSandboxstarted timing out from kubelet/crictl withDeadlineExceeded.- Restarting containerd was required to recover.
Expected result:
- A plugin-initiated
Runtime.UpdateContainersrequest 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:
- NRI plugin registration can trigger a deadlock #10085 reported a similar NRI lock inversion during plugin registration / initial sync.
- mod: bump github.com/containerd/[email protected] #10089 fixed NRI plugin registration can trigger a deadlock #10085 by bumping github.com/containerd/nri to v0.6.1.
- cri,nri: fix initial sync race of registering NRI plugins. #11384 added
BlockPluginSyncto block plugin synchronization during CRI event processing.
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.
Metadata
Metadata
Assignees
Type
Projects
- StatusShow more project fieldsDone
Activity
mikebrow commentedon Jul 14, 2026
/cc @fuweid
fuweid commentedon Jul 16, 2026
I think @Yukielz suggestion is quick fix for this deadlock issue. Based on my understanding, we can remove
r.Lock()in Adaptation layer saftely becauseAdaptation.updateContainers.updateFnis kind of immutable. And we havelocal.Lockin 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
adaptation: avoid holding lock across runtime update callback
adaptation: avoid holding lock across runtime update callback
adaptation: avoid holding lock across runtime update callback