Skip to content

Commit 3a43226

Browse files
fhs0intro
authored andcommitted
net: hangup TCP connection after Dial timeout in Plan 9
After Dial timeout, force close the TCP connection by writing "hangup" to the control file. This unblocks the "connect" command if the connection is taking too long to establish, and frees up the control file FD. Fixes #40118 Change-Id: I1cef8539cd9fe0793e32b49c9d0ef636b4b26e1d Reviewed-on: https://go-review.googlesource.com/c/go/+/241638 Run-TryBot: David du Colombier <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: David du Colombier <[email protected]>
1 parent 574dac9 commit 3a43226

File tree

1 file changed

+26
-2
lines changed

1 file changed

+26
-2
lines changed

src/net/ipsock_plan9.go

+26-2
Original file line numberDiff line numberDiff line change
@@ -206,9 +206,9 @@ func dialPlan9Blocking(ctx context.Context, net string, laddr, raddr Addr) (fd *
206206
return nil, err
207207
}
208208
if la := plan9LocalAddr(laddr); la == "" {
209-
_, err = f.WriteString("connect " + dest)
209+
err = hangupCtlWrite(ctx, proto, f, "connect "+dest)
210210
} else {
211-
_, err = f.WriteString("connect " + dest + " " + la)
211+
err = hangupCtlWrite(ctx, proto, f, "connect "+dest+" "+la)
212212
}
213213
if err != nil {
214214
f.Close()
@@ -339,3 +339,27 @@ func plan9LocalAddr(addr Addr) string {
339339
}
340340
return ip.String() + "!" + itoa(port)
341341
}
342+
343+
func hangupCtlWrite(ctx context.Context, proto string, ctl *os.File, msg string) error {
344+
if proto != "tcp" {
345+
_, err := ctl.WriteString(msg)
346+
return err
347+
}
348+
written := make(chan struct{})
349+
errc := make(chan error)
350+
go func() {
351+
select {
352+
case <-ctx.Done():
353+
ctl.WriteString("hangup")
354+
errc <- mapErr(ctx.Err())
355+
case <-written:
356+
errc <- nil
357+
}
358+
}()
359+
_, err := ctl.WriteString(msg)
360+
close(written)
361+
if e := <-errc; err == nil && e != nil { // we hung up
362+
return e
363+
}
364+
return err
365+
}

0 commit comments

Comments
 (0)