openDsh / dash

Join us on Slack! https://join.slack.com/t/opendsh/shared_invite/zt-la398uly-a6eMH5ttEQhbtE6asVKx4Q
GNU General Public License v3.0
238 stars 69 forks source link

"what(): bind: Address already in use" on restart after crash. #121

Closed kbader94 closed 2 years ago

kbader94 commented 2 years ago

Issue

"what(): bind: Address already in use" on attempted dash restart, after any other crash.

Hardware

N/A(ALL)

Installation Method: Install.sh

Description of problem:

If for any reason Open Dash crashes, and doesn't shut down properly, it won't be able to start again due to an error binding to an already open socket during the Open Auto initialization. A system restart is required to restart dash in order to get past it. This problem only exists in conjunction with any other error which causes dash to crash or otherwise shutdown improperly in the first place, so it isn't likely to affect end users unless another bug is also present. But for development I've found it to be a PITA because you have to restart the system to fix it(OR see the fix below). This is likely a low priority issue due to the nature of requiring an improper shutdown to trigger it, but the fix is relatively simple.

Expected Result:

The expected result would be that dash can start back up after a crash.

Problem-relevant steps to reproduce:

  1. Crash Open Dash(Writing bad code is a prerequisite!)
  2. Attempt restart
  3. Dash won't restart without a system restart first

Traceback (if applicable):

libc.so.6!GI_raise(int sig) (/build/glibc-sMfBJT/glibc-2.31/sysdeps/unix/sysv/linux/raise.c:50) libc.so.6!__GI_abort() (/build/glibc-sMfBJT/glibc-2.31/stdlib/abort.c:79) libstdc++.so.6![Unknown/Just-In-Time compiled code] (Unknown Source:0) libstdc++.so.6!std::terminate() (Unknown Source:0) libstdc++.so.6!cxa_throw (Unknown Source:0) boost::throw_exception(const boost::system::system_error & e) (/usr/include/boost/throw_exception.hpp:70) boost::asio::detail::do_throw_error(const boost::system::error_code & err, const char location) (/usr/include/boost/asio/detail/impl/throw_error.ipp:38) libopenauto.so.2!boost::asio::detail::throw_error(const char location, const boost::system::error_code & err) (/usr/include/boost/asio/detail/throw_error.hpp:42) libopenauto.so.2!boost::asio::basic_socket_acceptor<boost::asio::ip::tcp, boost::asio::executor>::basic_socket_acceptor(bool reuse_addr, const boost::asio::basic_socket_acceptor<boost::asio::ip::tcp, boost::asio::executor>::endpoint_type & endpoint, context, boost::asio::basic_socket_acceptor<boost::asio::ip::tcp, boost::asio::executor> const this) (/usr/include/boost/asio/basic_socket_acceptor.hpp:277) libopenauto.so.2!openauto::App::App(openauto::App const this, ioService, usbWrapper, tcpWrapper, androidAutoEntityFactory, aasdk::usb::IUSBHub::Pointer usbHub, aasdk::usb::IConnectedAccessoriesEnumerator::Pointer connectedAccessoriesEnumerator) (/home/kyle/Repos/openauto/openauto/App.cpp:38) gnu_cxx::new_allocator::construct<openauto::App, boost::asio::io_context&, aasdk::usb::USBWrapper&, aasdk::tcp::TCPWrapper&, openauto::service::AndroidAutoEntityFactory&, std::shared_ptr&, std::shared_ptr&>(gnu_cxx::new_allocator const this, openauto::App p) (/usr/include/c++/9/ext/new_allocator.h:146) std::allocator_traits<std::allocator >::construct<openauto::App, boost::asio::io_context&, aasdk::usb::USBWrapper&, aasdk::tcp::TCPWrapper&, openauto::service::AndroidAutoEntityFactory&, std::shared_ptr&, std::shared_ptr&>(std::allocator_traits<std::allocator >::allocator_type & a, openauto::App __p) (/usr/include/c++/9/bits/alloc_traits.h:483) std::_Sp_counted_ptr_inplace<openauto::App, std::allocator, (gnu_cxx::_Lock_policy)2>::_Sp_counted_ptr_inplace<boost::asio::io_context&, aasdk::usb::USBWrapper&, aasdk::tcp::TCPWrapper&, openauto::service::AndroidAutoEntityFactory&, std::shared_ptr&, std::shared_ptr&>(std::_Sp_counted_ptr_inplace<openauto::App, std::allocator, (gnu_cxx::_Lock_policy)2> const this, std::allocator a) (/usr/include/c++/9/bits/shared_ptr_base.h:548) std::shared_count<(gnu_cxx::_Lock_policy)2>::shared_count<openauto::App, std::allocator, boost::asio::io_context&, aasdk::usb::USBWrapper&, aasdk::tcp::TCPWrapper&, openauto::service::AndroidAutoEntityFactory&, std::shared_ptr&, std::shared_ptr&>(std::shared_count<(gnu_cxx::_Lock_policy)2> const this, openauto::App & p, std::_Sp_alloc_shared_tag<std::allocator > a) (/usr/include/c++/9/bits/shared_ptr_base.h:679) std::shared_ptr<openauto::App, (__gnu_cxx::_Lock_policy)2>::shared_ptr<std::allocator, boost::asio::io_context&, aasdk::usb::USBWrapper&, aasdk::tcp::TCPWrapper&, openauto::service::AndroidAutoEntityFactory&, std::shared_ptr&, std::shared_ptr&>(std::shared_ptr<openauto::App, (__gnu_cxx::_Lock_policy)2> * const this, std::_Sp_alloc_shared_tag<std::allocator > tag) (/usr/include/c++/9/bits/shared_ptr_base.h:1344) std::shared_ptr::shared_ptr<std::allocator, boost::asio::io_context&, aasdk::usb::USBWrapper&, aasdk::tcp::TCPWrapper&, openauto::service::AndroidAutoEntityFactory&, std::shared_ptr&, std::shared_ptr&>(std::shared_ptr const this, std::_Sp_alloc_shared_tag<std::allocator > __tag) (/usr/include/c++/9/bits/shared_ptr.h:359) std::allocate_shared<openauto::App, std::allocator, boost::asio::io_context&, aasdk::usb::USBWrapper&, aasdk::tcp::TCPWrapper&, openauto::service::AndroidAutoEntityFactory&, std::shared_ptr&, std::shared_ptr&>(const std::allocator & __a) (/usr/include/c++/9/bits/shared_ptr.h:702) std::make_shared<openauto::App, boost::asio::io_context&, aasdk::usb::USBWrapper&, aasdk::tcp::TCPWrapper&, openauto::service::AndroidAutoEntityFactory&, std::shared_ptr&, std::shared_ptr&>() (/usr/include/c++/9/bits/shared_ptr.h:718) OpenAutoWorker::OpenAutoWorker(std::function<void (bool)>, bool, QWidget, Arbiter&)(OpenAutoWorker const this, std::function<void(bool)> callback, bool night_mode, QWidget frame, Arbiter & arbiter) (/home/kyle/Repos/dash/src/app/pages/openauto.cpp:23) OpenAutoPage::init(OpenAutoPage const this) (/home/kyle/Repos/dash/src/app/pages/openauto.cpp:430) Dash::init(Dash const this) (/home/kyle/Repos/dash/src/app/window.cpp:81) Window::Window(Window * const this) (/home/kyle/Repos/dash/src/app/window.cpp:203) main(int argc, char ** argv) (/home/kyle/Repos/dash/src/dash.cpp:29)

