Skip to content

feat: add --no-stdin flag to container attach #4176

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

subashkotha
Copy link
Contributor

@subashkotha subashkotha commented Apr 30, 2025

Summary

This PR adds support for a new flag in the nerdctl container attach command:

--no-stdin: disables attaching stdin to the container

Notes

We are not adding --sig-proxy in this PR due to a limitation in nerdctl: attach only works for containers started with -it ref - #2108
When -it is used, signals like Ctrl+C (SIGINT) are sent directly to the container process by the TTY, bypassing nerdctl.
As a result, --sig-proxy=false has no effect in such cases and cannot be used/tested reliably under current constraints.

Testing

  • Added an integration test to validate that input is not sent to the container when --no-stdin is used.
  • Verified behavior across re-attached containers.


testCase.Setup = func(data test.Data, helpers test.Helpers) {
cmd := helpers.Command("run", "-it", "--detach-keys=ctrl-p,ctrl-q", "--name", data.Identifier(),
testutil.CommonImage, "sh", "-c", "sleep 1; echo 'after cat'; sleep 1")
Copy link
Member

Choose a reason for hiding this comment

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

Needs -e ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We're using detach-keys here specifically to immediately detachment after attach. hence there is no need for passing in env variables.

@AkihiroSuda
Copy link
Member

Please squash the commits

@AkihiroSuda AkihiroSuda added this to the v2.1.0 (tentative) milestone Apr 30, 2025
@subashkotha subashkotha force-pushed the add_attach_options branch from cae7965 to bfb53ae Compare May 2, 2025 18:58
@subashkotha subashkotha changed the title feat: add --no-stdin and --sig-proxy flags to container attach feat: add --no-stdin flag to container attach May 2, 2025
@subashkotha subashkotha force-pushed the add_attach_options branch 2 times, most recently from 9e09568 to a0d226e Compare May 5, 2025 19:23
@subashkotha subashkotha closed this May 6, 2025
@subashkotha subashkotha reopened this May 6, 2025
@subashkotha subashkotha closed this May 6, 2025
@subashkotha subashkotha reopened this May 6, 2025
@Shubhranshu153
Copy link
Contributor

Flaky Test PR's:
#4196
#4198
#4199

@subashkotha
Copy link
Contributor Author

Hi @apostasie , @AkihiroSuda

I have updated the PR addressing comments, can you please take a look.

@apostasie
Copy link
Contributor

Thanks a lot @subashkotha

Issues on the CI are AFAIK:

I am bit concerned about rootless old getting to timeout at 45 mins+ (clearly getting stuck in container tests).

Suggestion would be that we first merge a raft of fixes for the CI (pending), and then you could rebase on top of main to get a cleaner run.

Does that work for you?

@subashkotha
Copy link
Contributor Author

Sure, @apostasie
I'll rebase after fixes for CI are merged. Can you approve the PR if there are no concerns, thanks!

@apostasie
Copy link
Contributor

apostasie commented May 6, 2025

@subashkotha thanks.
Code looks good to me, so, approved, but I would like to see a clean CI run with no timeout before we merge.
Also note that I am not a maintainer: approval from me is just advisory, and you will need a maintainer approval for merge.

@AkihiroSuda AkihiroSuda closed this May 7, 2025
@AkihiroSuda AkihiroSuda reopened this May 7, 2025
@AkihiroSuda
Copy link
Member

Could you try rebasing?

@apostasie
Copy link
Contributor

This does not feel right. EL8 does not get past the new test.

cmd := helpers.Command("attach", "--no-stdin", data.Identifier())
cmd.WithPseudoTTY()
cmd.WithFeeder(func() io.Reader {
for {
Copy link
Contributor

Choose a reason for hiding this comment

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

Does the loop serve a purpose here?

If I am reading this correctly, we attach to the container, and we will not return until the sleep 5 has lapsed.
Once it has elapsed, we are guaranteed to have "ready" in the log, right?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, I tried removing the loop, but the test failed because it couldn't reliably assert "ready" in the logs (example failure). The loop ensures we wait until the container is actually ready before feeding input — without it, the test becomes racy.

@subashkotha subashkotha force-pushed the add_attach_options branch 2 times, most recently from 518708f to df77a10 Compare May 7, 2025 16:42
@apostasie
Copy link
Contributor

Thanks for keeping up with this @subashkotha
Appreciated.

Docker Hub is now flipping a 429 on us (<- this is rate limiting from Docker Hub) - unfortunately there is nothing to do about that but wait for it to chill (@AkihiroSuda so tired of this - need to act)

Let's give it a minute then we will re-run.

@subashkotha subashkotha closed this May 7, 2025
@subashkotha subashkotha reopened this May 7, 2025
@subashkotha subashkotha force-pushed the add_attach_options branch from df77a10 to ae61a20 Compare May 7, 2025 22:00
@subashkotha subashkotha closed this May 8, 2025
@subashkotha subashkotha reopened this May 8, 2025
@AkihiroSuda
Copy link
Member

Could you rebase to see if passes the CI?

@subashkotha subashkotha force-pushed the add_attach_options branch from ae61a20 to a7ef3d6 Compare May 8, 2025 23:38
@subashkotha subashkotha closed this May 8, 2025
@subashkotha subashkotha reopened this May 8, 2025
@AkihiroSuda
Copy link
Member

Seems hanging up on almalinux-8 (cgroup v1)

@AkihiroSuda AkihiroSuda modified the milestones: v2.1.0, v2.1.1 May 9, 2025
@apostasie
Copy link
Contributor

apostasie commented May 9, 2025

Seems hanging up on almalinux-8 (cgroup v1)

Reading tea-leaves: we get stuck in the Feed loop waiting for the logs to show ready (which never happens for whatever reason <- this is the culprit).

While tigron does drop the main command after 3 minutes, we might still be waiting (too politely) on the ioGroup (which is dead looping):
https://github.com/containerd/nerdctl/blob/main/mod/tigron/internal/com/command.go#L327

Maybe I could add a reasonable timeout of the ioGroup Wait to deal with these conditions.

@subashkotha so, beside the problem in tigron ^ that prevents this from timeouting (I will deal with that), could you add a counter in your loop waiting for ready so that we stop iterating after - let's say - 500 iterations (or 50 iterations if you increase the sleep to 1 second, which I would recommend to reduce pressure on the runtime).

UPDATE: Tigron issue tracked in #4231

Signed-off-by: Subash Kotha <subash.kotha2@gmail.com>
@subashkotha subashkotha force-pushed the add_attach_options branch from a7ef3d6 to 13e738c Compare May 9, 2025 18:34
@apostasie
Copy link
Contributor

apostasie commented May 9, 2025

Latest push:

Confirmed that on EL8, you never see ready no matter how long you wait.
Maybe this is a logs bug.
Reading this again.

@apostasie
Copy link
Contributor

@fahedouch
Here is the scenario:

nerdctl run -ti --name foo image "sh" "-c" `echo ready; sleep 5`

Then detach (ctrl+p ctrl+q).

Then we nerdctl logs foo, but in some cases we never see ready in the logs after an extended period of time.

Do you have an intuition on this? Could it be a bug in logging with detach?

testutil.CommonImage, "sh", "-c", `echo ready; sleep 5`)
cmd.WithPseudoTTY()
time.Sleep(1 * time.Second)
cmd.Feed(bytes.NewReader([]byte{16, 17})) // Ctrl-p, Ctrl-q to detach (https://en.wikipedia.org/wiki/C0_and_C1_control_codes)
Copy link
Contributor

@apostasie apostasie May 9, 2025

Choose a reason for hiding this comment

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

@subashkotha let's test my hypothesis that there is a bug in logging.

So, instead of sending cmd.Feed(bytes.NewReader([]byte{16, 17})) after one second, instead do your logs / wait on ready BEFORE sending the escape sequence.

eg:

cmd.Feeder(func()io.Reader{

for {
				out := helpers.Capture("logs", data.Identifier())
				if strings.Contains(out, "ready") {
					break
				}
}

return bytes.NewReader([]byte{16, 17})

}
)

Copy link
Contributor

Choose a reason for hiding this comment

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

Pseudo-code ^ just add back the sleep and make this proper - thanks a lot! :-)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks, let me try this out.

@apostasie
Copy link
Contributor

Looks indeed like a log bug: #4233

@subashkotha suggested change above should allow you to work-around it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants