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

fix: memory leak #4431

Open
wants to merge 2 commits into
base: dev
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion client/control.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ func NewControl(ctx context.Context, sessionCtx *SessionContext) (*Control, erro
ctl.msgDispatcher = msg.NewDispatcher(sessionCtx.Conn)
}
ctl.registerMsgHandlers()
ctl.msgTransporter = transport.NewMessageTransporter(ctl.msgDispatcher.SendChannel())
ctl.msgTransporter = transport.NewMessageTransporter(ctl.msgDispatcher.Send)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I hope to keep it as it is.


ctl.pm = proxy.NewManager(ctl.ctx, sessionCtx.Common, ctl.msgTransporter)
ctl.vm = visitor.NewManager(ctl.ctx, sessionCtx.RunID, sessionCtx.Common, ctl.connectServer, ctl.msgTransporter)
Expand Down
1 change: 1 addition & 0 deletions pkg/msg/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ func (d *Dispatcher) readLoop() {
m, err := ReadMsg(d.rw)
if err != nil {
close(d.doneCh)
close(d.sendCh)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sendCh does not need to be actively closed; it should be automatically reclaimed after all senders have exited.

As I commented in transport/message.go, I prefer to use a reasonable mechanism to ensure exit on the sender's side.

return
}

Expand Down
8 changes: 4 additions & 4 deletions pkg/transport/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,15 @@ type MessageTransporter interface {
DispatchWithType(m msg.Message, msgType, laneKey string) bool
}

func NewMessageTransporter(sendCh chan msg.Message) MessageTransporter {
func NewMessageTransporter(sendFn func(msg.Message) error) MessageTransporter {
return &transporterImpl{
sendCh: sendCh,
sendFn: sendFn,
registry: make(map[string]map[string]chan msg.Message),
}
}

type transporterImpl struct {
sendCh chan msg.Message
sendFn func(msg.Message) error

// First key is message type and second key is lane key.
// Dispatch will dispatch message to related channel by its message type
Expand All @@ -54,7 +54,7 @@ type transporterImpl struct {

func (impl *transporterImpl) Send(m msg.Message) error {
Copy link
Owner

@fatedier fatedier Sep 12, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I intend to add a context parameter similar to the Do function to allow cancellation of sending.

One case is when the user exits manually, which can be canceled through this mechanism, and another case is for potential scenarios where a timeout might be set later.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adding context parameters would be a better solution.
In other cases, adding a timeout is indeed a viable solution, and I have considered using this pattern before

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Additionally, a TrySend function can be added, which returns a specific error when sendCh is full, instead of blocking. This function is more suitable for certain specific scenarios.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes,Adding TrySend is indeed a way to make it clearer. I didn't read the code thoroughly and may not fully understand the this function.

return errors.PanicToError(func() {
impl.sendCh <- m
impl.sendFn(m)
})
}

Expand Down
2 changes: 1 addition & 1 deletion server/control.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ func NewControl(
ctl.msgDispatcher = msg.NewDispatcher(ctl.conn)
}
ctl.registerMsgHandlers()
ctl.msgTransporter = transport.NewMessageTransporter(ctl.msgDispatcher.SendChannel())
ctl.msgTransporter = transport.NewMessageTransporter(ctl.msgDispatcher.Send)
return ctl, nil
}

Expand Down