Skip to content

fix: remove context from wsl connection struct #1663

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

Merged
merged 6 commits into from
Jan 2, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions pkg/shellexec/shellexec.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,10 +148,12 @@ func (pp *PipePty) WriteString(s string) (n int, err error) {
}

func StartWslShellProc(ctx context.Context, termSize waveobj.TermSize, cmdStr string, cmdOpts CommandOptsType, conn *wsl.WslConn) (*ShellProc, error) {
utilCtx, cancelFn := context.WithTimeout(ctx, 2*time.Second)
defer cancelFn()
client := conn.GetClient()
shellPath := cmdOpts.ShellPath
if shellPath == "" {
remoteShellPath, err := wsl.DetectShell(conn.Context, client)
remoteShellPath, err := wsl.DetectShell(utilCtx, client)
if err != nil {
return nil, err
}
Expand All @@ -160,13 +162,13 @@ func StartWslShellProc(ctx context.Context, termSize waveobj.TermSize, cmdStr st
var shellOpts []string
log.Printf("detected shell: %s", shellPath)

err := wsl.InstallClientRcFiles(conn.Context, client)
err := wsl.InstallClientRcFiles(utilCtx, client)
if err != nil {
log.Printf("error installing rc files: %v", err)
return nil, err
}

homeDir := wsl.GetHomeDir(conn.Context, client)
homeDir := wsl.GetHomeDir(utilCtx, client)
shellOpts = append(shellOpts, "~", "-d", client.Name())

var subShellOpts []string
Expand Down
19 changes: 13 additions & 6 deletions pkg/wsl/wsl.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ type WslConn struct {
HasWaiter *atomic.Bool
LastConnectTime int64
ActiveConnNum int
Context context.Context
cancelFn func()
}

Expand Down Expand Up @@ -188,6 +187,8 @@ func (conn *WslConn) OpenDomainSocketListener() error {
}

func (conn *WslConn) StartConnServer() error {
utilCtx, cancelFn := context.WithTimeout(context.Background(), 5*time.Second)
defer cancelFn()
var allowed bool
conn.WithLock(func() {
if conn.Status != Status_Connecting {
Expand All @@ -200,7 +201,7 @@ func (conn *WslConn) StartConnServer() error {
return fmt.Errorf("cannot start conn server for %q when status is %q", conn.GetName(), conn.GetStatus())
}
client := conn.GetClient()
wshPath := GetWshPath(conn.Context, client)
wshPath := GetWshPath(utilCtx, client)
rpcCtx := wshrpc.RpcContext{
ClientType: wshrpc.ClientType_ConnServer,
Conn: conn.GetName(),
Expand All @@ -210,7 +211,7 @@ func (conn *WslConn) StartConnServer() error {
if err != nil {
return fmt.Errorf("unable to create jwt token for conn controller: %w", err)
}
shellPath, err := DetectShell(conn.Context, client)
shellPath, err := DetectShell(utilCtx, client)
if err != nil {
return err
}
Expand All @@ -221,7 +222,14 @@ func (conn *WslConn) StartConnServer() error {
cmdStr = fmt.Sprintf("%s=\"%s\" %s connserver --router", wshutil.WaveJwtTokenVarName, jwtToken, wshPath)
}
log.Printf("starting conn controller: %s\n", cmdStr)
cmd := client.WslCommand(conn.Context, cmdStr)
connServerCtx, cancelFn := context.WithCancel(context.Background())
conn.WithLock(func() {
if conn.cancelFn != nil {
conn.cancelFn()
}
conn.cancelFn = cancelFn
})
cmd := client.WslCommand(connServerCtx, cmdStr)
pipeRead, pipeWrite := io.Pipe()
inputPipeRead, inputPipeWrite := io.Pipe()
cmd.SetStdout(pipeWrite)
Expand Down Expand Up @@ -473,8 +481,7 @@ func getConnInternal(name string) *WslConn {
connName := WslName{Distro: name}
rtn := clientControllerMap[name]
if rtn == nil {
ctx, cancelFn := context.WithCancel(context.Background())
rtn = &WslConn{Lock: &sync.Mutex{}, Status: Status_Init, Name: connName, HasWaiter: &atomic.Bool{}, Context: ctx, cancelFn: cancelFn}
rtn = &WslConn{Lock: &sync.Mutex{}, Status: Status_Init, Name: connName, HasWaiter: &atomic.Bool{}, cancelFn: nil}
clientControllerMap[name] = rtn
}
return rtn
Expand Down
Loading