Skip to content
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

podman exec: correctly support detaching #25083

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

Luap99
Copy link
Member

@Luap99 Luap99 commented Jan 22, 2025

podman exec support detaching early via the detach key sequence. In that
case the podman process should exit successfully but the container exec
process keeps running.

Given that I could not find any existing test for the detach key
functionality not even for exec I added some. This seems to reveal more
issues with on podman-remote, podman-remote run detach was broken which
I fixed here as well but for podman-remote exec something bigger is
needed. While I thought I fixed most problems there there was a strange
race condition which caused the process to just hang.

Thus I skipped the remote exec test for now and filled #25089 to track
that.

Fixes #24895

Does this PR introduce a user-facing change?

Fix the detach sequence for podman exec so it cannot correctly detach without errors.

@openshift-ci openshift-ci bot added release-note do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. labels Jan 22, 2025
Copy link
Contributor

openshift-ci bot commented Jan 22, 2025

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: Luap99

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@openshift-ci openshift-ci bot added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label Jan 22, 2025
@Luap99
Copy link
Member Author

Luap99 commented Jan 22, 2025

I still need to fix the podman-remote parts

@github-actions github-actions bot added the kind/api-change Change to remote API; merits scrutiny label Jan 22, 2025
@Luap99 Luap99 force-pushed the exec-detach branch 2 times, most recently from ba4cf6b to f8a73cc Compare January 22, 2025 17:24
@Luap99 Luap99 marked this pull request as ready for review January 22, 2025 17:24
@openshift-ci openshift-ci bot removed the do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. label Jan 22, 2025
@Luap99 Luap99 force-pushed the exec-detach branch 2 times, most recently from d0ab25e to 61fd7a4 Compare January 22, 2025 18:19
Luap99 added a commit to Luap99/common that referenced this pull request Jan 23, 2025
The code only could handle the detach sequence when it read one byte at
a time. This obviously is not correct and lead to some issues for my
automated test in my podman PR[1] where I added some automated tests
for detaching and the read part is really undefined and depends on the
input side/kernel scheduling on how much we read at once.

This is large rework to make the code check for the key sequence across
the entire buffer. That is of course more work but it needs to happen
for this to work correctly.

I guess the only reason why this was never noticed is because normally
user detach manually by typing and not in an automated way which is much
slower and thus likely reads the bytes one by one.

I added new test to actually confirm the behavior. And to ensure this
works with various read sizes I made it a fuzz test. I had this running
for a while and did not spot any issues there. The old code fails
already on the simple test cases.

[1] containers/podman#25083

Signed-off-by: Paul Holzinger <[email protected]>
Luap99 added a commit to Luap99/common that referenced this pull request Jan 23, 2025
The code only could handle the detach sequence when it read one byte at
a time. This obviously is not correct and lead to some issues for my
automated test in my podman PR[1] where I added some automated tests
for detaching and the read part is really undefined and depends on the
input side/kernel scheduling on how much we read at once.

This is large rework to make the code check for the key sequence across
the entire buffer. That is of course more work but it needs to happen
for this to work correctly.

I guess the only reason why this was never noticed is because normally
user detach manually by typing and not in an automated way which is much
slower and thus likely reads the bytes one by one.

I added new test to actually confirm the behavior. And to ensure this
works with various read sizes I made it a fuzz test. I had this running
for a while and did not spot any issues there. The old code fails
already on the simple test cases.

[1] containers/podman#25083

Signed-off-by: Paul Holzinger <[email protected]>
podman exec support detaching early via the detach key sequence. In that
case the podman process should exit successfully but the container exec
process keeps running.

Given that I could not find any existing test for the detach key
functionality not even for exec I added some. This seems to reveal more
issues with on podman-remote, podman-remote run detach was broken which
I fixed here as well but for podman-remote exec something bigger is
needed. While I thought I fixed most problems there there was a strange
race condition which caused the process to just hang.

Thus I skipped the remote exec test for now and filled containers#25089 to track
that.

Fixes containers#24895

Signed-off-by: Paul Holzinger <[email protected]>
Now the the detach byte sequence can be anywhere in the stream this test
would flake as the randmon bytes might match the default sequence
accidentally.
Simply disable the sequence.

Signed-off-by: Paul Holzinger <[email protected]>
When we read the stdin to forward it into the container it doesn't
really make sense to have it buffered. In fact our detach.Copy() is
already reading in large buffers so we do not gain anything by it.

In fact this is causing a bug with the new detach.Copy() code from
c/common. The Copy() function now calls into the std io.Copy() function
if no detach keys are given. That function tries to use the WriterTo()
method if available. bufio.Buffer has that and this function causes a
empty write to the conmon attach socket[1]. I think this is a bug in the
standard library.
Now because the conmon socket where it writes to is a packet based
socket and not a stream one the reading side (conmon) read an empty
packet. This makes it think we hit EOF, not sure if this is intentional
over there either.

To avoid these bugs stop using bufio here. Now we keep using it in the
bindings as the API there is stable so I cannot just change the type
there and on the remote side it should not cause any issues as we do not
write to conmon there.

Now I did saw commit 1df4dba ("Switch to bufio Reader for exec streams")
but I don't quite follow the reason there. The code has changed a fair
bit since then so I don't think the races should still be there. If they
are the CI tests should show them I presume.

[1] https://cs.opensource.google/go/go/+/refs/tags/go1.23.5:src/bufio/bufio.go;l=518-521

Signed-off-by: Paul Holzinger <[email protected]>
@@ -231,3 +231,5 @@ require (
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 // indirect
tags.cncf.io/container-device-interface/specs-go v0.8.0 // indirect
)

replace github.com/containers/common => github.com/Luap99/common v0.20.3-0.20250123195450-f0fd031b07f8
Copy link
Member

Choose a reason for hiding this comment

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

Should this be marked WIP until this merges?

@Luap99 Luap99 marked this pull request as draft January 28, 2025 16:25
@openshift-ci openshift-ci bot added the do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. label Jan 28, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved Indicates a PR has been approved by an approver from all required OWNERS files. do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. kind/api-change Change to remote API; merits scrutiny release-note
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Detaching from an attached container results in an error
2 participants