Skip to content

Commit

Permalink
Merge pull request #100 from CoiaPrant233/patch
Browse files Browse the repository at this point in the history
fix: memory leak
  • Loading branch information
xtaci authored Oct 4, 2024
2 parents 784d53a + 41e6a51 commit fbed95b
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 4 deletions.
5 changes: 4 additions & 1 deletion session.go
Original file line number Diff line number Diff line change
Expand Up @@ -576,7 +576,10 @@ func (s *Session) sendLoop() {
// writeControlFrame writes the control frame to the underlying connection
// and returns the number of bytes written if successful
func (s *Session) writeControlFrame(f Frame) (n int, err error) {
return s.writeFrameInternal(f, time.After(openCloseTimeout), CLSCTRL)
timer := time.NewTimer(openCloseTimeout)
defer timer.Stop()

return s.writeFrameInternal(f, timer.C, CLSCTRL)
}

// internal writeFrame version to support deadline used in keepalive
Expand Down
12 changes: 10 additions & 2 deletions session_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -892,7 +892,11 @@ func TestWriteFrameInternal(t *testing.T) {
session.Close()
for i := 0; i < 100; i++ {
f := newFrame(1, byte(rand.Uint32()), rand.Uint32())
session.writeFrameInternal(f, time.After(session.config.KeepAliveTimeout), CLSDATA)

timer := time.NewTimer(session.config.KeepAliveTimeout)
defer timer.Stop()

session.writeFrameInternal(f, timer.C, CLSDATA)
}

// random cmds
Expand All @@ -904,7 +908,11 @@ func TestWriteFrameInternal(t *testing.T) {
session, _ = Client(cli, nil)
for i := 0; i < 100; i++ {
f := newFrame(1, allcmds[rand.Int()%len(allcmds)], rand.Uint32())
session.writeFrameInternal(f, time.After(session.config.KeepAliveTimeout), CLSDATA)

timer := time.NewTimer(session.config.KeepAliveTimeout)
defer timer.Stop()

session.writeFrameInternal(f, timer.C, CLSDATA)
}
//deadline occur
{
Expand Down
6 changes: 5 additions & 1 deletion stream.go
Original file line number Diff line number Diff line change
Expand Up @@ -494,7 +494,11 @@ func (s *stream) Close() error {
if once {
// send FIN in order
f := newFrame(byte(s.sess.config.Version), cmdFIN, s.id)
_, err = s.sess.writeFrameInternal(f, time.After(openCloseTimeout), CLSDATA)

timer := time.NewTimer(openCloseTimeout)
defer timer.Stop()

_, err = s.sess.writeFrameInternal(f, timer.C, CLSDATA)
s.sess.streamClosed(s.id)
return err
} else {
Expand Down

0 comments on commit fbed95b

Please sign in to comment.