Skip to content

Commit 6c53eb8

Browse files
authored
fix: remove context from wsl connection struct (#1663)
This fixes a WSL bug where disconnecting the controller does not allow the controller to restart until the app reboots.
1 parent 3fc4009 commit 6c53eb8

File tree

2 files changed

+18
-9
lines changed

2 files changed

+18
-9
lines changed

pkg/shellexec/shellexec.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -148,10 +148,12 @@ func (pp *PipePty) WriteString(s string) (n int, err error) {
148148
}
149149

150150
func StartWslShellProc(ctx context.Context, termSize waveobj.TermSize, cmdStr string, cmdOpts CommandOptsType, conn *wsl.WslConn) (*ShellProc, error) {
151+
utilCtx, cancelFn := context.WithTimeout(ctx, 2*time.Second)
152+
defer cancelFn()
151153
client := conn.GetClient()
152154
shellPath := cmdOpts.ShellPath
153155
if shellPath == "" {
154-
remoteShellPath, err := wsl.DetectShell(conn.Context, client)
156+
remoteShellPath, err := wsl.DetectShell(utilCtx, client)
155157
if err != nil {
156158
return nil, err
157159
}
@@ -160,13 +162,13 @@ func StartWslShellProc(ctx context.Context, termSize waveobj.TermSize, cmdStr st
160162
var shellOpts []string
161163
log.Printf("detected shell: %s", shellPath)
162164

163-
err := wsl.InstallClientRcFiles(conn.Context, client)
165+
err := wsl.InstallClientRcFiles(utilCtx, client)
164166
if err != nil {
165167
log.Printf("error installing rc files: %v", err)
166168
return nil, err
167169
}
168170

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

172174
var subShellOpts []string

pkg/wsl/wsl.go

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@ type WslConn struct {
5151
HasWaiter *atomic.Bool
5252
LastConnectTime int64
5353
ActiveConnNum int
54-
Context context.Context
5554
cancelFn func()
5655
}
5756

@@ -188,6 +187,8 @@ func (conn *WslConn) OpenDomainSocketListener() error {
188187
}
189188

190189
func (conn *WslConn) StartConnServer() error {
190+
utilCtx, cancelFn := context.WithTimeout(context.Background(), 5*time.Second)
191+
defer cancelFn()
191192
var allowed bool
192193
conn.WithLock(func() {
193194
if conn.Status != Status_Connecting {
@@ -200,7 +201,7 @@ func (conn *WslConn) StartConnServer() error {
200201
return fmt.Errorf("cannot start conn server for %q when status is %q", conn.GetName(), conn.GetStatus())
201202
}
202203
client := conn.GetClient()
203-
wshPath := GetWshPath(conn.Context, client)
204+
wshPath := GetWshPath(utilCtx, client)
204205
rpcCtx := wshrpc.RpcContext{
205206
ClientType: wshrpc.ClientType_ConnServer,
206207
Conn: conn.GetName(),
@@ -210,7 +211,7 @@ func (conn *WslConn) StartConnServer() error {
210211
if err != nil {
211212
return fmt.Errorf("unable to create jwt token for conn controller: %w", err)
212213
}
213-
shellPath, err := DetectShell(conn.Context, client)
214+
shellPath, err := DetectShell(utilCtx, client)
214215
if err != nil {
215216
return err
216217
}
@@ -221,7 +222,14 @@ func (conn *WslConn) StartConnServer() error {
221222
cmdStr = fmt.Sprintf("%s=\"%s\" %s connserver --router", wshutil.WaveJwtTokenVarName, jwtToken, wshPath)
222223
}
223224
log.Printf("starting conn controller: %s\n", cmdStr)
224-
cmd := client.WslCommand(conn.Context, cmdStr)
225+
connServerCtx, cancelFn := context.WithCancel(context.Background())
226+
conn.WithLock(func() {
227+
if conn.cancelFn != nil {
228+
conn.cancelFn()
229+
}
230+
conn.cancelFn = cancelFn
231+
})
232+
cmd := client.WslCommand(connServerCtx, cmdStr)
225233
pipeRead, pipeWrite := io.Pipe()
226234
inputPipeRead, inputPipeWrite := io.Pipe()
227235
cmd.SetStdout(pipeWrite)
@@ -473,8 +481,7 @@ func getConnInternal(name string) *WslConn {
473481
connName := WslName{Distro: name}
474482
rtn := clientControllerMap[name]
475483
if rtn == nil {
476-
ctx, cancelFn := context.WithCancel(context.Background())
477-
rtn = &WslConn{Lock: &sync.Mutex{}, Status: Status_Init, Name: connName, HasWaiter: &atomic.Bool{}, Context: ctx, cancelFn: cancelFn}
484+
rtn = &WslConn{Lock: &sync.Mutex{}, Status: Status_Init, Name: connName, HasWaiter: &atomic.Bool{}, cancelFn: nil}
478485
clientControllerMap[name] = rtn
479486
}
480487
return rtn

0 commit comments

Comments
 (0)