Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Investigate other memory leaks #1484

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions common/browser.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,19 @@ func (b *Browser) initEvents() error { //nolint:cyclop
if b.vuCtxCancelFn != nil {
b.vuCtxCancelFn()
}

go func() {
<-b.browserCtx.Done()

b.browserProc = nil
b.browserOpts = nil
b.conn = nil
b.context = nil
b.defaultContext = nil
b.pages = nil
b.sessionIDtoTargetID = nil
b.logger = nil
}()
}()
for {
select {
Expand Down
11 changes: 11 additions & 0 deletions common/browser_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,17 @@ func NewBrowserContext(
return nil, fmt.Errorf("adding web vital init script to new browser context: %w", err)
}

go func() {
<-b.ctx.Done()

b.browser = nil
b.opts = nil
b.timeoutSettings = nil
b.logger = nil
b.vu = nil
b.evaluateOnNewDocumentSources = nil
}()

return &b, nil
}

Expand Down
11 changes: 11 additions & 0 deletions common/connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,17 @@ func (c *Connection) close(code int) error {
// Stop the main control loop
close(c.done)
_ = c.conn.Close()

go func() {
<-c.ctx.Done()

c.cancelCtx = nil
c.logger = nil
c.conn = nil
c.msgIDGen = nil
c.sessions = nil
c.onTargetAttachedToTarget = nil
}()
}()

c.closeAllSessions()
Expand Down
13 changes: 13 additions & 0 deletions common/frame.go
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,19 @@ func (f *Frame) detach() error {
return fmt.Errorf("disposing document handle while detaching frame: %w", err)
}

f.page = nil
f.manager = nil
f.parentFrame = nil
f.childFrames = nil
f.vu = nil
f.lifecycleEvents = nil
f.documentHandle = nil
f.executionContexts = nil
f.inflightRequests = nil
f.currentDocument = nil
f.pendingDocument = nil
f.log = nil

return nil
}

Expand Down
17 changes: 17 additions & 0 deletions common/frame_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,23 @@ func NewFrameManager(
id: atomic.AddInt64(&frameManagerID, 1),
}

go func() {
<-m.ctx.Done()

for _, f := range m.frames {
m.removeFramesRecursively(f)
}

m.session = nil
m.page = nil
m.timeoutSettings = nil
m.mainFrame = nil
m.frames = nil
m.barriers = nil
m.vu = nil
m.logger = nil
}()

m.logger.Debugf("FrameManager:New", "fmid:%d", m.ID())

return m
Expand Down
18 changes: 18 additions & 0 deletions common/frame_session.go
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,24 @@ func (fs *FrameSession) initEvents() {
}

go func() {
go func() {
<-fs.ctx.Done()

fs.session = nil
fs.page = nil
fs.parent = nil
fs.manager = nil
fs.networkManager = nil
fs.k6Metrics = nil
fs.contextIDToContext = nil
fs.isolatedWorlds = nil
fs.eventCh = nil
fs.childSessions = nil
fs.vu = nil
fs.logger = nil
fs.mainFrameSpan = nil
}()

fs.logger.Debugf("NewFrameSession:initEvents:go",
"sid:%v tid:%v", fs.session.ID(), fs.targetID)
defer func() {
Expand Down
4 changes: 4 additions & 0 deletions common/js_handle.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,10 @@ func (h *BaseJSHandle) dispose() error {
h.remoteObject.ObjectID, err)
}

h.session = nil
h.execCtx = nil
h.remoteObject = nil

return nil
}

Expand Down
38 changes: 37 additions & 1 deletion common/network_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,9 +179,30 @@ func parseTTL(ttlS string) (time.Duration, error) {
}

func (m *NetworkManager) deleteRequestByID(reqID network.RequestID) {
var req *Request
m.reqsMu.Lock()
defer m.reqsMu.Unlock()
req = m.reqIDToRequest[reqID]
delete(m.reqIDToRequest, reqID)
m.reqsMu.Unlock()

// We need to nil the request in response otherwise they hold onto each
// others reference preventing the GC from cleaning the memory up.
req.responseMu.Lock()
if req.response != nil {
req.response.request = nil
req.response.body = nil
}
req.response = nil
req.responseMu.Unlock()

req.postDataEntries = nil

for _, r := range req.redirectChain {
if reqID != r.getID() {
m.deleteRequestByID(r.getID())
}
}
req.redirectChain = nil
}

func (m *NetworkManager) emitRequestMetrics(req *Request) {
Expand Down Expand Up @@ -354,6 +375,21 @@ func (m *NetworkManager) initEvents() {
}, chHandler)

go func() {
defer func() {
m.logger = nil
m.session = nil
m.parent = nil
m.frameManager = nil
m.credentials = nil
m.resolver = nil
m.vu = nil
m.customMetrics = nil
m.mi = nil
m.reqIDToRequest = nil
m.attemptedAuth = nil
m.extraHTTPHeaders = nil
}()

for m.handleEvents(chHandler) {
}
}()
Expand Down
23 changes: 23 additions & 0 deletions common/page.go
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,29 @@ func (p *Page) initEvents() {
defer func() {
p.logger.Debugf("Page:initEvents:go:return",
"sid:%v tid:%v", p.session.ID(), p.targetID)

go func() {
<-p.ctx.Done()

p.Keyboard = nil
p.Mouse = nil
p.Touchscreen = nil
p.session = nil
p.browserCtx = nil
p.opener = nil
p.frameManager = nil
p.timeoutSettings = nil
p.emulatedSize = nil
p.extraHTTPHeaders = nil
p.eventCh = nil
p.eventHandlers = nil
p.mainFrameSession = nil
p.frameSessions = nil
p.workers = nil
p.routes = nil
p.vu = nil
p.logger = nil
}()
}()

for {
Expand Down
2 changes: 2 additions & 0 deletions common/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ func (s *Session) close() {
s.closed = true

s.emit(EventSessionClosed, nil)

s.conn = nil
}

func (s *Session) markAsCrashed() {
Expand Down
Loading