Sync Gateway idle and not closing sessions


I am having a situation where my sessions are not closing even app force closed.

Is there anyway to force close sessions if app is not active and idle for more than 6 minutes?

At the moment load balancer idle sessions set to 6 minutes but still no luck and it seems like sync gateway is initiating a connection with client even client closed the app.

Any thoughts on this would be very helpful.

Here is the firewall sessions duration screenshot.

Load Balance session information of above firewall connection.

As you can see from above screen dump sessions are still active in firewall and load balancer (36501 seconds) and do not see that user is really active on app as i have tested myself and do not get disconnected even i close the app.

I am running Couchbase CE 4.0 and sync gateway 1.2.0 and couchbase lite 1.3 on android device and 1.1 on iOS


By default Sync Gateway is going to send a heartbeat to maintain a continuous replication, which would explain why the load balancer idle settings aren’t terminating the request.

I don’t know the details about whether a client application would be expected to terminate an active continuous replication when the app is closed, or if the replication needs to be explicitly stopped. @jens, @hideki - can you shed any light on that?

In case application is forced to exit/close, the socket connection with the SG is disconnected. But I believe the SG or the proxy can not detect the disconnection immediately. I called it half-closed connection. I believe the SG can detect disconnection when SG sends heartbeat.

By “session” I think you mean “TCP connection”? These are probably the app connections to the “changes feed”, used for server push.

I am having a situation where my sessions are not closing even app force closed.

If an app process exits, any TCP sockets it was using are immediately closed by the kernel. The socket should immediately be closed on the server side too, as soon as it receives the close packet from the client. (That’s just basic TCP behavior, nothing specific to us.)

The replicator also closes the socket when the app tells it to stop.

Are you sure the app process has actually quit?

it seems like sync gateway is initiating a connection with client even client closed the app.

SG will never open a socket to the app. Maybe you mean that it’s trying to send data over one of these left-open sockets? If that happens, the server’s TCP stack will detect that it never got a response to its packet, and will close the socket.

This should only happen if connectivity between the device and server is lost (i.e. device moves out of WiFi/cell range, or interference causes all packets to be dropped), or if the device abruptly loses power. If the app process exits, the TCP implementation in the OS kernel will send a FIN packet to tell the other end that the connection is closed.

Thank you all for your comments,

I have captured same information for same IP and it seems like it has got disconnected and connected again and this time i can still see connection online and have not disconnected (23411s).

After further testing today i have found some info regarding session idle.

Android App:

  1. Connect to couchbase sync gateway creates 2 sessions and once app goes background it closes one sessions and 1 more sessions idle for number of minutes and no idea when it closes.
  2. If i close the app then it does not close sessions and stays online number of minutes (No idea about when it closes the sessions and noticed some sessions are kept idle more than a day)
  3. Once i go to settings–> Application Manager and then “Force close” app then all sessions closes and does not create any sessions or any idle sessions anymore. (Couch lite 1.3)


  1. It creates 2 sessions when app opens and after few seconds goes to 1 sessions and depends on data push and pull it creates more sessoins.
  2. Once app goes background all sessions closes within few seconds and no need to force close app and all good with iOS app which runs on (couch lite 1.1)

Sync gate 1.2:

  1. Since i have above issue, last week i have set “maxhearbeat” settings to 120 seconds in sync gateway config and i am going to remove that settings and test again and see if any improvement.

Linux keep alive settings:
If i set linux keep alive settings as mentioned in couchbase site below, does it help to close sessions?


Most of the TCP sockets you’re seeing are HTTP connections opened by the OS’s HTTP library. Those are going to stay open a while in case the app sends more HTTP requests. The details of that are up to the HTTP implementation, which we don’t control.

(Actually, @hideki: on Android do we use an HTTP library built into CBL? In that case do we need to tell it to close sockets when the app is backgrounded?)

Thank you for your reply.

I have noticed even i restart Android device or shutdown and wait for a while does not close sessions. As restart give new public IP through 4G network and it creates sessions with new IP even though i do not open the application.

Only option to kill session through “force close” app option from Android Settings --> Application Manager and then select app and “Force Close”

I am planning on Keep alive settings and see how i go.


Oh, is the device connecting through cellular, not WiFi? Then there are probably some telco gateways in between, and who knows what they’re doing.

Is this causing actual problems for you, or are you just curious? I can’t imagine what you’re running into is any different from normal, and I don’t think we’ve gotten a report like this before.

At the moment i am testing with 4G only as our internal WIFI used by so many people and would be hard for me to test and narrow down my device sessions.

Yes, it is causing serious problem as sessions never go down and load balance always have high sessions usage, Sync gateway servers cpu and ram usage is very high around 90% and above.

Currently i am limiting sync gateway sessions at load balancer level and that helps us to run other websites.


CBL Android uses OkHttp. It does not have background setting.

I guess one connection keeps remaining for /_changes API by ChangeTracker even though application is in background.

You could try to stop replicator whenever application enters background mode.

Between CBL and SG, SG sends heart-beat to CBL. If heart-beat is not reachable to CBL, SG closes socket connection.


@Karunakar if you see sessions started after a reboot of the device, without opening the app, something must be starting up. The typical case would be a service started by a broadcast receiver listening for an on boot event.

What’s the structure of your app? Do you have any services, or just activities? Are you able to reproduce what you’re seeing with a simple test app?

Also, you can examine the network activity and processes directly on a device with adb. If you reboot, and start a shell on the device (“adb shell”) and run “ps”, do you see anything related to your app? Use netstat to look at network state.


Thank you Hideki and Hod

Yes, it creates even i restart or power off the mobile device and power back again.

What’s the structure of your app?

Push and pull updated as the app gets updates from the server and changes made on app pushed back to server.

Do you have any services, or just activities?

It is based on activities and i may need to check with developer but what i understand from developer it is based on activity and only gets update if any change on server end and wise-versa otherwise it should chose sessions.

Are you able to reproduce what you’re seeing with a simple test app?

Yes i can reproduce this issue and can see sessions dies after N number of minutes (No idea about when sessions are closing as some closes with in 2 hours and some stays more than a day)

I will try tracing with adb and see any traffic related to app but in my testing i have noticed when i restart phone i get a new public IP and which is making two sessions on Firewall/load balancer/couchbase sync gateway as i could follow the traffic from external device public IP to load balancer and once it passed on to sync gateway i could not trace anymore as sync gateway does not have an option to record client IP.


I have done a packet capture and here are the packet capture information.

Could you please help me what are these calls and noticed these are happening even app closed.

I have attached packet capture information.

Thank you.

Karunakar (1.2 KB)

@Karunakar sorry, missed your post.

I’d like to direct you back to looking at what processes are running after device boot. It simply makes no sense that your app would be trying to connect if it hasn’t been started in some way. Can you get a process listing after boot and after you see network traffic start?


@hod.greeley Thank you for your reply.

Last week myself and developer have performed further troubleshooting and found out that we are using a “OneSignal” framework for app, which is used for push notification.

OneSignal site:

It is basically listens the boot event since it’s a push notification framework. Once OneSignal fires their boot event, CBLite somehow gets activated and creates sessions.

Removing the framework solved the problem as we tested last week. Apart from that we have nothing else listening to boot event.

I have attached manifest file of “OneSignal” framework.

Can you please look into this issue. (1.2 KB)


Couchbase Lite is a library in your app, not a separate process. It’s not going to do anything on its own; for it to connect to the server, your app must have launched and started a Replication. Can’t you make your app log or hit a breakpoint or something when this happens, to diagnose it?

@jens Thank you.

I have forwarded your message to developer and update you if anything we find in our app.