Repository navigation
worker/runtime: wrap containers from NewContainer and Containers with request timeout - #9509
Conversation
… request timeout Signed-off-by: Kump3r <[email protected]>
Steps to reproduce
Pre-fix: hangs indefinitely despite In production the timing was driven by the TTY handshake race under load rather than an artificial sleep — but the effect was identical. Commands used
BEFORE FIX: continuously ongoing check not timeouted after 10s., until eventualy we see
|
|
I was able to test it locally. We had a quick sync with @Kump3r on how to test it manually and we update steps a little bit:
Additionally I think I came up with an integration test that would test behaviour in // TestNewContainerEnforcesTimeoutOnTask is a regression test verifying that
// containers returned by client.NewContainer enforce the requestTimeout on
// follow-on operations (Task, NewTask, Spec).
// How this test works:
// 1. Create a libcontainerd client with a 1s requestTimeout
// 2. Create a container directly via client.NewContainer with a minimal
// OCI runtime spec (no init binary or running task needed)
// 3. Freeze containerd with SIGSTOP (making all gRPC calls hang)
// 4. Call Task() on the container from NewContainer with context.Background()
// (no deadline from the caller — the wrapper must add it)
// 5. Expect a "deadline exceeded" error within ~1s
// 6. Also verify GetContainer returns a container that behaves identically
// 7. Unfreeze containerd and verify recovery
//
// Step 4 will blocks indefinitely and the test times out without the fix
func (s *IntegrationSuite) TestNewContainerEnforcesTimeoutOnTask() {
requestTimeout := 1 * time.Second
client := libcontainerd.New(
s.containerdSocket(),
"test-timeout-enforcement",
requestTimeout,
)
err := client.Init()
s.NoError(err)
defer client.Stop()
handle := uuid()
minimalSpec := &specs.Spec{
Version: specs.Version,
Process: &specs.Process{
Args: []string{"/bin/sh"},
},
Root: &specs.Root{
Path: s.rootfs,
},
}
contViaNew, err := client.NewContainer(
context.Background(), handle, map[string]string{}, minimalSpec,
)
s.NoError(err, "NewContainer should succeed")
defer func() {
_ = s.containerdProcess.Process.Signal(syscall.SIGCONT)
contViaNew.Delete(context.Background())
}()
_, err = contViaNew.Task(context.Background(), nil)
s.Error(err, "Task should return 'not found' error (no task started)")
contViaGet, err := client.GetContainer(context.Background(), handle)
s.NoError(err, "GetContainer should succeed while containerd is responsive")
_, err = contViaGet.Task(context.Background(), nil)
s.Error(err, "Task via GetContainer should also return 'not found'")
s.NoError(s.containerdProcess.Process.Signal(syscall.SIGSTOP))
start := time.Now()
_, err = contViaNew.Task(context.Background(), nil)
elapsed := time.Since(start)
s.Error(err, "Task must fail when containerd is frozen")
s.Contains(err.Error(), "deadline exceeded",
"Expected 'context deadline exceeded', got: %v", err)
s.Less(elapsed.Seconds(), 3.0,
"Task should timeout in ~1s (requestTimeout), but took %v", elapsed)
s.NoError(s.containerdProcess.Process.Signal(syscall.SIGCONT))
_, err = contViaNew.Task(context.Background(), nil)
s.Error(err)
s.NotContains(err.Error(), "deadline exceeded",
"After unfreeze, Task should not timeout")
} |
IvanChalukov
left a comment
There was a problem hiding this comment.
Great job! I've tested it locally as I already mention it comment, as well as through integration test.
… timeout is propagate correctly Signed-off-by: Kump3r <[email protected]>
Signed-off-by: Kump3r <[email protected]>
|
Just read over everything, this is great! Very simple fix, but it's all the research and debugging beforehand that shows the true effort of finding what feels like a "needle in a haystack" type of bug. Thanks for reviewing and testing Ivan! Glad you were able to write a test for this too 🙏 |

Description
NewContainerandContainerswere returning rawcontainerd.Containervalues,bypassing the
requestTimeoutwrapper applied inGetContainer. This meantdownstream calls to
Spec,Task, andNewTaskduringcontainer.Runcould blockindefinitely with a background context, even when --containerd-request-timeout
was configured.
This caused intermittent hangs where
gdn-initwas alive but the user processwas never exec'd - a userspace handshake deadlock that would only resolve when
the outer step timeout was reached, leaking a lidar check in the process.
closes:
#9398
#9511
What I changed?
client.gosoNewContainernow returns the timeout-wrapped container type instead of the raw containerd container.client.gosoContainersalso returns timeout-wrapped container objects for every listed container.Why this targets the hang?
containerdcalls during early process startup.Spec,Task, andNewTaskwith effectively unbounded contexts.Validation
Notes to reviewer
trade-off to be aware of: When unit tests use
FakeClient.NewContainerReturns(fakeContainer, nil), the backend gets back a rawFakeContainerwithout the timeout wrapper. Socontainer_test.goandbackend_test.godon't exercise the timeout wrapping. That's intentional for unit tests as the interfaces (Client-FakeClientandContainer-FakeContainer) remain unchanged.The wrapping is exercised in the integration tests under
integration_test.gowhich uses the reallibcontainerd.New(...)with a realcontainerdclient that now returns wrapped containers from bothNewContainerandContainers.Steps to reproduce
Still not actually 100% sure as it is quite intermittent, but I assume heavier load, IO on the machine might cause the leak
Possible verification:
As explained in the discussion on a problematic web, we see:
So I expect this works if eventually we see that the STARTED − (SUCCESS + ERROR) is always 0