Skip to content

Conversation

@shiavm006
Copy link
Contributor

Problem

The remote client completely ignores the --detach-keys option for podman exec sessions. Users cannot detach from exec sessions using custom key sequences.

The ExecStartAndAttach() function in pkg/bindings/containers/attach.go hardcodes empty detach keys:

// Line 461 - BEFORE
_, err := detach.Copy(socket, options.InputStream, []byte{})
//                                                   ^^^^^^^^ HARDCODED!

// Line 482 - BEFORE  
_, err := detach.Copy(options.GetOutputStream(), socket, []byte{})
//                                                        ^^^^^^^^ HARDCODED!

This fix complements the recent fix for empty --detach-keys="" in PR #27425 , maintaining consistency across attach, run, start, and exec commands.

    None 

@openshift-ci
Copy link
Contributor

openshift-ci bot commented Nov 5, 2025

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: shiavm006
Once this PR has been reviewed and has the lgtm label, please assign nalind for approval. For more information see the Code Review Process.

The full list of commands accepted by this bot can be found 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

Comment on lines 322 to 326
# Test with custom detach keys - should not error
# (actual detach behavior is hard to test in non-interactive mode,
# but we can verify the option is accepted)
run_podman exec --detach-keys="ctrl-x,ctrl-x" $cid echo "detach-keys-test"
is "$output" "detach-keys-test" "exec with custom detach-keys works"
Copy link
Member

Choose a reason for hiding this comment

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

This doesn't test anything really, you must ensure you actually test the detach sequence. These test would pass with or without this patch.

It is has been a while since I looked at this but there are some rather nasty bugs around detaching logic, #25083 may be a starting point maybe with this change you can get it to work.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I looked into testing actual detach behavior but it requires PTY/interactive terminal setup which is tricky in automated tests.

should i ?
Keep a simpler test that just verifies no error (but document it doesn't test behavior)
I'll also look at #25083 to see if there's a pattern I can follow.

The remote client completely ignores the --detach-keys option for exec
sessions. The ExecStartAndAttach() function in pkg/bindings/containers/attach.go
hardcodes empty detach keys ([]byte{}) instead of using the detach keys
stored in the exec session configuration.

This prevents users from detaching from exec sessions using custom key
sequences, making the --detach-keys option ineffective.

This commit fixes the issue by:
1. Extracting detach keys from the exec session after inspection
2. Parsing them using term.ToBytes() (unless empty string)
3. Using the parsed keys in both detach.Copy() calls

The fix maintains backward compatibility:
- Exec sessions without detach keys -> empty string -> []byte{} (unchanged)
- Empty detach keys (--detach-keys="") -> empty string -> []byte{} (correct)
- Custom detach keys -> parsed and used (new functionality)

Related to PR containers#25083 and issue containers#25089 which track comprehensive detach
key functionality. A proper interactive test for detach behavior exists
in test/system/450-interactive.bats but is currently skipped for remote
exec pending additional infrastructure work.

Signed-off-by: shiavm006 <shivammittal42006@gmail.com>
@shiavm006 shiavm006 force-pushed the fix-remote-exec-detach-keys-ignored branch from 7b4e29b to 180570a Compare November 6, 2025 05:32
@shiavm006
Copy link
Contributor Author

@Luap99
PR #25083 which has the proper interactive test in test/system/450-interactive.bats. I see that test is currently skipped for remote exec (issue #25089) pending additional infrastructure work.
Remove the inadequate test and Focus solely on the code fix ,Reference your existing test infrastructure in the commit message

@Luap99
Copy link
Member

Luap99 commented Nov 6, 2025

Currently we seems to send the detach keys to the server and handles it there. If I try podman-remote on main I get

$ bin/podman-remote exec -it --detach-keys=a,b magical_mestorf sh
/home/podman # Error: container 48bebf64fb8f79cc5b834c3fd75592560e131b381de98962556c30a11c70d618 exec session 720a9282e1db5fb3d3f2695897737ce41072c7414ae1805742712a60f5199ded is still running, cannot remove: exec session state improper

So basically the issue is that the remote client didn't know the server detached and tried to remove the exec session so it still make sense to move the detach keys to the client side so we can catch and handle the detach error here. And then you need to make sure the remote client handles the detach error and

With your patch here it actually worse

$ bin/podman-remote exec -it --detach-keys=a,b magical_mestorf sh
/home/podman # ERRO[0001] Failed to write input to service: detached from container

<<< it just hangs here now forever until you kill the exec session or remote service

So we definitely need the proper tests and code to make it actually work, just by adding the keys you make it worse unfortunately.

The reason the common change in #25083 is needed is because currently the detach check is broken in that it only works when bytes are read one by one which may not be always the case but it may not be strictly needed for this fix.

Overall the exec REST API interface is quite awkward but I don't see how we could chnage it much so we need to make it work with what we got.

@shiavm006
Copy link
Contributor Author

Currently we seems to send the detach keys to the server and handles it there. If I try podman-remote on main I get

$ bin/podman-remote exec -it --detach-keys=a,b magical_mestorf sh
/home/podman # Error: container 48bebf64fb8f79cc5b834c3fd75592560e131b381de98962556c30a11c70d618 exec session 720a9282e1db5fb3d3f2695897737ce41072c7414ae1805742712a60f5199ded is still running, cannot remove: exec session state improper

So basically the issue is that the remote client didn't know the server detached and tried to remove the exec session so it still make sense to move the detach keys to the client side so we can catch and handle the detach error here. And then you need to make sure the remote client handles the detach error and

With your patch here it actually worse

$ bin/podman-remote exec -it --detach-keys=a,b magical_mestorf sh
/home/podman # ERRO[0001] Failed to write input to service: detached from container

<<< it just hangs here now forever until you kill the exec session or remote service

So we definitely need the proper tests and code to make it actually work, just by adding the keys you make it worse unfortunately.

The reason the common change in #25083 is needed is because currently the detach check is broken in that it only works when bytes are read one by one which may not be always the case but it may not be strictly needed for this fix.

Overall the exec REST API interface is quite awkward but I don't see how we could chnage it much so we need to make it work with what we got.

I see now that the full solution requires more comprehensive changes like those
in #25083, plus the additional remote infrastructure tracked in #25089. i git ur point so i think i should close this pr as it create it more worse so closing it

@shiavm006 shiavm006 closed this Nov 12, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants