1
0
mirror of https://github.com/containers/podman.git synced 2026-02-05 06:45:31 +01:00

feat(exec): Add --no-session flag for improved performance

Fixes: #26588

For use cases like HPC, where `podman exec` is called in rapid succession, the standard exec process can become a bottleneck due to container locking and database I/O for session tracking.

This commit introduces a new `--no-session` flag to `podman exec`. When used, this flag invokes a new, lightweight backend implementation that:

- Skips container locking, reducing lock contention
- Bypasses the creation, tracking, and removal of exec sessions in the database
- Executes the command directly and retrieves the exit code without persisting session state
- Maintains consistency with regular exec for container lookup, TTY handling, and environment setup
- Shares implementation with health check execution to avoid code duplication

The implementation addresses all performance bottlenecks while preserving compatibility with existing exec functionality including --latest flag support and proper exit code handling.

Changes include:
- Add --no-session flag to cmd/podman/containers/exec.go
- Implement lightweight execution path in libpod/container_exec.go
- Ensure consistent container validation and environment setup
- Add comprehensive exit code testing including signal handling (exit 137)
- Optimize configuration to skip unnecessary exit command setup

Signed-off-by: Ryan McCann <ryan_mccann@student.uml.edu>
Signed-off-by: ryanmccann1024 <ryan_mccann@student.uml.edu>
This commit is contained in:
ryanmccann1024
2025-07-31 11:16:57 -05:00
parent 5c48d02fe8
commit 61cbc0c3ee
8 changed files with 237 additions and 105 deletions

View File

@@ -51,6 +51,7 @@ var (
execOpts entities.ExecOptions
execDetach bool
execCidFile string
execNoSession bool
)
func execFlags(cmd *cobra.Command) {
@@ -100,6 +101,10 @@ func execFlags(cmd *cobra.Command) {
flags.Int32(waitFlagName, 0, "Total seconds to wait for container to start")
_ = flags.MarkHidden(waitFlagName)
if !registry.IsRemote() {
flags.BoolVar(&execNoSession, "no-session", false, "Do not create a database session for the exec process")
}
if registry.IsRemote() {
_ = flags.MarkHidden("preserve-fds")
}
@@ -121,6 +126,12 @@ func init() {
}
func exec(cmd *cobra.Command, args []string) error {
if execNoSession {
if execDetach || cmd.Flags().Changed("detach-keys") {
return errors.New("--no-session cannot be used with --detach or --detach-keys")
}
}
nameOrID, command, err := determineTargetCtrAndCmd(args, execOpts.Latest, execCidFile != "")
if err != nil {
return err
@@ -168,17 +179,21 @@ func exec(cmd *cobra.Command, args []string) error {
}
}
if !execDetach {
streams := define.AttachStreams{}
streams.OutputStream = os.Stdout
streams.ErrorStream = os.Stderr
if execOpts.Interactive {
streams.InputStream = bufio.NewReader(os.Stdin)
streams.AttachInput = true
}
streams.AttachOutput = true
streams.AttachError = true
streams := define.AttachStreams{}
streams.OutputStream = os.Stdout
streams.ErrorStream = os.Stderr
if execOpts.Interactive {
streams.InputStream = bufio.NewReader(os.Stdin)
streams.AttachInput = true
}
streams.AttachOutput = true
streams.AttachError = true
if execNoSession {
exitCode, err := registry.ContainerEngine().ContainerExecNoSession(registry.Context(), nameOrID, execOpts, streams)
registry.SetExitCode(exitCode)
return err
} else if !execDetach {
exitCode, err := registry.ContainerEngine().ContainerExec(registry.Context(), nameOrID, execOpts, streams)
registry.SetExitCode(exitCode)
return err