openimsdk / openim-sdk-core

Instant Messaging
https://openim.io
Apache License 2.0
326 stars 304 forks source link

Bug: <Heartbeat not working in Android when application in background> #536

Open Vihanmy opened 4 months ago

Vihanmy commented 4 months ago

What happened?

//【1】: When user logined im and im runs works right (in android application) //【2】: App runs in background or screen lock situation //【3】: heart beat in android client blocked and sendPingToServer() never involved `func (c *LongConnMgr) heartbeat(ctx context.Context) { log.ZDebug(ctx, "heartbeat start", "goroutine ID:", getGoroutineID()) ticker := time.NewTicker(pingPeriod) defer func() { ticker.Stop() log.ZWarn(c.ctx, "heartbeat closed", nil, "heartbeat", "heartbeat done sdk logout.....") }() for { select { case <-ctx.Done(): log.ZInfo(ctx, "heartbeat done sdk logout.....") return case <-c.heartbeatCh: c.sendPingToServer(ctx) case <-ticker.C: c.sendPingToServer(ctx) //██ Blocked Here 🆕🆕🆕 } }

}` //【4】Server will never receive ping from client every 30 seconds //【5】Server will close this connection becouse heart beat failed from client //【6】Client will receive 10006 error and reconnect to server //【7】above progress will repeat every 30 seconds //██ the result is when app in background, the reconnect progress will repeat every 30s. Server will consume lots of resources.

What did you expect to happen?

When client in background, the heartbeat can work right. On the other word, CODE_1 will still work when client in bacground.

`func (c *LongConnMgr) heartbeat(ctx context.Context) { log.ZDebug(ctx, "heartbeat start", "goroutine ID:", getGoroutineID()) ticker := time.NewTicker(pingPeriod) defer func() { ticker.Stop() log.ZWarn(c.ctx, "heartbeat closed", nil, "heartbeat", "heartbeat done sdk logout.....") }() for { select { case <-ctx.Done(): log.ZInfo(ctx, "heartbeat done sdk logout.....") return case <-c.heartbeatCh: c.sendPingToServer(ctx) case <-ticker.C: c.sendPingToServer(ctx) //██ CODE_1 } }

}`

How can we reproduce it (as minimally and precisely as possible)?

App runs in background or screen lock situation

Anything else we need to know?

No response

version

```console $ {name} 3.4.1 # paste output here ```

Cloud provider

//【】

OS version

```console # On Linux: $ cat /etc/os-release # paste output here $ uname -a # paste output here # On Windows: C:\> wmic os get Caption, Version, BuildNumber, OSArchitecture # paste output here ```

Install tools

kubbot commented 2 months ago

This issue is stale because it has been open 60 days with no activity. Remove stale label or comment or this will be closed in 7 days.

kubbot commented 1 month ago

This issue was closed because it has been stalled for 7 days with no activity.