Skip to content

Commit 7b4e29b

Browse files
committed
Fix remote exec ignoring --detach-keys option
The remote client was completely ignoring the --detach-keys option for exec sessions. The ExecStartAndAttach() function hardcoded empty detach keys ([]byte{}) instead of using the detach keys stored in the exec session configuration. Signed-off-by: shiavm006 <shivammittal42006@gmail.com>
1 parent 2b646e7 commit 7b4e29b

File tree

2 files changed

+30
-2
lines changed

2 files changed

+30
-2
lines changed

pkg/bindings/containers/attach.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -402,6 +402,15 @@ func ExecStartAndAttach(ctx context.Context, sessionID string, options *ExecStar
402402
isTerm = respStruct.ProcessConfig.Tty
403403
}
404404

405+
// Extract and parse detach keys from the exec session
406+
detachKeysInBytes := []byte{}
407+
if respStruct.DetachKeys != "" {
408+
detachKeysInBytes, err = term.ToBytes(respStruct.DetachKeys)
409+
if err != nil {
410+
return fmt.Errorf("invalid detach keys in exec session: %w", err)
411+
}
412+
}
413+
405414
// If we are in TTY mode, we need to set raw mode for the terminal.
406415
// TODO: Share all of this with Attach() for containers.
407416
needTTY := terminalFile != nil && terminal.IsTerminal(int(terminalFile.Fd())) && isTerm
@@ -458,7 +467,7 @@ func ExecStartAndAttach(ctx context.Context, sessionID string, options *ExecStar
458467
if options.GetAttachInput() {
459468
go func() {
460469
logrus.Debugf("Copying STDIN to socket")
461-
_, err := detach.Copy(socket, options.InputStream, []byte{})
470+
_, err := detach.Copy(socket, options.InputStream, detachKeysInBytes)
462471
// Ignore "closed network connection" as it occurs when the exec ends, which is expected.
463472
// This avoids noisy logs but does not fix the goroutine leak
464473
// https://github.com/containers/podman/issues/25344
@@ -479,7 +488,7 @@ func ExecStartAndAttach(ctx context.Context, sessionID string, options *ExecStar
479488
return fmt.Errorf("exec session %s has a terminal and must have STDOUT enabled", sessionID)
480489
}
481490
// If not multiplex'ed, read from server and write to stdout
482-
_, err := detach.Copy(options.GetOutputStream(), socket, []byte{})
491+
_, err := detach.Copy(options.GetOutputStream(), socket, detachKeysInBytes)
483492
if err != nil {
484493
return err
485494
}

test/system/075-exec.bats

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -312,4 +312,23 @@ load helpers
312312

313313
run_podman rm -f -t0 $cid
314314
}
315+
316+
# bats test_tags=ci:parallel
317+
@test "podman exec with --detach-keys" {
318+
# Start a long-running container
319+
run_podman run -d $IMAGE top
320+
cid="$output"
321+
322+
# Test with custom detach keys - should not error
323+
# (actual detach behavior is hard to test in non-interactive mode,
324+
# but we can verify the option is accepted)
325+
run_podman exec --detach-keys="ctrl-x,ctrl-x" $cid echo "detach-keys-test"
326+
is "$output" "detach-keys-test" "exec with custom detach-keys works"
327+
328+
# Test with empty detach keys - should not error
329+
run_podman exec --detach-keys="" $cid echo "empty-detach-keys"
330+
is "$output" "empty-detach-keys" "exec with empty detach-keys works"
331+
332+
run_podman rm -f -t0 $cid
333+
}
315334
# vim: filetype=sh

0 commit comments

Comments
 (0)