Skip to content

adaptation: avoid holding lock across runtime update callback - #301

Merged
mikebrow merged 1 commit into
containerd:mainfrom
ningmingxiao:fix_deadlock
Jul 18, 2026
Merged

mikebrow merged 1 commit into
containerd:mainfrom
ningmingxiao:fix_deadlock

Conversation

@ningmingxiao

@ningmingxiao ningmingxiao commented Jul 10, 2026 •

Copy link
Copy Markdown
Contributor

fix containerd/containerd#13733

Avoid containerd deadlock when an NRI plugin calls UpdateContainers during concurrent container updates

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

This PR aims to address the deadlock reported in containerd/containerd#13733 by adjusting lock behavior in the NRI runtime adaptation, introducing an optional external mutex intended to control lock ordering between the runtime and the adaptation layer.

Changes:

  • Added adaptMu to Adaptation and a new option WithAdaptLock to configure an external mutex.
  • Updated updateContainers to acquire the optional external mutex before acquiring the internal Adaptation mutex.

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

Comment thread pkg/adaptation/adaptation.go Resolved
Comment thread pkg/adaptation/adaptation.go Resolved
@fuweid fuweid changed the title nri:fix deadlock adaptation: avoid holding lock across runtime update callback Jul 16, 2026
@fuweid

fuweid commented Jul 16, 2026 •

Copy link
Copy Markdown
Member

@ningmingxiao could you please update the git commit message with new title and this information? Thanks

  updateContainers currently holds the Adaptation mutex while calling the
  runtime-provided updateFn. containerd's updateFn acquires its local NRI
  mutex, while CRI lifecycle paths acquire the same locks in the opposite
  order:

      lifecycle: local mutex -> Adaptation mutex
      update:    Adaptation mutex -> local mutex

  Concurrent lifecycle and plugin-initiated update requests can therefore
  deadlock.

  updateContainers does not access mutable Adaptation state. Invoke updateFn
  without holding the Adaptation mutex and leave synchronization of
  runtime-owned state to the runtime.

  Fixes https://github.com/containerd/containerd/issues/13733

@ningmingxiao

ningmingxiao commented Jul 17, 2026 •

Copy link
Copy Markdown
Contributor Author

@fuweid done thanks

@ningmingxiao
ningmingxiao force-pushed the fix_deadlock branch 3 times, most recently from a9aabad to 39a71d5 Compare July 17, 2026 00:43
updateContainers currently holds the Adaptation mutex while calling the
runtime-provided updateFn. containerd's updateFn acquires its local NRI
mutex, while CRI lifecycle paths acquire the same locks in the opposite
order:

  lifecycle: local mutex -> Adaptation mutex
  update:    Adaptation mutex -> local mutex

Concurrent lifecycle and plugin-initiated update requests can therefore
deadlock.

updateContainers does not access mutable Adaptation state. Invoke updateFn
without holding the Adaptation mutex and leave synchronization of
runtime-owned state to the runtime.

Fixes containerd/containerd#13733

Signed-off-by: ningmingxiao <[email protected]>

@fuweid fuweid 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.

LGTM

Thanks

@mikebrow mikebrow 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.

LGTM

@mikebrow
mikebrow merged commit 84180b6 into containerd:main Jul 18, 2026
16 checks passed
@ningmingxiao
ningmingxiao deleted the fix_deadlock branch July 20, 2026 01:34
Kamillaova added a commit to Kamillaova/dra-driver-cpu that referenced this pull request Sep 5, 2026
Records what this fork adds on top of upstream, the baseline it was cut
from, the cluster feature gates and container runtime it needs, and the
conventions that keep rebases mechanical.

The runtime table is the load-bearing part: unsolicited NRI
UpdateContainers is only safe on runtimes carrying containerd/nri#301,
which containerd has since nri v0.12.1 but CRI-O v1.36 does not.

Signed-off-by: Kamila Lisowa <[email protected]>
Kamillaova added a commit to Kamillaova/dra-driver-cpu that referenced this pull request Sep 5, 2026
Adds the two options ahead of the code that uses them.

assumeUnsolicitedUpdatesSafe gates every feature that pushes a container
update the runtime did not ask for. It has to be an operator assertion:
such updates deadlock runtimes whose vendored NRI predates
containerd/nri#301, and the NRI Configure handshake reports the runtime
version but not its NRI version, so the driver cannot tell. A version
table keyed on runtime name would mis-gate both backported and development
builds, so the driver asks instead of guessing.

reconcileSharedOnUnprepare defaults on but is inert without that
assertion, so enabling one option is enough to get the prompt reconcile.

values.schema.json is regenerated because the chart bundles the driver
config schema rather than only referencing it.

Signed-off-by: Kamila Lisowa <[email protected]>
Kamillaova added a commit to Kamillaova/dra-driver-cpu that referenced this pull request Sep 5, 2026
Releasing a claim returns its CPUs to the shared pool at once, but the
containers entitled to them kept the narrower cpuset until their next
CreateContainer or a driver restart. Widen them straight away instead.

The work happens on a worker goroutine rather than in the hook that
notices it, for two independent reasons: kubelet's UnprepareResourceClaims
must not block on an NRI round trip, and issuing an unsolicited update
from inside a hook is precisely how the runtime-side lock inversion behind
containerd/nri#301 deadlocks, since the runtime may be holding the lock
our update needs in order to have called us. Requests coalesce, so a burst
of releases costs one pass.

