Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 29acd25

Browse files
authored
fix: chrome requests hanging over port-forward (#4588)
1 parent d2ee18c commit 29acd25

File tree

2 files changed

+19
-6
lines changed

2 files changed

+19
-6
lines changed

agent/agent.go

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -879,12 +879,22 @@ func (r *reconnectingPTY) Close() {
879879
// after one or both of them are done writing. If the context is canceled, both
880880
// of the connections will be closed.
881881
func Bicopy(ctx context.Context, c1, c2 io.ReadWriteCloser) {
882-
defer c1.Close()
883-
defer c2.Close()
882+
ctx, cancel := context.WithCancel(ctx)
883+
defer cancel()
884+
885+
defer func() {
886+
_ = c1.Close()
887+
_ = c2.Close()
888+
}()
884889

885890
var wg sync.WaitGroup
886891
copyFunc := func(dst io.WriteCloser, src io.Reader) {
887-
defer wg.Done()
892+
defer func() {
893+
wg.Done()
894+
// If one side of the copy fails, ensure the other one exits as
895+
// well.
896+
cancel()
897+
}()
888898
_, _ = io.Copy(dst, src)
889899
}
890900

cli/portforward.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -138,8 +138,7 @@ func portForward() *cobra.Command {
138138
case <-ctx.Done():
139139
closeErr = ctx.Err()
140140
case <-sigs:
141-
_, _ = fmt.Fprintln(cmd.OutOrStderr(), "Received signal, closing all listeners and active connections")
142-
closeErr = xerrors.New("signal received")
141+
_, _ = fmt.Fprintln(cmd.OutOrStderr(), "\nReceived signal, closing all listeners and active connections")
143142
}
144143

145144
cancel()
@@ -213,7 +212,11 @@ func listenAndPortForward(ctx context.Context, cmd *cobra.Command, conn *codersd
213212
for {
214213
netConn, err := l.Accept()
215214
if err != nil {
216-
_, _ = fmt.Fprintf(cmd.OutOrStderr(), "Error accepting connection from '%v://%v': %+v\n", spec.listenNetwork, spec.listenAddress, err)
215+
// Silently ignore net.ErrClosed errors.
216+
if xerrors.Is(err, net.ErrClosed) {
217+
return
218+
}
219+
_, _ = fmt.Fprintf(cmd.OutOrStderr(), "Error accepting connection from '%v://%v': %v\n", spec.listenNetwork, spec.listenAddress, err)
217220
_, _ = fmt.Fprintln(cmd.OutOrStderr(), "Killing listener")
218221
return
219222
}

0 commit comments

Comments
 (0)