Android couchbase-lite-core poll() crash when in background

I encountered several crashes on Android, all happening after onPause() or onStop() events (so while the application is in background).

Is there any knowledge about this issue?

Should the onPause()/onStop() events suspend the replicator activity?

Below are a few stack traces:

${ANDROID_NDK}/android-ndk-r21e-windows-x86_64/android-ndk-r21e/toolchains/llvm/prebuilt/windows-x86_64/sysroot/usr/include/c++/v1/memory:2562 std::__ndk1::unique_ptr<sockpp::sock_address, std::__ndk1::default_delete<sockpp::sock_address> >::operator->() const	
{$COUCHBASE_PATH}\couchbase-lite-core\Networking\WebSockets\BuiltInWebSocket.cc:432 litecore::websocket::BuiltInWebSocket::writeToSocket()	
${ANDROID_NDK}/android-ndk-r21e-windows-x86_64/android-ndk-r21e/toolchains/llvm/prebuilt/windows-x86_64/sysroot/usr/include/c++/v1/functional:1867 std::__ndk1::__function::__value_func<void ()>::operator()() const	
{$COUCHBASE_PATH}\couchbase-lite-core\Networking\Poller.cc:299 litecore::net::Poller::poll()	
{$COUCHBASE_PATH}\couchbase-lite-core\Networking\Poller.cc:150 operator()	
/apex/com.android.runtime/lib64/bionic/libc.so:999864 0x791507e1b8	
/apex/com.android.runtime/lib64/bionic/libc.so:585192 0x7915018de8	
unknown 0x0	
${COUCHBASE_PATH}\couchbase-lite-core\Networking\TCPSocket.cc:1499 litecore::net::TCPSocket::checkStreamError()	
${ANDROID_NDK_PATH}/android-ndk-r21e-windows-x86_64/android-ndk-r21e/toolchains/llvm/prebuilt/windows-x86_64/sysroot/usr/include/c++/v1/functional:1867 std::__ndk1::__function::__value_func<void ()>::operator()() const	
${COUCHBASE_PATH}\couchbase-lite-core\Networking\Poller.cc:297 litecore::net::Poller::poll()	
${COUCHBASE_PATH}\couchbase-lite-core\Networking\Poller.cc:150 operator()	
/apex/com.android.runtime/lib64/bionic/libc.so:746040 0x7dc83c1238	
/apex/com.android.runtime/lib64/bionic/libc.so:331368 0x7dc835be68	
unknown 0x0	
${COUCHBASE_PATH}\couchbase-lite-core\vendor\mbedtls\library\debug.c:82 debug_send_line	
${COUCHBASE_PATH}\couchbase-lite-core\vendor\mbedtls\library\ssl_tls.c:4467 mbedtls_ssl_write_record	
${COUCHBASE_PATH}\couchbase-lite-core\vendor\mbedtls\library\ssl_tls.c:11035 ssl_write_real	
${COUCHBASE_PATH}\couchbase-lite-core\vendor\mbedtls\library\ssl_tls.c:11065 ssl_write_split	
${COUCHBASE_PATH}\couchbase-lite-core\vendor\sockpp\src\mbedtls_context.cpp:289 sockpp::mbedtls_socket::write(void const*, unsigned long)	
${COUCHBASE_PATH}\couchbase-lite-core\Networking\TCPSocket.cc:260 litecore::net::TCPSocket::write(std::__ndk1::vector<fleece::slice, std::__ndk1::allocator<fleece::slice> >&)	
${COUCHBASE_PATH}\couchbase-lite-core\Networking\WebSockets\BuiltInWebSocket.cc:432 litecore::websocket::BuiltInWebSocket::writeToSocket()	
${ANDROID_NDK_PATH}/android-ndk-r21e-windows-x86_64/android-ndk-r21e/toolchains/llvm/prebuilt/windows-x86_64/sysroot/usr/include/c++/v1/functional:1867 std::__ndk1::__function::__value_func<void ()>::operator()() const	
${COUCHBASE_PATH}\couchbase-lite-core\Networking\Poller.cc:299 litecore::net::Poller::poll()	
${COUCHBASE_PATH}\couchbase-lite-core\Networking\Poller.cc:150 operator()	
/system/lib64/libc.so:424120 0x77e05128b8	
/system/lib64/libc.so:126760 0x77e04c9f28	
unknown 0x0

