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

Skip to content

Commit 5fab57a

Browse files
committed
fix: chrome requests hanging over port-forward
Fixes #3733
1 parent d2ee18c commit 5fab57a

File tree

2 files changed

+20
-6
lines changed

2 files changed

+20
-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: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -138,8 +138,8 @@ 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")
142+
// closeErr = xerrors.New("signal received")
143143
}
144144

145145
cancel()
@@ -213,7 +213,11 @@ func listenAndPortForward(ctx context.Context, cmd *cobra.Command, conn *codersd
213213
for {
214214
netConn, err := l.Accept()
215215
if err != nil {
216-
_, _ = fmt.Fprintf(cmd.OutOrStderr(), "Error accepting connection from '%v://%v': %+v\n", spec.listenNetwork, spec.listenAddress, err)
216+
// Silently ignore net.ErrClosed errors.
217+
if xerrors.Is(err, net.ErrClosed) {
218+
return
219+
}
220+
_, _ = fmt.Fprintf(cmd.OutOrStderr(), "Error accepting connection from '%v://%v': %v\n", spec.listenNetwork, spec.listenAddress, err)
217221
_, _ = fmt.Fprintln(cmd.OutOrStderr(), "Killing listener")
218222
return
219223
}

0 commit comments

Comments
 (0)