0

I use Poco-libraries parallel socket acceptor in my application and it sometimes crashes. Here is the backtrace of my application:

Program terminated with signal SIGABRT, Aborted.
#0  0x00007f9ed30ee107 in __GI_raise (sig=sig@entry=6) at ../nptl/sysdeps/unix/sysv/linux/raise.c:56
56      ../nptl/sysdeps/unix/sysv/linux/raise.c: No such file or directory.
(gdb) bt
#0  0x00007f9ed30ee107 in __GI_raise (sig=sig@entry=6) at ../nptl/sysdeps/unix/sysv/linux/raise.c:56
#1  0x00007f9ed30ef4e8 in __GI_abort () at abort.c:89
#2  0x00007f9ed312c044 in __libc_message (do_abort=do_abort@entry=1, 
    fmt=fmt@entry=0x7f9ed321ec60 "*** Error in `%s': %s: 0x%s ***\n") at ../sysdeps/posix/libc_fatal.c:175
#3  0x00007f9ed313181e in malloc_printerr (action=1, str=0x7f9ed321f000 "malloc(): memory corruption (fast)", 
    ptr=<optimized out>) at malloc.c:4996
#4  0x00007f9ed3133bbb in _int_malloc (av=0x7f9ecc000020, bytes=32) at malloc.c:3359
#5  0x00007f9ed3134eb0 in __GI___libc_malloc (bytes=32) at malloc.c:2891
#6  0x00007f9ed39d82e8 in operator new(unsigned long) () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#7  0x0000000000471058 in Poco::Net::ParallelSocketAcceptor<BFSTcpServiceHandler, Poco::Net::SocketReactor>::createServiceHandler (this=0x7f9ed0e17d70, socket=...) at /usr/local/include/Poco/Net/ParallelSocketAcceptor.h:172
#8  0x00000000004709d2 in Poco::Net::ParallelSocketAcceptor<BFSTcpServiceHandler, Poco::Net::SocketReactor>::onAccept
    (this=0x7f9ed0e17d70, pNotification=0x7f9ecc0009c0) at /usr/local/include/Poco/Net/ParallelSocketAcceptor.h:160
#9  0x0000000000472bfe in Poco::Observer<Poco::Net::ParallelSocketAcceptor<BFSTcpServiceHandler, Poco::Net::SocketReactor>, Poco::Net::ReadableNotification>::notify (this=0x7f9ecc001d20, pNf=0x7f9ecc0009c0)
    at /usr/local/include/Poco/Observer.h:86
#10 0x00007f9ed4709c4b in Poco::NotificationCenter::postNotification(Poco::AutoPtr<Poco::Notification>) ()
   from /usr/local/lib/libPocoFoundation.so.30
#11 0x00007f9ed43c6630 in Poco::Net::SocketNotifier::dispatch(Poco::Net::SocketNotification*) ()
   from /usr/local/lib/libPocoNet.so.30
#12 0x00007f9ed43c38a4 in Poco::Net::SocketReactor::dispatch(Poco::AutoPtr<Poco::Net::SocketNotifier>&, Poco::Net::SocketNotification*) () from /usr/local/lib/libPocoNet.so.30
#13 0x00007f9ed43c3d1b in Poco::Net::SocketReactor::dispatch(Poco::Net::Socket const&, Poco::Net::SocketNotification*)
    () from /usr/local/lib/libPocoNet.so.30
#14 0x00007f9ed43c4910 in Poco::Net::SocketReactor::run() () from /usr/local/lib/libPocoNet.so.30
#15 0x000000000046a8dc in BFSTcpServer::run () at src/BFSTcpServer.cpp:69
#16 0x0000000000459c1b in std::_Bind_simple<void (*())()>::_M_invoke<>(std::_Index_tuple<>) (this=0x1ee8d38)
    at /usr/include/c++/4.9/functional:1700
---Type <return> to continue, or q <return> to quit---
#17 0x0000000000459b63 in std::_Bind_simple<void (*())()>::operator()() (this=0x1ee8d38)
    at /usr/include/c++/4.9/functional:1688
#18 0x0000000000459ae0 in std::thread::_Impl<std::_Bind_simple<void (*())()> >::_M_run() (this=0x1ee8d20)
    at /usr/include/c++/4.9/thread:115
#19 0x00007f9ed3a2f970 in ?? () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#20 0x00007f9ed4eaa0a4 in start_thread (arg=0x7f9ed0e18700) at pthread_create.c:309
#21 0x00007f9ed319eccd in clone () at ../sysdeps/unix/sysv/linux/x86_64/clone.S:111

I use the socket acceptor like this:

...
ServerSocket serverSocket(port);
reactor = new SocketReactor();
ParallelSocketAcceptor<BFSTcpServiceHandler,SocketReactor> acceptor(serverSocket, *reactor);    
reactor->run();
...

And my servicehandler class is as follows:

class BFSTcpServiceHandler {
  Poco::Net::StreamSocket socket;
  Poco::Net::SocketReactor& reactor;
...
  void onReadable(const Poco::AutoPtr<Poco::Net::ReadableNotification>& pNf);
...
public:
  BFSTcpServiceHandler(Poco::Net::StreamSocket& _socket,
        Poco::Net::SocketReactor& _reactor);
  virtual ~BFSTcpServiceHandler();
};
//And implementation:
BFSTcpServiceHandler::BFSTcpServiceHandler(StreamSocket& _socket,
    SocketReactor& _reactor): socket(_socket),reactor(_reactor) {

  //Register Callbacks
  reactor.addEventHandler(socket, NObserver<BFSTcpServiceHandler,
    ReadableNotification>(*this, &BFSTcpServiceHandler::onReadable));
}

BFSTcpServiceHandler::~BFSTcpServiceHandler() {
  //Unregister Callbacks
  reactor.removeEventHandler(socket, NObserver<BFSTcpServiceHandler,
    ReadableNotification>(*this, &BFSTcpServiceHandler::onReadable));

  //Close socket
  try {
    socket.shutdown();
    socket.close();
  }catch(Exception &e){
    LOG(ERROR)<<"ERROR IN CLOSING CONNECTION";
  }
}

void BFSTcpServiceHandler::onReadable(
    const Poco::AutoPtr<Poco::Net::ReadableNotification>& pNf) {
...
    int read = socket.receiveBytes(_packet,sizeof(reqPacket.opCode));
...
  //connection is served just close it!
  delete this;
}

I am not sure if this is a bug in poco or my program. Either way, I will highly appreciate any comment or help. You can take a look at the full source of mine here:

https://github.com/bshafiee/BFS/blob/master/src/BFSTcpServer.cpp#L57

https://github.com/bshafiee/BFS/blob/master/src/BFSTcpServiceHandler.cpp#L65

Compiler Info: gcc (Debian 4.9.1-19) 4.9.1 Poco Verision: 1.6.0 (2014-12-22)

Behrooz
  • 468
  • 5
  • 14
  • 1
    It is clear from the stack trace that the memory is corrupted and new throws on handler creation. I recommend to run it in [valgrind](http://valgrind.org/) or [Dr Memory](http://www.drmemory.org/), it will likely show you precisely where the memory is being corrupted. If you trim down your code to small, copy/paste/compile example reproducing the behavior, I will look into it. I won't claim it is impossible that this is Poco bug, but I have deployed production-grade, long-running Poco::ParallelSocketReactor without problems on Ubuntu. – Alex Apr 02 '15 at 00:05
  • Thanks Alex, for tip. I will try it with Valgrind or Dr Memory. – Behrooz Apr 02 '15 at 18:28

0 Answers0