Skip to content

Commit

Permalink
Avoid dead lock on deferred pty close and infinite reconnect loop
Browse files Browse the repository at this point in the history
  • Loading branch information
Stanislas POLU committed Apr 28, 2017
1 parent 5264d28 commit 0e4735a
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 13 deletions.
3 changes: 2 additions & 1 deletion client/command/connect.go
Expand Up @@ -223,6 +223,7 @@ func (c *Connect) Execute(

// Listen for state updates.
go func() {
STATELOOP:
for {
if st, err := c.ss.DecodeState(ctx); err != nil {
break
Expand All @@ -236,7 +237,7 @@ func (c *Connect) Execute(

select {
case <-ctx.Done():
break
break STATELOOP
default:
}
}
Expand Down
22 changes: 11 additions & 11 deletions client/command/open.go
Expand Up @@ -208,7 +208,7 @@ func (c *Open) Execute(
)
}

// Store initial sizxe of the terminal.
// Store initial size of the terminal.
cols, rows, err := terminal.GetSize(stdin)
if err != nil {
return errors.Trace(
Expand All @@ -231,7 +231,11 @@ func (c *Open) Execute(
)
}
// Restores the terminal once we're done.
defer terminal.Restore(stdin, old)
defer func() {
terminal.Restore(stdin, old)
// Let's attempt to clean things up with a newline.
fmt.Printf("\n")
}()

// Start shell.
c.cmd = exec.Command(c.shell)
Expand All @@ -255,9 +259,6 @@ func (c *Open) Execute(
cancel()
}()

// Closes the newly created pty.
defer c.pty.Close()

// Main loops.

// c.errC is used to capture user facing errors generated from the
Expand Down Expand Up @@ -362,10 +363,7 @@ func (c *Open) Execute(

<-ctx.Done()

// By then we probably show a prompt so lets add add a newline.
fmt.Printf("\n")

return userErr
return errors.Trace(userErr)
}

// ReconnectLoop handles reconnecting the host to warpd. Each time the
Expand All @@ -377,6 +375,7 @@ func (c *Open) ConnLoop(
ctx context.Context,
) {
first := true
CONNLOOP:
for {
var conn net.Conn
var err error
Expand Down Expand Up @@ -419,7 +418,7 @@ func (c *Open) ConnLoop(

select {
case <-ctx.Done():
break
break CONNLOOP
default:
}
}
Expand Down Expand Up @@ -506,6 +505,7 @@ func (c *Open) ManageSession(

// Listen for state updates.
go func() {
STATELOOP:
for {
if st, err := ss.DecodeState(ctx); err != nil {
break
Expand All @@ -516,7 +516,7 @@ func (c *Open) ManageSession(
}
select {
case <-ctx.Done():
break
break STATELOOP
default:
}
}
Expand Down
3 changes: 2 additions & 1 deletion lib/plex/plex.go
Expand Up @@ -12,6 +12,7 @@ func Run(
src io.Reader,
) {
buf := make([]byte, 1024)
PLEXLOOP:
for {
nr, err := src.Read(buf)
if nr > 0 {
Expand All @@ -24,7 +25,7 @@ func Run(
}
select {
case <-ctx.Done():
break
break PLEXLOOP
default:
}
}
Expand Down

0 comments on commit 0e4735a

Please sign in to comment.