libreoffice-online/net/DelaySocket.hpp

36 lines
956 B
C++
Raw Normal View History

2017-03-23 12:14:51 -05:00
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
/*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
#pragma once
2017-03-23 12:14:51 -05:00
#include <Socket.hpp>
wsd: avoid static SocketPoll The lifetime management of static objects is extremely unpredictable and depends on many variables outside of our control or even reliable reproducibility. Complex static objects that own threads and other objects are doubly problematic because of their dependency and/or interaction with other objects. Here we replace the static DelayPoll instance with one we control its lifetime in the LOOLWSD main body, such that it is destroyed properly. Specifically, DelayPoll's dtor was accessing Poco's Logging subsystem out of order. That is, after Poco had been destroyed. Another advantage to this approach is that we don't even create the DelayPoll at all if we don't need it. The onus now is on the user of DelayPoll to make sure they create a Delay object with a long-enough lifetime to encompase it use. For completeness, here is the stacktrace: terminate called after throwing an instance of 'std::bad_alloc' what(): std::bad_alloc Program received signal SIGABRT, Aborted. __GI_raise (sig=sig@entry=6) at ../sysdeps/unix/sysv/linux/raise.c:51 51 ../sysdeps/unix/sysv/linux/raise.c: No such file or directory. (gdb) bt #0 __GI_raise (sig=sig@entry=6) at ../sysdeps/unix/sysv/linux/raise.c:51 #1 0x00007ffff613f801 in __GI_abort () at abort.c:79 #2 0x00007ffff6d51957 in ?? () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6 #3 0x00007ffff6d57ae6 in ?? () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6 #4 0x00007ffff6d56b49 in ?? () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6 #5 0x00007ffff6d574b8 in __gxx_personality_v0 () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6 #6 0x00007ffff671f573 in ?? () from /lib/x86_64-linux-gnu/libgcc_s.so.1 #7 0x00007ffff671fad1 in _Unwind_RaiseException () from /lib/x86_64-linux-gnu/libgcc_s.so.1 #8 0x00007ffff6d57d47 in __cxa_throw () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6 #9 0x00007ffff6d582dc in operator new(unsigned long) () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6 #10 0x00005555556b5927 in void std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::_M_construct<char*>(char*, char*, std::forward_iterator_tag) [clone .isra.41] () #11 0x0000555555982a14 in Poco::Message::Message(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, Poco::Message::Priority) () #12 0x000055555585a909 in SocketPoll::~SocketPoll (this=0x555555d10f60 <DelayPoll>, __in_chrg=<optimized out>) at net/Socket.cpp:145 #13 0x00007ffff6142041 in __run_exit_handlers (status=0, listp=0x7ffff64ea718 <__exit_funcs>, run_list_atexit=run_list_atexit@entry=true, run_dtors=run_dtors@entry=true) at exit.c:108 #14 0x00007ffff614213a in __GI_exit (status=<optimized out>) at exit.c:139 #15 0x0000555555752d78 in LOOLWSD::innerInitialize (this=<optimized out>, self=...) at wsd/LOOLWSD.cpp:1213 #16 0x0000555555788b07 in LOOLWSD::initialize (this=<optimized out>, self=...) at wsd/LOOLWSD.hpp:432 #17 0x00005555558dd7e4 in Poco::Util::Application::run() () #18 0x00005555556b6574 in main (argc=2, argv=0x7fffffffe528) at wsd/LOOLWSD.cpp:4276 Change-Id: Ifda55fe869fa6734b9c2490da4497d2551ac21c1 Signed-off-by: Ashod Nakashian <ashod.nakashian@collabora.co.uk>
2021-03-13 07:52:16 -06:00
#include <mutex>
2017-03-23 12:14:51 -05:00
/// Simulates network latency for local debugging.
///
/// We are lifecycle managed internally based on the physical /
/// delayFd lifecycle.
wsd: avoid static SocketPoll The lifetime management of static objects is extremely unpredictable and depends on many variables outside of our control or even reliable reproducibility. Complex static objects that own threads and other objects are doubly problematic because of their dependency and/or interaction with other objects. Here we replace the static DelayPoll instance with one we control its lifetime in the LOOLWSD main body, such that it is destroyed properly. Specifically, DelayPoll's dtor was accessing Poco's Logging subsystem out of order. That is, after Poco had been destroyed. Another advantage to this approach is that we don't even create the DelayPoll at all if we don't need it. The onus now is on the user of DelayPoll to make sure they create a Delay object with a long-enough lifetime to encompase it use. For completeness, here is the stacktrace: terminate called after throwing an instance of 'std::bad_alloc' what(): std::bad_alloc Program received signal SIGABRT, Aborted. __GI_raise (sig=sig@entry=6) at ../sysdeps/unix/sysv/linux/raise.c:51 51 ../sysdeps/unix/sysv/linux/raise.c: No such file or directory. (gdb) bt #0 __GI_raise (sig=sig@entry=6) at ../sysdeps/unix/sysv/linux/raise.c:51 #1 0x00007ffff613f801 in __GI_abort () at abort.c:79 #2 0x00007ffff6d51957 in ?? () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6 #3 0x00007ffff6d57ae6 in ?? () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6 #4 0x00007ffff6d56b49 in ?? () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6 #5 0x00007ffff6d574b8 in __gxx_personality_v0 () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6 #6 0x00007ffff671f573 in ?? () from /lib/x86_64-linux-gnu/libgcc_s.so.1 #7 0x00007ffff671fad1 in _Unwind_RaiseException () from /lib/x86_64-linux-gnu/libgcc_s.so.1 #8 0x00007ffff6d57d47 in __cxa_throw () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6 #9 0x00007ffff6d582dc in operator new(unsigned long) () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6 #10 0x00005555556b5927 in void std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::_M_construct<char*>(char*, char*, std::forward_iterator_tag) [clone .isra.41] () #11 0x0000555555982a14 in Poco::Message::Message(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, Poco::Message::Priority) () #12 0x000055555585a909 in SocketPoll::~SocketPoll (this=0x555555d10f60 <DelayPoll>, __in_chrg=<optimized out>) at net/Socket.cpp:145 #13 0x00007ffff6142041 in __run_exit_handlers (status=0, listp=0x7ffff64ea718 <__exit_funcs>, run_list_atexit=run_list_atexit@entry=true, run_dtors=run_dtors@entry=true) at exit.c:108 #14 0x00007ffff614213a in __GI_exit (status=<optimized out>) at exit.c:139 #15 0x0000555555752d78 in LOOLWSD::innerInitialize (this=<optimized out>, self=...) at wsd/LOOLWSD.cpp:1213 #16 0x0000555555788b07 in LOOLWSD::initialize (this=<optimized out>, self=...) at wsd/LOOLWSD.hpp:432 #17 0x00005555558dd7e4 in Poco::Util::Application::run() () #18 0x00005555556b6574 in main (argc=2, argv=0x7fffffffe528) at wsd/LOOLWSD.cpp:4276 Change-Id: Ifda55fe869fa6734b9c2490da4497d2551ac21c1 Signed-off-by: Ashod Nakashian <ashod.nakashian@collabora.co.uk>
2021-03-13 07:52:16 -06:00
///
/// An instance of Delay must be created before using the
/// static members and must outlive all sockets.
class Delay final
2017-03-23 12:14:51 -05:00
{
wsd: avoid static SocketPoll The lifetime management of static objects is extremely unpredictable and depends on many variables outside of our control or even reliable reproducibility. Complex static objects that own threads and other objects are doubly problematic because of their dependency and/or interaction with other objects. Here we replace the static DelayPoll instance with one we control its lifetime in the LOOLWSD main body, such that it is destroyed properly. Specifically, DelayPoll's dtor was accessing Poco's Logging subsystem out of order. That is, after Poco had been destroyed. Another advantage to this approach is that we don't even create the DelayPoll at all if we don't need it. The onus now is on the user of DelayPoll to make sure they create a Delay object with a long-enough lifetime to encompase it use. For completeness, here is the stacktrace: terminate called after throwing an instance of 'std::bad_alloc' what(): std::bad_alloc Program received signal SIGABRT, Aborted. __GI_raise (sig=sig@entry=6) at ../sysdeps/unix/sysv/linux/raise.c:51 51 ../sysdeps/unix/sysv/linux/raise.c: No such file or directory. (gdb) bt #0 __GI_raise (sig=sig@entry=6) at ../sysdeps/unix/sysv/linux/raise.c:51 #1 0x00007ffff613f801 in __GI_abort () at abort.c:79 #2 0x00007ffff6d51957 in ?? () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6 #3 0x00007ffff6d57ae6 in ?? () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6 #4 0x00007ffff6d56b49 in ?? () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6 #5 0x00007ffff6d574b8 in __gxx_personality_v0 () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6 #6 0x00007ffff671f573 in ?? () from /lib/x86_64-linux-gnu/libgcc_s.so.1 #7 0x00007ffff671fad1 in _Unwind_RaiseException () from /lib/x86_64-linux-gnu/libgcc_s.so.1 #8 0x00007ffff6d57d47 in __cxa_throw () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6 #9 0x00007ffff6d582dc in operator new(unsigned long) () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6 #10 0x00005555556b5927 in void std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::_M_construct<char*>(char*, char*, std::forward_iterator_tag) [clone .isra.41] () #11 0x0000555555982a14 in Poco::Message::Message(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, Poco::Message::Priority) () #12 0x000055555585a909 in SocketPoll::~SocketPoll (this=0x555555d10f60 <DelayPoll>, __in_chrg=<optimized out>) at net/Socket.cpp:145 #13 0x00007ffff6142041 in __run_exit_handlers (status=0, listp=0x7ffff64ea718 <__exit_funcs>, run_list_atexit=run_list_atexit@entry=true, run_dtors=run_dtors@entry=true) at exit.c:108 #14 0x00007ffff614213a in __GI_exit (status=<optimized out>) at exit.c:139 #15 0x0000555555752d78 in LOOLWSD::innerInitialize (this=<optimized out>, self=...) at wsd/LOOLWSD.cpp:1213 #16 0x0000555555788b07 in LOOLWSD::initialize (this=<optimized out>, self=...) at wsd/LOOLWSD.hpp:432 #17 0x00005555558dd7e4 in Poco::Util::Application::run() () #18 0x00005555556b6574 in main (argc=2, argv=0x7fffffffe528) at wsd/LOOLWSD.cpp:4276 Change-Id: Ifda55fe869fa6734b9c2490da4497d2551ac21c1 Signed-off-by: Ashod Nakashian <ashod.nakashian@collabora.co.uk>
2021-03-13 07:52:16 -06:00
public:
Delay(std::size_t latencyMs);
~Delay();
static int create(int delayMs, int physicalFd);
static void dumpState(std::ostream &os);
private:
static std::shared_ptr<TerminatingPoll> DelayPoll;
static std::once_flag DelayPollOnceFlag;
2017-03-23 12:14:51 -05:00
};
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */