Skip to content

Commit

Permalink
Merge pull request #393 from drakkan/rsleak
Browse files Browse the repository at this point in the history
request server: fix handles leak in error case
  • Loading branch information
drakkan authored Nov 16, 2020
2 parents 4bca1e2 + 732b2a3 commit c30c93e
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
12 changes: 10 additions & 2 deletions request-server.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,12 +198,20 @@ func (rs *RequestServer) packetWorker(
rpkt = cleanPacketPath(pkt)
case *sshFxpOpendirPacket:
request := requestFromPacket(ctx, pkt)
rs.nextRequest(request)
handle := rs.nextRequest(request)
rpkt = request.opendir(rs.Handlers, pkt)
if _, ok := rpkt.(*sshFxpHandlePacket); !ok {
// if we return an error we have to remove the handle from the active ones
rs.closeRequest(handle)
}
case *sshFxpOpenPacket:
request := requestFromPacket(ctx, pkt)
rs.nextRequest(request)
handle := rs.nextRequest(request)
rpkt = request.open(rs.Handlers, pkt)
if _, ok := rpkt.(*sshFxpHandlePacket); !ok {
// if we return an error we have to remove the handle from the active ones
rs.closeRequest(handle)
}
case *sshFxpFstatPacket:
handle := pkt.getHandle()
request, ok := rs.getRequest(handle)
Expand Down
4 changes: 4 additions & 0 deletions request-server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,9 @@ func TestRequestOpenFail(t *testing.T) {
rf, err := p.cli.Open("/foo")
assert.Exactly(t, os.ErrNotExist, err)
assert.Nil(t, rf)
// if we return an error the sftp client will not close the handle
// ensure that we close it ourself
assert.Len(t, p.svr.openRequests, 0)
checkRequestServerAllocator(t, p)
}

Expand Down Expand Up @@ -711,6 +714,7 @@ func TestRequestReaddir(t *testing.T) {
require.Len(t, di, 100)
names := []string{di[18].Name(), di[81].Name()}
assert.Equal(t, []string{"foo_18", "foo_81"}, names)
assert.Len(t, p.svr.openRequests, 0)
checkRequestServerAllocator(t, p)
}

Expand Down

0 comments on commit c30c93e

Please sign in to comment.