Widening is the safest possible unsolicited update: it only ever adds CPUs
a container already has a right to. A refused or partially failed update
leaves that container on a narrower-than-entitled mask, which is exactly
today's behaviour, and nothing is released on the strength of the reply -
so no exclusivity guarantee depends on it. That makes this a good first
exercise of the primitive.

The containerUpdater seam lands here rather than in its own commit,
because an interface with no caller is dead code.

The exclusive-allocation specifications asserted that shared containers
keep their narrowed cpuset after a release. That is now one of two
documented behaviours, so they assert whichever the deployed
configuration selects, read back the way the suite reads the rest of the
driver config. The reconcile option defaults to true, so a config file
that does not mention it leaves it on; the read-back models that with a
pointer, since absent-means-true is not something a plain bool can say.

Fixes kubernetes-sigs#279

Signed-off-by: Kamila Lisowa <[email protected]>
Kamillaova added a commit to Kamillaova/dra-driver-cpu that referenced this pull request Sep 5, 2026
Records what this fork adds on top of upstream, the baseline it was cut
from, the cluster feature gates and container runtime it needs, and the
conventions that keep rebases mechanical.

The runtime table is the load-bearing part: unsolicited NRI
UpdateContainers is only safe on runtimes carrying containerd/nri#301,
which containerd has since nri v0.12.1 but CRI-O v1.36 does not.

Signed-off-by: Kamila Lisowa <[email protected]>
Kamillaova added a commit to Kamillaova/dra-driver-cpu that referenced this pull request Sep 5, 2026
Adds the two options ahead of the code that uses them.

assumeUnsolicitedUpdatesSafe gates every feature that pushes a container
update the runtime did not ask for. It has to be an operator assertion:
such updates deadlock runtimes whose vendored NRI predates
containerd/nri#301, and the NRI Configure handshake reports the runtime
version but not its NRI version, so the driver cannot tell. A version
table keyed on runtime name would mis-gate both backported and development
builds, so the driver asks instead of guessing.

reconcileSharedOnUnprepare defaults on but is inert without that
assertion, so enabling one option is enough to get the prompt reconcile.

values.schema.json is regenerated because the chart bundles the driver
config schema rather than only referencing it.

Signed-off-by: Kamila Lisowa <[email protected]>
Kamillaova added a commit to Kamillaova/dra-driver-cpu that referenced this pull request Sep 5, 2026
Releasing a claim returns its CPUs to the shared pool at once, but the
containers entitled to them kept the narrower cpuset until their next
CreateContainer or a driver restart. Widen them straight away instead.

The work happens on a worker goroutine rather than in the hook that
notices it, for two independent reasons: kubelet's UnprepareResourceClaims
must not block on an NRI round trip, and issuing an unsolicited update
from inside a hook is precisely how the runtime-side lock inversion behind
containerd/nri#301 deadlocks, since the runtime may be holding the lock
our update needs in order to have called us. Requests coalesce, so a burst
of releases costs one pass.

Widening is the safest possible unsolicited update: it only ever adds CPUs
a container already has a right to. A refused or partially failed update
leaves that container on a narrower-than-entitled mask, which is exactly
today's behaviour, and nothing is released on the strength of the reply -
so no exclusivity guarantee depends on it. That makes this a good first
exercise of the primitive.

The containerUpdater seam lands here rather than in its own commit,
because an interface with no caller is dead code.

The exclusive-allocation specifications asserted that shared containers
keep their narrowed cpuset after a release. That is now one of two
documented behaviours, so they assert whichever the deployed
configuration selects, read back the way the suite reads the rest of the
driver config. The reconcile option defaults to true, so a config file
that does not mention it leaves it on; the read-back models that with a
pointer, since absent-means-true is not something a plain bool can say.

Fixes kubernetes-sigs#279

Signed-off-by: Kamila Lisowa <[email protected]>
Kamillaova added a commit to Kamillaova/dra-driver-cpu that referenced this pull request Sep 5, 2026
Adds the two options ahead of the code that uses them.

assumeUnsolicitedUpdatesSafe gates every feature that pushes a container
update the runtime did not ask for. It has to be an operator assertion:
such updates deadlock runtimes whose vendored NRI predates
containerd/nri#301, and the NRI Configure handshake reports the runtime
version but not its NRI version, so the driver cannot tell. A version
table keyed on runtime name would mis-gate both backported and development
builds, so the driver asks instead of guessing.

reconcileSharedOnUnprepare defaults on but is inert without that
assertion, so enabling one option is enough to get the prompt reconcile.

values.schema.json is regenerated because the chart bundles the driver
config schema rather than only referencing it.

Signed-off-by: Kamila Lisowa <[email protected]>
Kamillaova added a commit to Kamillaova/dra-driver-cpu that referenced this pull request Sep 8, 2026
Adds the two options ahead of the code that uses them.

assumeUnsolicitedUpdatesSafe gates every feature that pushes a container
update the runtime did not ask for. It has to be an operator assertion:
such updates deadlock runtimes whose vendored NRI predates
containerd/nri#301, and the NRI Configure handshake reports the runtime
version but not its NRI version, so the driver cannot tell. A version
table keyed on runtime name would mis-gate both backported and development
builds, so the driver asks instead of guessing.

