arvidn / libtorrent

an efficient feature complete C++ bittorrent implementation
http://libtorrent.org
Other
5.23k stars 992 forks source link

Crash possible related to tracker_request::outgoing_socket #2144

Closed aldenml closed 7 years ago

aldenml commented 7 years ago

libtorrent version (or branch): master platform/architecture: android/arm but possible all

Hi @ssiloti, now I'm able to see crashes like this:

native: pc 00000000003021d6  /data/app/com.frostwire.android-2/lib/x86/libjlibtorrent.so (libtorrent::torrent::tracker_request_error(libtorrent::tracker_request const&, int, boost::system::error_code const&, std::__ndk1::basic_string<char, std::__ndk1::char_traits<char>, std::__ndk1::allocator<char> > const&, std::__ndk1::chrono::duration<int, std::__ndk1::ratio<1ll, 1ll> >)+966)
native: pc 00000000003391fc  /data/app/com.frostwire.android-2/lib/x86/libjlibtorrent.so (libtorrent::tracker_connection::fail_impl(boost::system::error_code const&, int, std::__ndk1::basic_string<char, std::__ndk1::char_traits<char>, std::__ndk1::allocator<char> >, std::__ndk1::chrono::duration<int, std::__ndk1::ratio<1ll, 1ll> >, std::__ndk1::chrono::duration<int, std::__ndk1::ratio<1ll, 1ll> >)+316)
native: pc 000000000033b696  /data/app/com.frostwire.android-2/lib/x86/libjlibtorrent.so (void boost::asio::asio_handler_invoke<std::__ndk1::__bind<void (libtorrent::tracker_connection::*)(boost::system::error_code const&, int, std::__ndk1::basic_string<char, std::__ndk1::char_traits<char>, std::__ndk1::allocator<char> >, std::__ndk1::chrono::duration<int, std::__ndk1::ratio<1ll, 1ll> >, std::__ndk1::chrono::duration<int, std::__ndk1::ratio<1ll, 1ll> >), std::__ndk1::shared_ptr<libtorrent::tracker_connection>, boo    #03 pc 0033b4e7  /data/app/com.frostwire.android-2/lib/x86/libjlibtorrent.so (boost::asio::detail::completion_handler<std::__ndk1::__bind<void (libtorrent::tracker_connection::*)(boost::system::error_code const&, int, std::__ndk1::basic_string<char, std::__ndk1::char_traits<char>, std::__ndk1::allocator<char> >, std::__ndk1::chrono::duration<int, std::__ndk1::ratio<1ll, 1ll> >, std::__ndk1::chrono::duration<int, std::__ndk1::ratio<1ll, 1ll> >), std::__ndk1::shared_ptr<libtorrent::tracker_connection>, b    #04 pc 00254485  /data/app/com.frostwire.android-2/lib/x86/libjlibtorrent.so (boost::asio::detail::task_io_service::do_run_one(boost::asio::detail::scoped_lock<boost::asio::detail::posix_mutex>&, boost::asio::detail::task_io_service_thread_info&, boost::system::error_code const&)+597)
native: pc 0000000000253fdb  /data/app/com.frostwire.android-2/lib/x86/libjlibtorrent.so (boost::asio::detail::task_io_service::run(boost::system::error_code&)+155)
native: pc 0000000000284a33  /data/app/com.frostwire.android-2/lib/x86/libjlibtorrent.so
native: pc 0000000000094219  /system/lib/libc.so (__pthread_start(void*)+57)
native: pc 0000000000030e4a  /system/lib/libc.so (__start_thread+26)
native: pc 0000000000017646  /system/lib/libc.so (__bionic_clone+70)

and

native: pc 00000000003392fa  /data/app/com.frostwire.android-2/lib/x86/libjlibtorrent.so (_ZNK10libtorrent18tracker_connection14bind_interfaceEv+42)
native: pc 0000000000344137  /data/app/com.frostwire.android-2/lib/x86/libjlibtorrent.so (_ZNK10libtorrent22udp_tracker_connection20pick_target_endpointEv+183)
native: pc 0000000000343e18  /data/app/com.frostwire.android-2/lib/x86/libjlibtorrent.so (_ZN10libtorrent22udp_tracker_connection4failERKN5boost6system10error_codeEiPKcNSt6__ndk16chrono8durationIiNS8_5ratioILx1ELx1EEEEESD_+1192)
native: pc 0000000000345907  /data/app/com.frostwire.android-2/lib/x86/libjlibtorrent.so (_ZN10libtorrent22udp_tracker_connection10on_timeoutERKN5boost6system10error_codeE+263)
native: pc 00000000003388cb  /data/app/com.frostwire.android-2/lib/x86/libjlibtorrent.so (_ZN10libtorrent15timeout_handler16timeout_callbackERKN5boost6system10error_codeE+203)
native: pc 000000000033b2fe  /data/app/com.frostwire.android-2/lib/x86/libjlibtorrent.so (_ZN5boost4asio19asio_handler_invokeINS0_6detail7binder1INSt6__ndk16__bindIMN10libtorrent15timeout_handlerEFvRKNS_6system10error_codeEEJNS4_10shared_ptrIS7_EERNS4_12placeholders4__phILi1EEEEEES9_EEEEvRT_z+46)
native: pc 000000000033b26b  /data/app/com.frostwire.android-2/lib/x86/libjlibtorrent.so (_ZN5boost4asio6detail12wait_handlerINSt6__ndk16__bindIMN10libtorrent15timeout_handlerEFvRKNS_6system10error_codeEEJNS3_10shared_ptrIS6_EERNS3_12placeholders4__phILi1EEEEEEE11do_completeEPNS1_15task_io_serviceEPNS1_25task_io_service_operationESA_j+107)
native: pc 0000000000254485  /data/app/com.frostwire.android-2/lib/x86/libjlibtorrent.so (_ZN5boost4asio6detail15task_io_service10do_run_oneERNS1_11scoped_lockINS1_11posix_mutexEEERNS1_27task_io_service_thread_infoERKNS_6system10error_codeE+597)
native: pc 0000000000253fdb  /data/app/com.frostwire.android-2/lib/x86/libjlibtorrent.so (_ZN5boost4asio6detail15task_io_service3runERNS_6system10error_codeE+155)
native: pc 0000000000284a33  /data/app/com.frostwire.android-2/lib/x86/libjlibtorrent.so
native: pc 0000000000094219  /system/lib/libc.so (_ZL15__pthread_startPv+57)
native: pc 0000000000030e4a  /system/lib/libc.so (__start_thread+26)
native: pc 0000000000017646  /system/lib/libc.so (__bionic_clone+70)

I think the problem is that tracker_request::outgoing_socket is assigned like this:

for (auto& ls : m_listen_sockets)
{
    req.listen_port = listen_port(&ls);
...
    req.outgoing_socket = &ls;
    m_tracker_manager.queue_request(get_io_service(), req, c);
}

and that address becomes invalid when reopen_listen_sockets remove it.

Do you think this is a potential problem? If yes, not clear to me how to address it

ssiloti commented 7 years ago

Yeah, there are definitely problems with dangling pointers to session_listen_socket here. I need to add code to go through the tracker manager and clear out all references to a socket when it is removed, like is done in dht_tracker.

ssiloti commented 7 years ago

Thinking about it a little more, I think it might be better to bite the bullet and store listen_socket_t in a shared_ptr. We would then use weak_ptr instead of raw pointers to session_listen_socket. I had thought I could avoid that overhead without too much trouble, but between this and the previous crash failures with the DHT it seems I was wrong.

aldenml commented 7 years ago

In case you are working on this, I'm giving it a try, but I'm a little lost in the cleanup part. Do you prefer that I wait?

ssiloti commented 7 years ago

I've started working on it. I have a good idea of what I wan to do so you can hold off.

aldenml commented 7 years ago

thanks!

aldenml commented 7 years ago

For what I see in my logs, the problem seems fixed, closing this (and for reopen is something related reappears)