Skip to content

Commit 8a95f4e

Browse files
authored
Merge pull request #237 from SenseUnit/fix_subprocess_ctx
Fix subprocess lifecycle
2 parents 57258c5 + 8bd2bc7 commit 8a95f4e

3 files changed

Lines changed: 28 additions & 11 deletions

File tree

‎dialer/command.go‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,8 @@ func (d *CommandDialer) Dial(network, address string) (net.Conn, error) {
4949
}
5050

5151
func (d *CommandDialer) DialContext(ctx context.Context, network, address string) (net.Conn, error) {
52-
cmd := exec.CommandContext(ctx, d.command[0], d.command[1:]...)
52+
killCtx, kill := context.WithCancel(context.Background())
53+
cmd := exec.CommandContext(killCtx, d.command[0], d.command[1:]...)
5354
cmd.Env = append(os.Environ(),
5455
"DUMBPROXY_DST_ADDR="+address,
5556
"DUMBPROXY_DST_NET="+network,
@@ -72,7 +73,7 @@ func (d *CommandDialer) DialContext(ctx context.Context, network, address string
7273
cmdIn.Close()
7374
cmdOut.Close()
7475
}()
75-
return NewPipeConn(cmdOut.(ReadPipe), cmdIn.(WritePipe)), nil
76+
return NewPipeConn(cmdOut.(ReadPipe), cmdIn.(WritePipe), kill), nil
7677
}
7778

7879
func (d *CommandDialer) WantsHostname(_ context.Context, _, _ string) bool {

‎dialer/pipewrap.go‎

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"io"
66
"net"
77
"sync"
8+
"sync/atomic"
89
"time"
910

1011
"github.com/hashicorp/go-multierror"
@@ -40,16 +41,19 @@ func (a PipeAddr) String() string {
4041
}
4142

4243
type PipeConn struct {
43-
r ReadPipe
44-
w WritePipe
45-
rc sync.Once
46-
wc sync.Once
44+
r ReadPipe
45+
w WritePipe
46+
onClose func()
47+
rc sync.Once
48+
wc sync.Once
49+
closeFlags atomic.Uint32
4750
}
4851

49-
func NewPipeConn(r ReadPipe, w WritePipe) *PipeConn {
52+
func NewPipeConn(r ReadPipe, w WritePipe, onClose func()) *PipeConn {
5053
return &PipeConn{
51-
r: r,
52-
w: w,
54+
r: r,
55+
w: w,
56+
onClose: onClose,
5357
}
5458
}
5559

@@ -76,14 +80,26 @@ func (c *PipeConn) CloseWrite() error {
7680
var err error
7781
c.wc.Do(func() {
7882
err = c.w.Close()
83+
if c.onClose != nil {
84+
c.closeFlags.Or(2)
85+
if c.closeFlags.Load() == 3 {
86+
c.onClose()
87+
}
88+
}
7989
})
8090
return err
8191
}
8292

8393
func (c *PipeConn) CloseRead() error {
8494
var err error
85-
c.wc.Do(func() {
95+
c.rc.Do(func() {
8696
err = c.r.Close()
97+
if c.onClose != nil {
98+
c.closeFlags.Or(1)
99+
if c.closeFlags.Load() == 3 {
100+
c.onClose()
101+
}
102+
}
87103
})
88104
return err
89105
}

‎handler/stdio.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ func StdIOHandler(d HandlerDialer, logger *clog.CondLogger, forward ForwardFunc)
2020
}
2121
defer target.Close()
2222

23-
return forward(ctx, "", dialer.NewPipeConn(reader, writer), target, "tcp", dstAddress)
23+
return forward(ctx, "", dialer.NewPipeConn(reader, writer, nil), target, "tcp", dstAddress)
2424
}
2525
}
2626

0 commit comments

Comments
 (0)