Poco StreamSocket segfault
StreamSocket
class within Poco 1.4.2p1 library is not thread safe when used under Linux, because
gethostbyname()
is not thread-safe under Linux. For instance, if a client like
class Client : public Runnable { public: void run() { StreamSocket client(SocketAddress("localhost:7000")); } }; int main() { int counter = 0; while (counter++ < 500) { ThreadPool pool(1, 10); Client c; try { for (int j = 1; j <= 10; j++) pool.start(c); pool.joinAll(); } catch (NoThreadAvailableException& exc) { cout << "main(): NoThreadAvailableException=" << exc.what() << ", pool.used()=" << pool.used() << endl; } catch (Exception& exc) { cout << "main(): Exception=" << exc.what() << ", pool.used()=" << pool.used() << endl; } cout << "main(): finishing: counter=" << counter << endl; } return EXIT_SUCCESS; }
Program received signal SIGSEGV, Segmentation fault. [Switching to Thread 0x7ffff5f9d700 (LWP 3553)] 0x00007ffff664bae1 in __strlen_sse2 () from /lib64/libc.so.6 (gdb) bt #0 0x00007ffff664bae1 in __strlen_sse2 () from /lib64/libc.so.6 #1 0x00007ffff6ea5e20 in std::basic_string<char, std::char_traits<char>, std::allocator<char> >::basic_string(char const*, std::allocator<char> const&) () from /usr/lib64/libstdc++.so.6 #2 0x00007ffff770e6cb in Poco::Net::HostEntry::HostEntry (this=0x7ffff5f9cbd0, entry=0x7ffff696cea0) at src/HostEntry.cpp:61 #3 0x00007ffff770a603 in Poco::Net::DNS::hostByName (hostname=...) at src/DNS.cpp:104 #4 0x00007ffff7716b04 in Poco::Net::SocketAddress::init (this=0x7ffff5f9cd50, host=..., port=7000) at src/SocketAddress.cpp:385 #5 0x00007ffff7716422 in Poco::Net::SocketAddress::SocketAddress (this=0x7ffff5f9cd50, hostAndPort=...) at src/SocketAddress.cpp:273 #6 0x00000000004015cd in Client::run (this=0x7fffffffdd30) at client.cpp:42 #7 0x00007ffff7b1037d in Poco::PooledThread::run (this=0x7ffff0001970) at src/ThreadPool.cpp:215 #8 0x00007ffff7b0da03 in Poco::ThreadImpl::runnableEntry (pThread=0x7ffff0001998) at src/Thread_POSIX.cpp:352 #9 0x00007ffff63b1d6b in start_thread () from /lib64/libpthread.so.0 #10 0x00007ffff66ae75d in clone () from /lib64/libc.so.6
DNS.cpp
shows that there is no alias addresses:
(gdb) print he->h_name $1 = 0x61ed4b "localhost" (gdb) print he->h_aliases $2 = (char **) 0x61ed58 (gdb) print *(he->h_aliases) $3 = 0x0
HostEntry::HostEntry(struct hostent* entry)
.
This causes the segmentation fault shown above.
FreeBSD does not suffer of this issue.