Skip to content

Commit 098ca1c

Browse files
authored
remove context support for net.Listener in sftp server (#87)
1 parent 6108b26 commit 098ca1c

File tree

3 files changed

+21
-14
lines changed

3 files changed

+21
-14
lines changed

go.mod

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module github.com/minio/pkg/v2
22

3-
go 1.19
3+
go 1.20
44

55
require (
66
github.com/cheggaaa/pb v1.0.29
@@ -16,6 +16,7 @@ require (
1616
github.com/montanaflynn/stats v0.7.0
1717
github.com/rjeczalik/notify v0.9.3
1818
go.etcd.io/etcd/client/v3 v3.5.7
19+
golang.org/x/crypto v0.9.0
1920
golang.org/x/sys v0.8.0
2021
gopkg.in/yaml.v2 v2.4.0
2122
)
@@ -61,7 +62,6 @@ require (
6162
go.uber.org/atomic v1.10.0 // indirect
6263
go.uber.org/multierr v1.9.0 // indirect
6364
go.uber.org/zap v1.24.0 // indirect
64-
golang.org/x/crypto v0.9.0 // indirect
6565
golang.org/x/net v0.10.0 // indirect
6666
golang.org/x/sync v0.1.0 // indirect
6767
golang.org/x/text v0.9.0 // indirect

go.sum

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ github.com/Azure/go-ntlmssp v0.0.0-20220621081337-cb9428e4ac1e/go.mod h1:chxPXzS
22
github.com/Azure/go-ntlmssp v0.0.0-20221128193559-754e69321358 h1:mFRzDkZVAjdal+s7s0MwaRv9igoPqLRdzOLzw/8Xvq8=
33
github.com/Azure/go-ntlmssp v0.0.0-20221128193559-754e69321358/go.mod h1:chxPXzSsl7ZWRAuOIE23GDNzjWuZquvFlgA8xmpunjU=
44
github.com/benbjohnson/clock v1.1.0 h1:Q92kusRqC1XV2MjkWETPvjJVqKetz1OzxZB7mHJLju8=
5+
github.com/benbjohnson/clock v1.1.0/go.mod h1:J11/hYXuz8f4ySSvYwY0FKfm+ezbsZBKZxNJlLklBHA=
56
github.com/cheggaaa/pb v1.0.29 h1:FckUN5ngEk2LpvuG0fw1GEFx6LtyY2pWI/Z2QgCnEYo=
67
github.com/cheggaaa/pb v1.0.29/go.mod h1:W40334L7FMC5JKWldsTWbdGjLo0RxUKK73K+TuPxX30=
78
github.com/coreos/go-semver v0.3.1 h1:yi21YpKnrx1gt5R+la8n5WgS0kCrsPp33dmEyHReZr4=
@@ -86,6 +87,7 @@ github.com/minio/minio-go/v7 v7.0.49/go.mod h1:UI34MvQEiob3Cf/gGExGMmzugkM/tNgbF
8687
github.com/minio/mux v1.8.2 h1:r9oVDFM09y+u8CF4HPLanguAG41niXgYwZAFkVHce9M=
8788
github.com/minio/mux v1.8.2/go.mod h1:1pAare17ZRL5GpmNL+9YmqHoWnLmMZF9C/ioUCfy0BQ=
8889
github.com/minio/sha256-simd v1.0.0 h1:v1ta+49hkWZyvaKwrQB8elexRqm6Y0aMLjCNsrYxo6g=
90+
github.com/minio/sha256-simd v1.0.0/go.mod h1:OuYzVNI5vcoYIAmbIvHPl3N3jUzVedXbKy5RFepssQM=
8991
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
9092
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg=
9193
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
@@ -101,6 +103,7 @@ github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
101103
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
102104
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
103105
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 h1:Jamvg5psRIccs7FGNTlIRMkT8wgtp5eCXdBlqhYGL6U=
106+
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
104107
github.com/power-devops/perfstat v0.0.0-20210106213030-5aafc221ea8c/go.mod h1:OmDBASR4679mdNQnz2pUhc2G8CO2JrUAVFDRBDP/hJE=
105108
github.com/power-devops/perfstat v0.0.0-20221212215047-62379fc7944b h1:0LFwY6Q3gMACTjAbMZBjXAqTOzOwFaj2Ld6cjeQ7Rig=
106109
github.com/power-devops/perfstat v0.0.0-20221212215047-62379fc7944b/go.mod h1:OmDBASR4679mdNQnz2pUhc2G8CO2JrUAVFDRBDP/hJE=
@@ -148,6 +151,7 @@ go.etcd.io/etcd/client/v3 v3.5.7/go.mod h1:sOWmj9DZUMyAngS7QQwCyAXXAL6WhgTOPLNS/
148151
go.uber.org/atomic v1.10.0 h1:9qC72Qh0+3MqyJbAn8YU5xVq1frD8bn3JtD2oXtafVQ=
149152
go.uber.org/atomic v1.10.0/go.mod h1:LUxbIzbOniOlMKjJjyPfpl4v+PKK2cNJn91OQbhoJI0=
150153
go.uber.org/goleak v1.1.11 h1:wy28qYRKZgnJTxGxvye5/wgWr1EKjmUDGYox5mGlRlI=
154+
go.uber.org/goleak v1.1.11/go.mod h1:cwTWslyiVhfpKIDGSZEM2HlOvcqm+tG4zioyIeLoqMQ=
151155
go.uber.org/multierr v1.9.0 h1:7fIwc/ZtS0q++VgcfqFDxSBZVv/Xo49/SYnDFupUwlI=
152156
go.uber.org/multierr v1.9.0/go.mod h1:X2jQV1h+kxSjClGpnseKVIxpmcjrj7MNnI0bnlfKTVQ=
153157
go.uber.org/zap v1.24.0 h1:FiJd5l1UOLj0wCgbSE0rwwXHzEdAZS6hiiSnxJN/D60=
@@ -207,6 +211,7 @@ golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9sn
207211
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
208212
golang.org/x/term v0.3.0/go.mod h1:q750SLmJuPmVoN1blW3UFBPREJfb1KmY3vwxfr+nFDA=
209213
golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k=
214+
golang.org/x/term v0.8.0 h1:n5xxQn2i3PC0yLAbjTpNT85q/Kgzcr2gIoX9OrJUols=
210215
golang.org/x/term v0.8.0/go.mod h1:xPskH00ivmX89bAKVGSKKtLOWNx2+17Eiy94tnKShWo=
211216
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
212217
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=

sftp/sftp.go

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ type Logger interface {
6767

6868
// Server implements a composable SFTP Server.
6969
type Server struct {
70+
quit chan struct{}
7071
port int
7172
publicIP string
7273
sshConfig ssh.ServerConfig
@@ -75,14 +76,14 @@ type Server struct {
7576
beforeHandle func(conn net.Conn, err error) (acceptConn bool)
7677
handleSFTPSession func(channel ssh.Channel, sconn *ssh.ServerConn)
7778
listener net.Listener
78-
ctx context.Context
79-
cancelFunc context.CancelFunc
8079
}
8180

8281
// ShutDown calls the cancel context and shuts
8382
// down the SFTP server.
84-
func (s *Server) ShutDown() {
85-
s.cancelFunc()
83+
func (s *Server) ShutDown() (err error) {
84+
close(s.quit)
85+
err = s.listener.Close()
86+
return
8687
}
8788

8889
// Options defines required configurations
@@ -92,8 +93,6 @@ type Options struct {
9293
PublicIP string
9394
Logger Logger
9495
SSHConfig *ssh.ServerConfig
95-
// Context is used to shut down the net.Listener
96-
Context context.Context
9796
// ConnectionKeepAlive controls how long the connection keep-alive duration is set to.
9897
ConnectionKeepAlive time.Duration
9998
// SSHHandshakeDeadline controls the time.Duration which ssh session
@@ -139,14 +138,11 @@ func NewServer(options *Options) (sftpServer *Server, err error) {
139138
}
140139

141140
sftpServer = new(Server)
142-
if options.Context == nil {
143-
sftpServer.ctx, sftpServer.cancelFunc = context.WithCancel(context.Background())
144-
} else {
145-
sftpServer.ctx, sftpServer.cancelFunc = context.WithCancel(options.Context)
146-
}
147141

142+
// net.Listener does not respect the context cancelFunc.
143+
// Hence we just pass it a normal context.Background()
148144
sftpServer.listener, err = lc.Listen(
149-
sftpServer.ctx,
145+
context.Background(),
150146
"tcp",
151147
net.JoinHostPort(options.PublicIP, strconv.Itoa(options.Port)),
152148
)
@@ -161,6 +157,7 @@ func NewServer(options *Options) (sftpServer *Server, err error) {
161157
sftpServer.beforeHandle = options.BeforeHandle
162158
sftpServer.handleSFTPSession = options.HandleSFTPSession
163159
sftpServer.logger = options.Logger
160+
sftpServer.quit = make(chan struct{})
164161
return
165162
}
166163

@@ -180,6 +177,11 @@ func (s *Server) Listen() (err error) {
180177
continue
181178
}
182179
if err != nil {
180+
select {
181+
case <-s.quit:
182+
return nil
183+
default:
184+
}
183185
// Temporary() is deprecated but since it's been deployed to
184186
// current production builds I do not want to simply switch it out.
185187
// ISSUE: https://github.com/golang/go/issues/45729

0 commit comments

Comments
 (0)