reconcileSharedOnUnprepare defaults on but is inert without that
assertion, so enabling one option is enough to get the prompt reconcile.

values.schema.json is regenerated because the chart bundles the driver
config schema rather than only referencing it.

Signed-off-by: Kamila Lisowa <[email protected]>
Kamillaova added a commit to Kamillaova/dra-driver-cpu that referenced this pull request Sep 8, 2026
Releasing a claim returns its CPUs to the shared pool at once, but the
containers entitled to them kept the narrower cpuset until their next
CreateContainer or a driver restart. Widen them straight away instead.

The work happens on a worker goroutine rather than in the hook that
notices it, for two independent reasons: kubelet's UnprepareResourceClaims
must not block on an NRI round trip, and issuing an unsolicited update
from inside a hook is precisely how the runtime-side lock inversion behind
containerd/nri#301 deadlocks, since the runtime may be holding the lock
our update needs in order to have called us. Requests coalesce, so a burst
of releases costs one pass.

Widening is the safest possible unsolicited update: it only ever adds CPUs
a container already has a right to. A refused or partially failed update
leaves that container on a narrower-than-entitled mask, which is exactly
today's behaviour, and nothing is released on the strength of the reply -
so no exclusivity guarantee depends on it. That makes this a good first
exercise of the primitive.

The containerUpdater seam lands here rather than in its own commit,
because an interface with no caller is dead code.

The exclusive-allocation specifications asserted that shared containers
keep their narrowed cpuset after a release. That is now one of two
documented behaviours, so they assert whichever the deployed
configuration selects, read back the way the suite reads the rest of the
driver config. The reconcile option defaults to true, so a config file
that does not mention it leaves it on; the read-back models that with a
pointer, since absent-means-true is not something a plain bool can say.

Fixes kubernetes-sigs#279

Signed-off-by: Kamila Lisowa <[email protected]>
Kamillaova added a commit to Kamillaova/dra-driver-cpu that referenced this pull request Sep 12, 2026
Records what this fork adds on top of upstream, the baseline it was cut
from, the cluster feature gates and container runtime it needs, and the
conventions that keep rebases mechanical.

The runtime table is the load-bearing part: unsolicited NRI
UpdateContainers is only safe on runtimes carrying containerd/nri#301,
which containerd has since nri v0.12.1 but CRI-O v1.36 does not.

Signed-off-by: Kamila Lisowa <[email protected]>
Kamillaova added a commit to Kamillaova/dra-driver-cpu that referenced this pull request Sep 12, 2026
Adds the two options ahead of the code that uses them.

assumeUnsolicitedUpdatesSafe gates every feature that pushes a container
update the runtime did not ask for. It has to be an operator assertion:
such updates deadlock runtimes whose vendored NRI predates
containerd/nri#301, and the NRI Configure handshake reports the runtime
version but not its NRI version, so the driver cannot tell. A version
table keyed on runtime name would mis-gate both backported and development
builds, so the driver asks instead of guessing.

reconcileSharedOnUnprepare defaults on but is inert without that
assertion, so enabling one option is enough to get the prompt reconcile.

values.schema.json is regenerated because the chart bundles the driver
config schema rather than only referencing it.

Signed-off-by: Kamila Lisowa <[email protected]>
Kamillaova added a commit to Kamillaova/dra-driver-cpu that referenced this pull request Sep 12, 2026
Releasing a claim returns its CPUs to the shared pool at once, but the
containers entitled to them kept the narrower cpuset until their next
CreateContainer or a driver restart. Widen them straight away instead.

The work happens on a worker goroutine rather than in the hook that
notices it, for two independent reasons: kubelet's UnprepareResourceClaims
must not block on an NRI round trip, and issuing an unsolicited update
from inside a hook is precisely how the runtime-side lock inversion behind
containerd/nri#301 deadlocks, since the runtime may be holding the lock
our update needs in order to have called us. Requests coalesce, so a burst
of releases costs one pass.

Widening is the safest possible unsolicited update: it only ever adds CPUs
a container already has a right to. A refused or partially failed update
leaves that container on a narrower-than-entitled mask, which is exactly
today's behaviour, and nothing is released on the strength of the reply -
so no exclusivity guarantee depends on it. That makes this a good first
exercise of the primitive.

The containerUpdater seam lands here rather than in its own commit,
because an interface with no caller is dead code.

The exclusive-allocation specifications asserted that shared containers
keep their narrowed cpuset after a release. That is now one of two
documented behaviours, so they assert whichever the deployed
configuration selects, read back the way the suite reads the rest of the
driver config. The reconcile option defaults to true, so a config file
that does not mention it leaves it on; the read-back models that with a
pointer, since absent-means-true is not something a plain bool can say.

Fixes kubernetes-sigs#279

Signed-off-by: Kamila Lisowa <[email protected]>
Kamillaova added a commit to Kamillaova/dra-driver-cpu that referenced this pull request Sep 13, 2026
Records what this fork adds on top of upstream, the baseline it was cut
from, the cluster feature gates and container runtime it needs, and the
conventions that keep rebases mechanical.

The runtime table is the load-bearing part: unsolicited NRI
UpdateContainers is only safe on runtimes carrying containerd/nri#301,
which containerd has since nri v0.12.1 but CRI-O v1.36 does not.

