-
Notifications
You must be signed in to change notification settings - Fork 3.2k
fix: Modify the container terminal connection mode #8301
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -25,8 +25,8 @@ type LocalCommand struct { | |
| pty *os.File | ||
| } | ||
|
|
||
| func NewCommand(initCmd string) (*LocalCommand, error) { | ||
| cmd := exec.Command("bash") | ||
| func NewCommand(initCmd []string) (*LocalCommand, error) { | ||
| cmd := exec.Command("docker", initCmd...) | ||
| if term := os.Getenv("TERM"); term != "" { | ||
| cmd.Env = append(os.Environ(), "TERM="+term) | ||
| } else { | ||
|
|
@@ -38,11 +38,6 @@ func NewCommand(initCmd string) (*LocalCommand, error) { | |
| return nil, errors.Wrapf(err, "failed to start command") | ||
| } | ||
|
|
||
| if len(initCmd) != 0 { | ||
| time.Sleep(100 * time.Millisecond) | ||
| _, _ = pty.Write([]byte(initCmd + "\n")) | ||
| } | ||
|
|
||
| lcmd := &LocalCommand{ | ||
| closeSignal: DefaultCloseSignal, | ||
| closeTimeout: DefaultCloseTimeout, | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There are a couple of improvements and corrections that can be made to this function:
Here's how you could refactor the relevant part based on these points: import (
"context"
"os"
"syscall"
"time"
execCtx "golang.org/x/sys/exec/exec_ctx"
)
const (
DefaultCloseContext = context.Background() // Assuming this was a typo as described above
)
func Run(ctx context.Context, dockerCmd ...string) error {
cmd := execCtx.WithDirectIO(exec.Command("docker", dockerCmd...))
errChan := make(chan error, 1)
go func(cmd *exec.Cmd) {
defer close(errChan)
errChan <- cmd.Run()
}(cmd)
select {
case err := <-errChan:
return err
case <-ctx.Done():
stdinPipe, stdinError := cmd.StdinPipe()
if stdinErr == nil && ctx.Err() != context.Canceled {
_, _ = stdinPipe.Write([]byte(strings.Join(dockerCmd, "\n") + "\n")) // Ensure each arg followed by a newline
readStderr(&stdinPipe, logrus.Errorf)
os.Exit(127)
}
return ctx.Err()
default:
time.Sleep(time.Second) // Wait for some seconds before proceeding further, optional depending on use-case
cmd.Process.Signal(syscall.SIGTERM)
return ctx.Err()
}
}
// Example usage:
// runContext, cancelFunc := context.WithCancel(context.Background())
// defer cancelFunc()
// if err := Run(runContext, "-itd", image); err != nil {
// logrus.Fatalf("Failed running Docker: %s", err)
/// }This refactored version uses structured contexts where appropriate and provides more detailed error handling around user input processing when starting processes inside a container via direct I/O operations. It also includes comments explaining important parts of what the function does, which helps clarify its purpose and functionality. |
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The provided code seems generally clean and well-structured, but there are a few potential improvements:
Potential Improvements
loadRedisInitCmd,loadOllamaInitCmd, andloadContainerInitCmdfunctions have similar logic. Consider creating a common function to handle these cases.Function Naming Consistency:
wshandleError, could be more descriptive or consistent with other methods. For example,handleWebSocketError.Security Best Practices:
Logging:
Performance Optimization:
Overall, while minor changes can enhance readability and maintainability, the core functionality appears robust.