Skip to content

Conversation

@jursonmo
Copy link

@jursonmo jursonmo commented Sep 4, 2025

Fixes #385

1.客户端重启可能导致协程泄漏的问题

2025-9-03,客户端在重启过程中可能会有协程泄漏的问题,特别是重复调用client.Restart()或者忘记调用client.Stop(), 以致没有关闭c.exitChan. 导致协程没有退出。同时也有可能导致底层网络连接没有关闭。
func (c *Client) Restart() {
    .....
    .....
    func() {
        .......

		select {
		case <-c.exitChan:
			zlog.Ins().InfoF("client exit.")
		}
    }()
}

2.重复调用client.Stop() 会panic的问题。

func (c *Client) Stop() {
	con := c.Conn()
	if con != nil {
		zlog.Ins().InfoF("[STOP] Zinx Client LocalAddr: %s, RemoteAddr: %s\n", con.LocalAddr(), con.RemoteAddr())
		con.Stop()
	}
	c.exitChan <- struct{}{}
	close(c.exitChan)
	close(c.ErrChan)
}

重复调用client.Stop()的话, 第二次调用client.Stop()会往exitChan这个已经closed的channel 写入数据,导致panic

处理方法:

  1. 允许重复调用client.Restart(), 每次重启时会内部会调用client.Stop() 停掉目前正在运行的后台协程,同时会关闭底层网络连接,保证一个client只要一个后台协程在运行。
  2. 允许重复调用client.Stop(), 内部判断client是否处于运行状态,只有真正运行才做实际操作,比如关闭底层网络连接等。
  3. 增加context, 用来取消后台协程的运行,和控制底层网络连接dial的生命周期。

@aceld
Copy link
Owner

aceld commented Sep 15, 2025

@jursonmo Thanks for this PR

@aceld aceld merged commit b3c9f9b into aceld:master Sep 15, 2025
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

协程泄漏

2 participants