Signed-off-by: Kamila Lisowa <[email protected]>
Kamillaova added a commit to Kamillaova/dra-driver-cpu that referenced this pull request Sep 13, 2026
Adds the two options ahead of the code that uses them.

assumeUnsolicitedUpdatesSafe gates every feature that pushes a container
update the runtime did not ask for. It has to be an operator assertion:
such updates deadlock runtimes whose vendored NRI predates
containerd/nri#301, and the NRI Configure handshake reports the runtime
version but not its NRI version, so the driver cannot tell. A version
table keyed on runtime name would mis-gate both backported and development
builds, so the driver asks instead of guessing.

reconcileSharedOnUnprepare defaults on but is inert without that
assertion, so enabling one option is enough to get the prompt reconcile.

values.schema.json is regenerated because the chart bundles the driver
config schema rather than only referencing it.

Signed-off-by: Kamila Lisowa <[email protected]>
Kamillaova added a commit to Kamillaova/dra-driver-cpu that referenced this pull request Sep 13, 2026
Releasing a claim returns its CPUs to the shared pool at once, but the
containers entitled to them kept the narrower cpuset until their next
CreateContainer or a driver restart. Widen them straight away instead.

The work happens on a worker goroutine rather than in the hook that
notices it, for two independent reasons: kubelet's UnprepareResourceClaims
must not block on an NRI round trip, and issuing an unsolicited update
from inside a hook is precisely how the runtime-side lock inversion behind
containerd/nri#301 deadlocks, since the runtime may be holding the lock
our update needs in order to have called us. Requests coalesce, so a burst
of releases costs one pass.

Widening is the safest possible unsolicited update: it only ever adds CPUs
a container already has a right to. A refused or partially failed update
leaves that container on a narrower-than-entitled mask, which is exactly
today's behaviour, and nothing is released on the strength of the reply -
so no exclusivity guarantee depends on it. That makes this a good first
exercise of the primitive.

The containerUpdater seam lands here rather than in its own commit,
because an interface with no caller is dead code.

The exclusive-allocation specifications asserted that shared containers
keep their narrowed cpuset after a release. That is now one of two
documented behaviours, so they assert whichever the deployed
configuration selects, read back the way the suite reads the rest of the
driver config. The reconcile option defaults to true, so a config file
that does not mention it leaves it on; the read-back models that with a
pointer, since absent-means-true is not something a plain bool can say.

Fixes kubernetes-sigs#279

Signed-off-by: Kamila Lisowa <[email protected]>
@samuelkarp samuelkarp added impact/changelog area/nri Node Resource Interface (NRI) and removed impact/changelog labels Sep 14, 2026
Kamillaova added a commit to Kamillaova/dra-driver-cpu that referenced this pull request Sep 18, 2026
Adds the two options ahead of the code that uses them.

assumeUnsolicitedUpdatesSafe gates every feature that pushes a container
update the runtime did not ask for. It has to be an operator assertion:
such updates deadlock runtimes whose vendored NRI predates
containerd/nri#301, and the NRI Configure handshake reports the runtime
version but not its NRI version, so the driver cannot tell. A version
table keyed on runtime name would mis-gate both backported and development
builds, so the driver asks instead of guessing.

reconcileSharedOnUnprepare defaults on but is inert without that
assertion, so enabling one option is enough to get the prompt reconcile.

values.schema.json is regenerated because the chart bundles the driver
config schema rather than only referencing it.

Signed-off-by: Kamila Lisowa <[email protected]>
Kamillaova added a commit to Kamillaova/dra-driver-cpu that referenced this pull request Sep 18, 2026
Releasing a claim returns its CPUs to the shared pool at once, but the
containers entitled to them kept the narrower cpuset until their next
CreateContainer or a driver restart. Widen them straight away instead.

The work happens on a worker goroutine rather than in the hook that
notices it, for two independent reasons: kubelet's UnprepareResourceClaims
must not block on an NRI round trip, and issuing an unsolicited update
from inside a hook is precisely how the runtime-side lock inversion behind
containerd/nri#301 deadlocks, since the runtime may be holding the lock
our update needs in order to have called us. Requests coalesce, so a burst
of releases costs one pass.

Widening is the safest possible unsolicited update: it only ever adds CPUs
a container already has a right to. A refused or partially failed update
leaves that container on a narrower-than-entitled mask, which is exactly
today's behaviour, and nothing is released on the strength of the reply -
so no exclusivity guarantee depends on it. That makes this a good first
exercise of the primitive.

The containerUpdater seam lands here rather than in its own commit,
because an interface with no caller is dead code.

The exclusive-allocation specifications asserted that shared containers
keep their narrowed cpuset after a release. That is now one of two
documented behaviours, so they assert whichever the deployed
configuration selects, read back the way the suite reads the rest of the
driver config. The reconcile option defaults to true, so a config file
that does not mention it leaves it on; the read-back models that with a
pointer, since absent-means-true is not something a plain bool can say.

Fixes kubernetes-sigs#279

Signed-off-by: Kamila Lisowa <[email protected]>
Kamillaova added a commit to Kamillaova/dra-driver-cpu that referenced this pull request Sep 18, 2026
Adds the two options ahead of the code that uses them.