Hi @Ionut_Cosmin_Mihai
I would think it wold be best practice to make sure that replication did not run for very long, when it was not needed. In particular, the Android system is going to suspend your app, at some point. You will not get any notification that that’s going to happen so, if a replicator is running it might well be stopped abruptly.

I don’t recognize those logs. What version of the platform are you running?

The version used by couchbase-lite-C which is GitHub - couchbase/couchbase-lite-core at e25884358722da0939f325b8f0a31c53ccbafad2 . By the way, I noticed that there are a lot of new commits in couchbase-lite-core, but couchbase-lite-C still uses couchbase-lite-core revision e25884358722da0939f325b8f0a31c53ccbafad2 . Is this intended?
The solution for now was to stop the replicator when the application is in background.

I’m really happy to hear that that worked for you.

None the less, if you don’t mind, I’d like to pursue this a bit. Our app should not be crashing…

Just to set some expectations here, Couchbase Lite C is still prerelease. There have been no official releases of it yet and it has been tested only minimally so bugs are pretty much expected. That being said, we certainly would like to get rid of them so perhaps you could indicate whether these are just logs or actual crashes? It is recommended to stop replication when you go into the background as well because things can be unpredictable while there. You can resume them when your app comes back to the foreground again.

Also, the C repo is in a bit of turmoil right now and as you’ve noticed the master branch is currently getting quite old. The main branch being worked on at the moment is the dev branch which we are working on stabilizing before merging back to master and then continuing there so you might have more luck with that one as well.

Let us know how the above suggestions work.

@borrrden I’m aware that Couchbase Lite C is not released yet, I posted this issue because it is happening inside the lite-core.
Actual crashes not just logs.

Stopping the replicator when in background did not solve the problem of that socket being null. I investigated a bit more and I think the respective socket becomes null because the connection with the database is still active when in background(although the replication is stopped). I’ll try next to disconnect/connect the database for background/foreground.

Below is another point where is crashing, apparently because of the same reason.

C:\Users\SomeUser\couchbase-lite-C\vendor\couchbase-lite-core\vendor\fleece\Fleece\Support\RefCounted.hh:99 fleece::Retained<litecore::blip::BLIPIO>::operator->() const	
C:\Users\SomeUser\couchbase-lite-C\vendor\couchbase-lite-core\Networking\WebSockets\BuiltInWebSocket.cc:250 litecore::websocket::BuiltInWebSocket::_connectLoop()	
C:\Users\SomeUser\couchbase-lite-C\vendor\couchbase-lite-core\Networking\WebSockets\BuiltInWebSocket.cc:129 litecore::websocket::BuiltInWebSocket::_bgConnect()	
C:/Users/SomeUser/Downloads/android-ndk-r21e-windows-x86_64/android-ndk-r21e/toolchains/llvm/prebuilt/windows-x86_64/sysroot/usr/include/c++/v1/type_traits:3442 _ZNSt6__ndk18__invokeIRMN8litecore9websocket16BuiltInWebSocketEFvvERPS3_JEvEEDTcldsdeclsr3std6__ndk1E7forwardIT0_Efp0_Efp_spclsr3std6__ndk1E7forwardIT1_Efp1_EEEOT_OS9_DpOSA_	
/apex/com.android.runtime/lib64/bionic/libc.so:999864 0x705cf4a1b8	
/apex/com.android.runtime/lib64/bionic/libc.so:585192 0x705cee4de8	
unknown 0x0