Console Output:

[2022-04-09 16:12:35.939707] [0x00007ffff13da840] [info] [OpenAuto] SERVICE FACTORY INITED terminate called after throwing an instance of 'boost::wrapexcept' what(): bind: Address already in use

Additional info:

This bug originates in Open Auto's App constructor, Line 36 of openauto/App.cpp: , acceptor(ioService, boost::asio::ip::tcp::endpoint(boost::asio::ip::tcp::v4(),5000))

Where it attempts to open a tcp connection to port 5000 which is already open from the last use, and presumably not closed properly on the crash. Using the command netstat -tulpn reveals it's a connection to the file browser, oddly. In my case Dolphin is connected. Using the command pkill dolphin resolves the error without requiring a system restart.

So I know this isn't an error in the Open Dash code base, and it also shouldn't affect most users unless there's another bug present too, but can we implement a fix for this to ease development? Either using a random port, or ensuring this port is available before attempting connection? Alternatively I'll just keep using pkill for now but I wanted to open an issue in case anyone else has noticed this error and was wondering where it's coming from.

kbader94 commented 2 years ago

So apparently this is somewhat related to the launcher I'm working on. For whatever reason opening apps from the launcher gives them access to that port too? If they don't get shutdown after a crash then the port stays open with a connection to one of the apps that were open. This might happen with the existing launcher too but it's such an edge case we can probably close this issue. Or we can keep it open and I'll implement a fix later, this is definitely fairly low on the priority list for me tho.

icecube45 commented 2 years ago

I was going to say.. I segfault with every other bit of code I write and haven't seen this.

kbader94 commented 2 years ago

Even with the launcher you're working on? I want to say it's probably related to opening a child process and then obviously not closing it when it crashes, since I open processes the same way as the main branch I was thinking this may be present in the original. In either case I'll close this issue for now. I'll try and remember to fix it later although I think it would need a change to the Open Auto repo. At the very least it's documented now so if anyone encounters this problem when working on the main branch they'll have something to reference.