assumeUnsolicitedUpdatesSafe gates every feature that pushes a container
update the runtime did not ask for. It has to be an operator assertion:
such updates deadlock runtimes whose vendored NRI predates
containerd/nri#301, and the NRI Configure handshake reports the runtime
version but not its NRI version, so the driver cannot tell. A version
table keyed on runtime name would mis-gate both backported and development
builds, so the driver asks instead of guessing.

reconcileSharedOnUnprepare defaults on but is inert without that
assertion, so enabling one option is enough to get the prompt reconcile.

values.schema.json is regenerated because the chart bundles the driver
config schema rather than only referencing it.

Signed-off-by: Kamila Lisowa <[email protected]>
Kamillaova added a commit to Kamillaova/dra-driver-cpu that referenced this pull request Sep 18, 2026
Releasing a claim returns its CPUs to the shared pool at once, but the
containers entitled to them kept the narrower cpuset until their next
CreateContainer or a driver restart. Widen them straight away instead.

The work happens on a worker goroutine rather than in the hook that
notices it, for two independent reasons: kubelet's UnprepareResourceClaims
must not block on an NRI round trip, and issuing an unsolicited update
from inside a hook is precisely how the runtime-side lock inversion behind
containerd/nri#301 deadlocks, since the runtime may be holding the lock
our update needs in order to have called us. Requests coalesce, so a burst
of releases costs one pass.

Widening is the safest possible unsolicited update: it only ever adds CPUs
a container already has a right to. A refused or partially failed update
leaves that container on a narrower-than-entitled mask, which is exactly
today's behaviour, and nothing is released on the strength of the reply -
so no exclusivity guarantee depends on it. That makes this a good first
exercise of the primitive.

The containerUpdater seam lands here rather than in its own commit,
because an interface with no caller is dead code.

The exclusive-allocation specifications asserted that shared containers
keep their narrowed cpuset after a release. That is now one of two
documented behaviours, so they assert whichever the deployed
configuration selects, read back the way the suite reads the rest of the
driver config. The reconcile option defaults to true, so a config file
that does not mention it leaves it on; the read-back models that with a
pointer, since absent-means-true is not something a plain bool can say.

Fixes kubernetes-sigs#279

Signed-off-by: Kamila Lisowa <[email protected]>
AutuSnow added a commit to AutuSnow/dra-driver-cpu that referenced this pull request Nov 17, 2026
Widening running shared containers right after a claim is unprepared
needs an unsolicited NRI UpdateContainers call. Such a call deadlocks a
runtime whose NRI adaptation predates containerd/nri#301, and the NRI
Configure handshake reports the runtime version but not the NRI version it
vendors, so the driver cannot tell a safe runtime from an unsafe one. The
operator has to opt in, per node pool, with knowledge of the runtime.

This is a tunable of the running mode, not a mode switch, so it goes in
the configuration file and not on the command line. It defaults to off,
which keeps the current behavior: shared containers pick up released CPUs
at their next CreateContainer or driver restart.

The driver reads the option in the next commit.
AutuSnow added a commit to AutuSnow/dra-driver-cpu that referenced this pull request Nov 17, 2026
Since Unprepare became the release point for a claim's CPUs, a successful
Unprepare returns them to the shared pool at once, but the shared
containers entitled to them keep the narrower cpuset until their next
CreateContainer or a driver restart. A long-running shared workload can sit
on a shrunken pool indefinitely. This follows up on that TODO (kubernetes-sigs#279).

With reconcileSharedOnUnprepare on, Unprepare hands the widening to a
worker goroutine, which computes the shared cpuset and pushes it with an
unsolicited UpdateContainers. It is not done inline for two reasons:
kubelet's UnprepareResourceClaims must not wait on an NRI round trip, and
an unsolicited update sent from inside an NRI hook is the lock inversion
containerd/nri#301 fixed on the runtime side. Requests coalesce, so a burst
of releases costs one pass.

Widening is not harmless on its own. The worker computes the cpuset under
stateMu but, per the lock's contract, cannot hold it across the runtime
call, so the runtime applies a snapshot that may be stale by the time it
lands. A deterministic test on an earlier implementation of this feature
shows the ordering: Unprepare releases 0-1, the worker snapshots 0-3, a
new claim is prepared on 0-1, its container is created and the pool shrinks
back to 2-3, then the stale 0-3 lands and both shared containers run on the
new exclusive container's CPUs. No Go-level race is involved; the runtime
applies each request in order, the request is just old.

To close that window the worker marks the in-flight update under stateMu
and prepareResourceClaim waits for the mark to clear before it reserves,
bounded by kubelet's context so a hung runtime surfaces as a Prepare error
kubelet retries, never as a reservation under a stale snapshot. The NRI
stub offers no context for UpdateContainers, so this is the only bound
available. NRI hooks never wait on the worker. A failed update is logged
and not retried: the containers keep the narrower cpuset, which is the
behavior without the option, and nothing is released on the strength of
the reply.

The tests go through the real Prepare, CreateContainer, StopContainer and
Unprepare paths with a fake runtime that applies requests in order and can
hold one, so the interleaving above is reproduced exactly rather than by
sleeping. Disabling the wait makes TestReconcilePrepareWaitsForInFlightUpdate
fail with Prepare returning under the in-flight update.
AutuSnow added a commit to AutuSnow/dra-driver-cpu that referenced this pull request Nov 17, 2026
Since Unprepare became the release point for a claim's CPUs, a successful
Unprepare returns them to the shared pool at once, but the shared
containers entitled to them keep the narrower cpuset until their next
CreateContainer or a driver restart. A long-running shared workload can sit
on a shrunken pool indefinitely. This follows up on that TODO (kubernetes-sigs#279).

With reconcileSharedOnUnprepare on, Unprepare hands the widening to a
worker goroutine, which computes the shared cpuset and pushes it with an
unsolicited UpdateContainers. It is not done inline for two reasons:
kubelet's UnprepareResourceClaims must not wait on an NRI round trip, and
an unsolicited update sent from inside an NRI hook is the lock inversion
containerd/nri#301 fixed on the runtime side. Requests coalesce, so a burst
of releases costs one pass.

Widening is not harmless on its own. The worker computes the cpuset under
stateMu but, per the lock's contract, cannot hold it across the runtime
call, so the runtime applies a snapshot that may be stale by the time it
lands. A deterministic test on an earlier implementation of this feature
shows the ordering: Unprepare releases 0-1, the worker snapshots 0-3, a
new claim is prepared on 0-1, its container is created and the pool shrinks
back to 2-3, then the stale 0-3 lands and both shared containers run on the
new exclusive container's CPUs. No Go-level race is involved; the runtime
applies each request in order, the request is just old.

To close that window the worker marks the in-flight update under stateMu
and prepareResourceClaim waits for the mark to clear before it reserves,
bounded by kubelet's context so a hung runtime surfaces as a Prepare error
kubelet retries, never as a reservation under a stale snapshot. The NRI
stub offers no context for UpdateContainers, so this is the only bound
available. NRI hooks never wait on the worker. A failed update is logged
and not retried: the containers keep the narrower cpuset, which is the
behavior without the option, and nothing is released on the strength of
the reply.

The tests go through the real Prepare, CreateContainer, StopContainer and
Unprepare paths with a fake runtime that applies requests in order and can
hold one, so the interleaving above is reproduced exactly rather than by
sleeping. Disabling the wait makes TestReconcilePrepareWaitsForInFlightUpdate
fail with Prepare returning under the in-flight update.
AutuSnow added a commit to AutuSnow/dra-driver-cpu that referenced this pull request Nov 17, 2026
Since Unprepare became the release point for a claim's CPUs, a successful
Unprepare returns them to the shared pool at once, but the shared
containers entitled to them keep the narrower cpuset until their next
CreateContainer or a driver restart. A long-running shared workload can sit
on a shrunken pool indefinitely. This follows up on that TODO (kubernetes-sigs#279).

With reconcileSharedOnUnprepare on, Unprepare hands the widening to a
worker goroutine, which computes the shared cpuset and pushes it with an
unsolicited UpdateContainers. It is not done inline for two reasons:
kubelet's UnprepareResourceClaims must not wait on an NRI round trip, and
an unsolicited update sent from inside an NRI hook is the lock inversion
containerd/nri#301 fixed on the runtime side. Requests coalesce, so a burst
of releases costs one pass.

Widening is not harmless on its own. The worker computes the cpuset under
stateMu but, per the lock's contract, cannot hold it across the runtime
call, so the runtime applies a snapshot that may be stale by the time it
lands. A deterministic test on an earlier implementation of this feature
shows the ordering: Unprepare releases 0-1, the worker snapshots 0-3, a
new claim is prepared on 0-1, its container is created and the pool shrinks
back to 2-3, then the stale 0-3 lands and both shared containers run on the
new exclusive container's CPUs. No Go-level race is involved; the runtime
applies each request in order, the request is just old.

To close that window the worker marks the in-flight update under stateMu
and prepareResourceClaim waits for the mark to clear before it reserves,
bounded by kubelet's context so a hung runtime surfaces as a Prepare error
kubelet retries, never as a reservation under a stale snapshot. The NRI
stub offers no context for UpdateContainers, so this is the only bound
available. NRI hooks never wait on the worker. A failed update is logged
and not retried: the containers keep the narrower cpuset, which is the
behavior without the option, and nothing is released on the strength of
the reply.

Waiting alone is not enough under a burst of Unprepares. After each round
the worker and the woken Prepare both go for stateMu, and the worker tends
to win, so a Prepare sat through 3-14 rounds in a test that keeps the
trigger topped up. Waiting Prepares now register in prepareTurn and the
worker holds its next round until they have the lock, which brings the
wait back to the one round that was in flight.

The tests go through the real Prepare, CreateContainer, StopContainer and
Unprepare paths with a fake runtime that applies requests in order and can
hold one, so the interleaving above is reproduced exactly rather than by
sleeping. Disabling the wait makes TestReconcilePrepareWaitsForInFlightUpdate
fail with Prepare returning under the in-flight update.
AutuSnow added a commit to AutuSnow/dra-driver-cpu that referenced this pull request Nov 17, 2026
Since Unprepare became the release point for a claim's CPUs, a successful
Unprepare returns them to the shared pool at once, but the shared
containers entitled to them keep the narrower cpuset until their next
CreateContainer or a driver restart. A long-running shared workload can sit
on a shrunken pool indefinitely. This follows up on that TODO (kubernetes-sigs#279).

With reconcileSharedOnUnprepare on, Unprepare hands the widening to a
worker goroutine, which computes the shared cpuset and pushes it with an
unsolicited UpdateContainers. It is not done inline for two reasons:
kubelet's UnprepareResourceClaims must not wait on an NRI round trip, and
an unsolicited update sent from inside an NRI hook is the lock inversion
containerd/nri#301 fixed on the runtime side. Requests coalesce, so a burst
of releases costs one pass.

Widening is not harmless on its own. The worker computes the cpuset under
stateMu but, per the lock's contract, cannot hold it across the runtime
call, so the runtime applies a snapshot that may be stale by the time it
lands. A deterministic test on an earlier implementation of this feature
shows the ordering: Unprepare releases 0-1, the worker snapshots 0-3, a
new claim is prepared on 0-1, its container is created and the pool shrinks
back to 2-3, then the stale 0-3 lands and both shared containers run on the
new exclusive container's CPUs. No Go-level race is involved; the runtime
applies each request in order, the request is just old.

To close that window the worker marks the in-flight update under stateMu
and prepareResourceClaim waits for the mark to clear before it reserves,
bounded by kubelet's context so a hung runtime surfaces as a Prepare error
kubelet retries, never as a reservation under a stale snapshot. The NRI
stub offers no context for UpdateContainers, so this is the only bound
available. NRI hooks never wait on the worker. A failed update is logged
and not retried: the containers keep the narrower cpuset, which is the
behavior without the option, and nothing is released on the strength of
the reply.

Waiting alone is not enough under a burst of Unprepares. After each round
the worker and the woken Prepare both go for stateMu, and the worker tends
to win, so a Prepare sat through 3-14 rounds in a test that keeps the
trigger topped up. Waiting Prepares now register in prepareTurn and the
worker holds its next round until they have the lock, which brings the
wait back to the one round that was in flight.

The tests go through the real Prepare, CreateContainer, StopContainer and
Unprepare paths with a fake runtime that applies requests in order and can
hold one, so the interleaving above is reproduced exactly rather than by
sleeping. Disabling the wait makes TestReconcilePrepareWaitsForInFlightUpdate
fail with Prepare returning under the in-flight update.

The e2e release specs read the option back from the driver ConfigMap and
assert whichever behavior it selects: widened onto the whole pool, or kept
on the last cpuset. DRACPU_E2E_RECONCILE_SHARED_ON_UNPREPARE makes
ci-kind-setup deploy with the option on. The stock kindest/node:v1.37.0
image ships containerd v2.3.4 whose NRI adaptation still takes the lock
nri#301 removed, so CI keeps it off; the on-path was exercised by hand on
such a cluster: two running shared pods went from 3-9 back to 1-9 within
5s of the exclusive pod being deleted, with zero restarts.
AutuSnow added a commit to AutuSnow/dra-driver-cpu that referenced this pull request Nov 19, 2026
Implements auto-detection for reconcileSharedOnUnprepare as suggested in kubernetes-sigs#395.

Changes the field from bool to *bool (tri-state):
- nil (default): auto-detect based on runtime version
- true: force enable
- false: force disable

## Implementation

- Runtime detection: NRI Configure callback captures runtime name/version
- Decision logic: Synchronize enables on containerd 2.4.0+, CRI-O 1.37.0+
- Conservative fallback: Unknown runtimes (k3s, microk8s, forks) stay disabled
- Logging: Decision logged once at startup with reasoning

## Safe Runtimes

| Runtime | Min Version | Reason |
|---------|-------------|--------|
| containerd | 2.4.0+ | Includes NRI v0.12.3+ (containerd/nri#301) |
| CRI-O | 1.37.0+ | Vendors NRI v0.12.3+ |

## Breaking Change

BREAKING (v0.5.0): Omitting reconcileSharedOnUnprepare now means auto-detect
instead of disabled. To preserve old behavior, explicitly set:

  driverConfig:
    reconcileSharedOnUnprepare: false

## Testing

- 28 unit tests for version parsing (all combinations)
- 3 integration tests for auto-detect flow, including worker responsiveness
- All existing reconcile tests pass

## Code Review Fixes

Addresses findings from manual review and CodeRabbitAI:
1. Save lifecycleCtx and launch worker when auto-detect enables
2. Document breaking change and migration path
3. Add stateMu lock in Configure to prevent race
4. Handle empty runtimeName with clear log message
5. Add integration tests for auto-detect path
6. Simplify dumpConfig ReconcileSharedOnUnprepare handling
7. Add comment explaining goroutine start safety under lock
8. Enhance test to verify worker actually responds to triggers
AutuSnow added a commit to AutuSnow/dra-driver-cpu that referenced this pull request Nov 19, 2026
Implements auto-detection for reconcileSharedOnUnprepare as suggested in kubernetes-sigs#395.

Changes the field from bool to *bool (tri-state):
- nil (default): auto-detect based on runtime version
- true: force enable
- false: force disable

## Implementation

- Runtime detection: NRI Configure callback captures runtime name/version
- Decision logic: Synchronize enables on containerd 2.4.0+, CRI-O 1.37.0+
- Conservative fallback: Unknown runtimes (k3s, microk8s, forks) stay disabled
- Logging: Decision logged once at startup with reasoning

## Safe Runtimes

| Runtime | Min Version | Reason |
|---------|-------------|--------|
| containerd | 2.4.0+ | Includes NRI v0.12.3+ (containerd/nri#301) |
| CRI-O | 1.37.0+ | Vendors NRI v0.12.3+ |

## Breaking Change

BREAKING (v0.5.0): Omitting reconcileSharedOnUnprepare now means auto-detect
instead of disabled. To preserve old behavior, explicitly set:

  driverConfig:
    reconcileSharedOnUnprepare: false

## Testing

- 28 unit tests for version parsing (all combinations)
- 3 integration tests for auto-detect flow, including worker responsiveness
- All existing reconcile tests pass

## Code Review Fixes

Addresses findings from manual review and CodeRabbitAI:
1. Save lifecycleCtx and launch worker when auto-detect enables
2. Document breaking change and migration path
3. Add stateMu lock in Configure to prevent race
4. Handle empty runtimeName with clear log message
5. Add integration tests for auto-detect path
6. Simplify dumpConfig ReconcileSharedOnUnprepare handling
7. Add comment explaining goroutine start safety under lock
8. Enhance test to verify worker actually responds to triggers
AutuSnow added a commit to AutuSnow/dra-driver-cpu that referenced this pull request Nov 19, 2026
Implements auto-detection for reconcileSharedOnUnprepare as suggested in kubernetes-sigs#395.

Changes the field from bool to *bool (tri-state):
- nil (default): auto-detect based on runtime version
- true: force enable
- false: force disable

## Implementation

- Runtime detection: NRI Configure callback captures runtime name/version
- Decision logic: Synchronize enables on containerd 2.4.0+, CRI-O 1.37.0+
- Conservative fallback: Unknown runtimes (k3s, microk8s, forks) stay disabled
- Logging: Decision logged once at startup with reasoning

## Safe Runtimes

| Runtime | Min Version | Reason |
|---------|-------------|--------|
| containerd | 2.4.0+ | Includes NRI v0.12.3+ (containerd/nri#301) |
| CRI-O | 1.37.0+ | Vendors NRI v0.12.3+ |

## Breaking Change

BREAKING (v0.5.0): Omitting reconcileSharedOnUnprepare now means auto-detect
instead of disabled. To preserve old behavior, explicitly set:

  driverConfig:
    reconcileSharedOnUnprepare: false

## Testing

- 28 unit tests for version parsing (all combinations)
- 3 integration tests for auto-detect flow, including worker responsiveness
- All existing reconcile tests pass

## Code Review Fixes

Addresses findings from manual review and CodeRabbitAI:
1. Save lifecycleCtx and launch worker when auto-detect enables
2. Document breaking change and migration path
3. Add stateMu lock in Configure to prevent race
4. Handle empty runtimeName with clear log message
5. Add integration tests for auto-detect path
6. Simplify dumpConfig ReconcileSharedOnUnprepare handling
7. Add comment explaining goroutine start safety under lock
8. Enhance test to verify worker actually responds to triggers
AutuSnow added a commit to AutuSnow/dra-driver-cpu that referenced this pull request Nov 19, 2026
Implements auto-detection for reconcileSharedOnUnprepare as suggested in kubernetes-sigs#395.

Changes the field from bool to *bool (tri-state):
- nil (default): auto-detect based on runtime version
- true: force enable
- false: force disable

## Implementation

- Runtime detection: NRI Configure callback captures runtime name/version
- Decision logic: Synchronize enables on containerd 2.4.0+, CRI-O 1.37.0+
- Conservative fallback: Unknown runtimes (k3s, microk8s, forks) stay disabled
- Logging: Decision logged once at startup with reasoning

## Safe Runtimes

| Runtime | Min Version | Reason |
|---------|-------------|--------|
| containerd | 2.4.0+ | Includes NRI v0.12.3+ (containerd/nri#301) |
| CRI-O | 1.37.0+ | Vendors NRI v0.12.3+ |

## Breaking Change

BREAKING (v0.5.0): Omitting reconcileSharedOnUnprepare now means auto-detect
instead of disabled. To preserve old behavior, explicitly set:

  driverConfig:
    reconcileSharedOnUnprepare: false

## Testing

- 28 unit tests for version parsing (all combinations)
- 3 integration tests for auto-detect flow, including worker responsiveness
- All existing reconcile tests pass

## Code Review Fixes

Addresses findings from manual review and CodeRabbitAI:
1. Save lifecycleCtx and launch worker when auto-detect enables
2. Document breaking change and migration path
3. Add stateMu lock in Configure to prevent race
4. Handle empty runtimeName with clear log message
5. Add integration tests for auto-detect path
6. Simplify dumpConfig ReconcileSharedOnUnprepare handling
7. Add comment explaining goroutine start safety under lock
8. Enhance test to verify worker actually responds to triggers
AutuSnow added a commit to AutuSnow/dra-driver-cpu that referenced this pull request Nov 20, 2026
When a claim is unprepared its CPUs go back to the shared pool, but
already-running shared containers keep the narrower cpuset until their
next container creation. This option makes the driver widen them at once.

Sending that update needs a runtime whose NRI adaptation includes
containerd/nri#301: the handshake reports the runtime version but not the
NRI version it vendors, so the driver cannot check it and the operator
opts in with `true`. Defaults to false.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/nri Node Resource Interface (NRI)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

5 participants