[complement-svn] SF.net SVN: complement: [1912] branches/complement-sockios/explore
Status: Pre-Alpha
Brought to you by:
complement
From: <com...@us...> - 2008-06-26 05:44:55
|
Revision: 1912 http://complement.svn.sourceforge.net/complement/?rev=1912&view=rev Author: complement Date: 2008-06-25 22:44:53 -0700 (Wed, 25 Jun 2008) Log Message: ----------- Under debugging; Detected problem use case: after close fd next assigned fd may has same number. Suggestion: clean close_quere ASAP, and remove appropriate fd from epoll's vector too. Modified Paths: -------------- branches/complement-sockios/explore/include/sockios/socksrv.cc branches/complement-sockios/explore/include/sockios/socksrv.h branches/complement-sockios/explore/include/sockios/sockstream branches/complement-sockios/explore/include/sockios/sockstream.cc branches/complement-sockios/explore/include/sockios/sp.cc branches/complement-sockios/explore/include/sockios/sp.h branches/complement-sockios/explore/lib/sockios/ut/sockios2_test.cc Modified: branches/complement-sockios/explore/include/sockios/socksrv.cc =================================================================== --- branches/complement-sockios/explore/include/sockios/socksrv.cc 2008-06-26 05:44:26 UTC (rev 1911) +++ branches/complement-sockios/explore/include/sockios/socksrv.cc 2008-06-26 05:44:53 UTC (rev 1912) @@ -1,4 +1,4 @@ -// -*- C++ -*- Time-stamp: <08/06/16 20:25:28 yeti> +// -*- C++ -*- Time-stamp: <08/06/17 14:54:16 yeti> /* * Copyright (c) 2008 @@ -205,16 +205,20 @@ { typename base_t::sockstream_t* s = base_t::create_stream( fd, addr ); + if ( s == 0 ) { + return 0; + } + Connect* c = new Connect( *s ); // bad point! I can't read from s in ctor indeed! - if ( s->rdbuf()->in_avail() ) { - std::tr2::lock_guard<std::tr2::mutex> lk( rdlock ); - ready_pool.push_back( processor( c, s ) ); - cnd.notify_one(); - } else { - std::tr2::lock_guard<std::tr2::mutex> lk( wklock ); - worker_pool.insert( std::make_pair( fd, processor( c, s ) ) ); - } + // if ( s->rdbuf()->in_avail() ) { + // std::tr2::lock_guard<std::tr2::mutex> lk( rdlock ); + // ready_pool.push_back( processor( c, s ) ); + // cnd.notify_one(); + // } else { + std::tr2::lock_guard<std::tr2::mutex> lk( wklock ); + worker_pool.insert( std::make_pair( fd, processor( c, s ) ) ); + // } return s->rdbuf(); } Modified: branches/complement-sockios/explore/include/sockios/socksrv.h =================================================================== --- branches/complement-sockios/explore/include/sockios/socksrv.h 2008-06-26 05:44:26 UTC (rev 1911) +++ branches/complement-sockios/explore/include/sockios/socksrv.h 2008-06-26 05:44:53 UTC (rev 1912) @@ -1,4 +1,4 @@ -// -*- C++ -*- Time-stamp: <08/06/17 09:17:05 ptr> +// -*- C++ -*- Time-stamp: <08/06/17 15:44:44 yeti> /* * Copyright (c) 2008 @@ -109,7 +109,9 @@ sockstream_t* create_stream( int fd, const sockaddr& addr ) { sockstream_t* s = new sockstream_t(); - s->rdbuf()->_open_sockmgr( fd, addr ); + if ( s != 0 ) { + s->rdbuf()->_open_sockmgr( fd, addr ); + } return s; } @@ -197,7 +199,11 @@ { std::tr2::lock_guard<std::tr2::mutex> lk2( wklock ); - cerr << __FILE__ << ":" << __LINE__ << " " << worker_pool.size() << endl; + cerr << __FILE__ << ":" << __LINE__ << " " << worker_pool.size() << endl; + for ( typename worker_pool_t::iterator i = worker_pool.begin(); i != worker_pool.end(); ++i ) { + delete i->second.c; + delete i->second.s; + } } ((Init *)Init_buf)->~Init(); Modified: branches/complement-sockios/explore/include/sockios/sockstream =================================================================== --- branches/complement-sockios/explore/include/sockios/sockstream 2008-06-26 05:44:26 UTC (rev 1911) +++ branches/complement-sockios/explore/include/sockios/sockstream 2008-06-26 05:44:53 UTC (rev 1912) @@ -1,4 +1,4 @@ -// -*- C++ -*- Time-stamp: <08/06/15 17:11:29 ptr> +// -*- C++ -*- Time-stamp: <08/06/17 17:09:45 yeti> /* * Copyright (c) 1997-1999, 2002, 2003, 2005-2008 @@ -448,8 +448,10 @@ virtual ~basic_sockbuf() { + std::cerr << __FILE__ << ":" << __LINE__ << " " << basic_socket_t::_fd << std::endl; close(); _M_deallocate_block(); + std::cerr << __FILE__ << ":" << __LINE__ << " " << basic_socket_t::_fd << std::endl; } sockbuf_type *open( const char *hostname, int port, Modified: branches/complement-sockios/explore/include/sockios/sockstream.cc =================================================================== --- branches/complement-sockios/explore/include/sockios/sockstream.cc 2008-06-26 05:44:26 UTC (rev 1911) +++ branches/complement-sockios/explore/include/sockios/sockstream.cc 2008-06-26 05:44:53 UTC (rev 1912) @@ -1,4 +1,4 @@ -// -*- C++ -*- Time-stamp: <08/06/11 14:47:58 yeti> +// -*- C++ -*- Time-stamp: <08/06/17 17:09:02 yeti> /* * Copyright (c) 1997-1999, 2002, 2003, 2005-2008 @@ -392,19 +392,26 @@ template<class charT, class traits, class _Alloc> int basic_sockbuf<charT, traits, _Alloc>::sync() { + std::cerr << __FILE__ << ":" << __LINE__ << std::endl; if ( !basic_socket_t::is_open() ) { return -1; } long count = this->pptr() - this->pbase(); if ( count ) { + std::cerr << __FILE__ << ":" << __LINE__ << std::endl; // _STLP_ASSERT( this->pbase() != 0 ); count *= sizeof(charT); long start = 0; while ( count > 0 ) { long offset = (this->*_xwrite)( this->pbase() + start, count ); + std::cerr << __FILE__ << ":" << __LINE__ << std::endl; if ( offset < 0 ) { - if ( errno == EAGAIN ) { + std::cerr << __FILE__ << ":" << __LINE__ << std::endl; + if ( errno == EINTR ) { + errno = 0; + continue; + } else if ( errno == EAGAIN ) { pollfd wpfd; wpfd.fd = basic_socket_t::_fd; wpfd.events = POLLOUT | POLLHUP | POLLWRNORM; @@ -412,6 +419,7 @@ while ( poll( &wpfd, 1, basic_socket_t::_use_wrtimeout ? basic_socket_t::_wrtimeout.count() : -1 ) <= 0 ) { // wait infinite if ( errno == EINTR ) { // may be interrupted, check and ignore errno = 0; + // reduce timeout? continue; } return -1; @@ -427,6 +435,7 @@ return -1; } } + std::cerr << __FILE__ << ":" << __LINE__ << " " << basic_socket_t::_fd << std::endl; count -= offset; start += offset; } Modified: branches/complement-sockios/explore/include/sockios/sp.cc =================================================================== --- branches/complement-sockios/explore/include/sockios/sp.cc 2008-06-26 05:44:26 UTC (rev 1911) +++ branches/complement-sockios/explore/include/sockios/sp.cc 2008-06-26 05:44:53 UTC (rev 1912) @@ -1,4 +1,4 @@ -// -*- C++ -*- Time-stamp: <08/06/17 13:10:39 yeti> +// -*- C++ -*- Time-stamp: <08/06/17 17:41:00 yeti> /* * Copyright (c) 2008 @@ -116,8 +116,21 @@ ev_add.events = EPOLLIN | EPOLLRDHUP | EPOLLERR | EPOLLHUP | EPOLLET | EPOLLONESHOT; ev_add.data.fd = static_cast<sockbuf_t*>(_ctl.data.ptr)->fd(); if ( ev_add.data.fd >= 0 ) { - fd_info new_info = { fd_info::buffer, static_cast<sockbuf_t*>(_ctl.data.ptr), 0 }; + fd_info new_info = { 0, static_cast<sockbuf_t*>(_ctl.data.ptr), 0 }; descr[ev_add.data.fd] = new_info; + + std::tr2::lock_guard<std::tr2::mutex> lck( cll ); + + typename fd_container_type::iterator closed_ifd = closed_queue.find( ev_add.data.fd ); + if ( closed_ifd != closed_queue.end() ) { // reuse same fd? + closed_queue.erase( closed_ifd ); + if ( epoll_ctl( efd, EPOLL_CTL_DEL, ev_add.data.fd, 0 ) < 0 ) { + // throw system_error + std::cerr << __FILE__ << ":" << __LINE__ << std::endl; + } + // descr.erase( ifd ); + } + if ( epoll_ctl( efd, EPOLL_CTL_ADD, ev_add.data.fd, &ev_add ) < 0 ) { descr.erase( ev_add.data.fd ); // throw system_error @@ -164,19 +177,19 @@ return; // I don't know what to do this case... } - { - std::tr2::lock_guard<std::tr2::mutex> lck( cll ); - typename fd_container_type::iterator closed_ifd = closed_queue.find( ev.data.fd ); - if ( closed_ifd != closed_queue.end() && closed_ifd->second.p == ifd->second.p ) { - // listener in process of closing, ignore all incoming connects - closed_queue.erase( closed_ifd ); - if ( epoll_ctl( efd, EPOLL_CTL_DEL, ifd->first, 0 ) < 0 ) { - // throw system_error - } - descr.erase( ifd ); - return; - } + // { + std::tr2::lock_guard<std::tr2::mutex> lck( cll ); + for ( typename fd_container_type::iterator closed_ifd = closed_queue.begin(); closed_ifd != closed_queue.end(); ++closed_ifd ) { + if ( epoll_ctl( efd, EPOLL_CTL_DEL, closed_ifd->first, 0 ) < 0 ) { + // ignore + } + if ( closed_ifd->first == ifd->first ) { + std::cerr << __FILE__ << ":" << __LINE__ << std::endl; + } + descr.erase( closed_ifd->first ); } + closed_queue.clear(); + // at this point closed queue empty sockaddr addr; socklen_t sz = sizeof( sockaddr_in ); @@ -189,11 +202,13 @@ // std::cerr << "Accept, listener # " << ev.data.fd << ", errno " << errno << std::endl; std::cerr << __FILE__ << ":" << __LINE__ /* << " " << std::tr2::getpid() */ << std::endl; if ( (errno == EINTR) || (errno == ECONNABORTED) /* || (errno == ERESTARTSYS) */ ) { + std::cerr << __FILE__ << ":" << __LINE__ << std::endl; errno = 0; continue; } if ( !(errno == EAGAIN /* || errno == EWOULDBLOCK */ ) ) { // EWOULDBLOCK == EAGAIN // std::cerr << "Accept, listener " << ev.data.fd << ", errno " << errno << std::endl; + std::cerr << __FILE__ << ":" << __LINE__ << std::endl; if ( epoll_ctl( efd, EPOLL_CTL_DEL, ifd->first, 0 ) < 0 ) { std::cerr << __FILE__ << ":" << __LINE__ << std::endl; // throw system_error @@ -211,7 +226,7 @@ descr.erase( ifd ); // check closed_queue, due to ifd->second.p->close(); add record in it - std::tr2::lock_guard<std::tr2::mutex> lck( cll ); +// std::tr2::lock_guard<std::tr2::mutex> lck( cll ); typename fd_container_type::iterator closed_ifd = closed_queue.find( ev.data.fd ); if ( closed_ifd != closed_queue.end() && closed_ifd->second.p == ifd->second.p ) { // listener in process of close @@ -237,20 +252,23 @@ } try { - std::cerr << __FILE__ << ":" << __LINE__ << std::endl; - sockbuf_t* b = (*info.p)( fd, addr ); - std::cerr << __FILE__ << ":" << __LINE__ << std::endl; - epoll_event ev_add; ev_add.events = EPOLLIN | EPOLLRDHUP | EPOLLERR | EPOLLHUP | EPOLLET | EPOLLONESHOT; ev_add.data.fd = fd; - fd_info new_info = { fd_info::owner, b, info.p }; - descr[fd] = new_info; if ( epoll_ctl( efd, EPOLL_CTL_ADD, fd, &ev_add ) < 0 ) { descr.erase( fd ); // throw system_error + std::cerr << __FILE__ << ":" << __LINE__ << std::endl; + return; // throw } + + std::cerr << __FILE__ << ":" << __LINE__ << std::endl; + sockbuf_t* b = (*info.p)( fd, addr ); + std::cerr << __FILE__ << ":" << __LINE__ << std::endl; + + fd_info new_info = { 0, b, info.p }; + descr[fd] = new_info; } catch ( const std::bad_alloc& ) { // nothing @@ -265,53 +283,45 @@ template<class charT, class traits, class _Alloc> void sockmgr<charT,traits,_Alloc>::process_regular( epoll_event& ev, typename sockmgr<charT,traits,_Alloc>::fd_container_type::iterator ifd ) { + std::cerr << __FILE__ << ":" << __LINE__ << std::endl; + fd_info& info = ifd->second; std::tr2::lock_guard<std::tr2::mutex> lck( cll ); - typename fd_container_type::iterator closed_ifd = closed_queue.find( ev.data.fd ); - if ( closed_ifd != closed_queue.end() ) { - closed_queue.erase( closed_ifd ); + + for ( typename fd_container_type::iterator closed_ifd = closed_queue.begin(); closed_ifd != closed_queue.end(); ++closed_ifd ) { + if ( epoll_ctl( efd, EPOLL_CTL_DEL, closed_ifd->first, 0 ) < 0 ) { + // ignore + } + if ( closed_ifd->first == ifd->first ) { + std::cerr << __FILE__ << ":" << __LINE__ << std::endl; + } + descr.erase( closed_ifd->first ); + } + closed_queue.clear(); + // at this point closed queue empty + + sockbuf_t* b = info.b; + if ( b == 0 ) { // marginal case: sockbuf wasn't created by processor... if ( epoll_ctl( efd, EPOLL_CTL_DEL, ifd->first, 0 ) < 0 ) { // throw system_error } + if ( info.p != 0 ) { // ... but controlled by processor + (*info.p)( ifd->first, typename socks_processor_t::adopt_close_t() ); + } descr.erase( ifd ); + std::cerr << __FILE__ << ":" << __LINE__ << std::endl; return; } + errno = 0; + + std::cerr << __FILE__ << ":" << __LINE__ << std::endl; if ( ev.events & EPOLLIN ) { -#if 0 - if ( (info.flags & fd_info::owner) == 0 ) { - /* - marginal case: sockmgr isn't owner (registerd via push(), - when I owner, I know destroy point), - already closed, but I don't see closed event yet; - object may be deleted already, so I can't - call b->egptr() etc. here - */ - std::tr2::lock_guard<std::tr2::mutex> lck( cll ); - typename fd_container_type::iterator closed_ifd = closed_queue.find( ev.data.fd ); - if ( closed_ifd != closed_queue.end() ) { - closed_queue.erase( closed_ifd ); - if ( epoll_ctl( efd, EPOLL_CTL_DEL, ifd->first, 0 ) < 0 ) { - // throw system_error - } - descr.erase( ifd ); - return; - } - } -#endif - - sockbuf_t* b = info.b; - errno = 0; - if ( b == 0 ) { // marginal case: sockbuf wasn't created by processor... - if ( info.p != 0 ) { // ... but controlled by processor - (*info.p)( ifd->first, typename socks_processor_t::adopt_close_t() ); - } - descr.erase( ifd ); - return; - } + std::cerr << __FILE__ << ":" << __LINE__ << std::endl; for ( ; ; ) { if ( b->_ebuf == b->egptr() ) { + std::cerr << __FILE__ << ":" << __LINE__ << std::endl; // process extract data from buffer too slow for us! if ( (info.flags & fd_info::level_triggered) == 0 ) { epoll_event xev; @@ -332,6 +342,7 @@ long offset = read( ev.data.fd, b->egptr(), sizeof(charT) * (b->_ebuf - b->egptr()) ); // std::cerr << "offset " << offset << ", " << errno << std::endl; if ( offset < 0 ) { + std::cerr << __FILE__ << ":" << __LINE__ << std::endl; switch ( errno ) { case EINTR: // read was interrupted errno = 0; @@ -339,10 +350,12 @@ break; case EFAULT: // Bad address case ECONNRESET: // Connection reset by peer + errno = 0; ev.events |= EPOLLRDHUP; // will be processed below break; case EAGAIN: // case EWOULDBLOCK: + errno = 0; { epoll_event xev; xev.events = EPOLLIN | EPOLLRDHUP | EPOLLERR | EPOLLHUP | EPOLLET | EPOLLONESHOT; @@ -352,10 +365,12 @@ break; default: // std::cerr << "not listener, other " << ev.data.fd << std::hex << ev.events << std::dec << " : " << errno << std::endl; + std::cerr << __FILE__ << ":" << __LINE__ << std::endl; break; } break; } else if ( offset > 0 ) { + std::cerr << __FILE__ << ":" << __LINE__ << std::endl; offset /= sizeof(charT); // if offset % sizeof(charT) != 0, rest will be lost! if ( info.flags & fd_info::level_triggered ) { @@ -371,9 +386,11 @@ b->setg( b->eback(), b->gptr(), b->egptr() + offset ); b->ucnd.notify_one(); if ( info.p != 0 ) { + std::cerr << __FILE__ << ":" << __LINE__ << " " << ev.data.fd << std::endl; (*info.p)( ev.data.fd ); } } else { + std::cerr << __FILE__ << ":" << __LINE__ << std::endl; // std::cerr << "K " << ev.data.fd << ", " << errno << std::endl; // EPOLLRDHUP may be missed in kernel, but offset 0 is the same ev.events |= EPOLLRDHUP; // will be processed below @@ -384,15 +401,18 @@ if ( (ev.events & (EPOLLRDHUP | EPOLLHUP | EPOLLERR) ) != 0 ) { // std::cerr << "Poll EPOLLRDHUP " << ev.data.fd << ", " << errno << std::endl; + std::cerr << __FILE__ << ":" << __LINE__ << std::endl; if ( epoll_ctl( efd, EPOLL_CTL_DEL, ifd->first, 0 ) < 0 ) { // throw system_error + std::cerr << __FILE__ << ":" << __LINE__ << std::endl; } if ( info.p != 0 ) { + std::cerr << __FILE__ << ":" << __LINE__ << std::endl; (*info.p)( ifd->first, typename socks_processor_t::adopt_close_t() ); - } /* else */ - if ( (info.flags & fd_info::buffer) != 0 ) { - info.b->close(); + } else { + std::cerr << __FILE__ << ":" << __LINE__ << std::endl; + b->close(); } // std::tr2::lock_guard<std::tr2::mutex> lck( cll ); closed_queue.erase( ev.data.fd ); Modified: branches/complement-sockios/explore/include/sockios/sp.h =================================================================== --- branches/complement-sockios/explore/include/sockios/sp.h 2008-06-26 05:44:26 UTC (rev 1911) +++ branches/complement-sockios/explore/include/sockios/sp.h 2008-06-26 05:44:53 UTC (rev 1912) @@ -1,4 +1,4 @@ -// -*- C++ -*- Time-stamp: <08/06/15 23:21:50 ptr> +// -*- C++ -*- Time-stamp: <08/06/17 15:09:45 yeti> /* * Copyright (c) 2008 @@ -87,9 +87,7 @@ { enum { listener = 0x1, - level_triggered = 0x2, - owner = 0x4, - buffer = 0x8 + level_triggered = 0x2 }; unsigned flags; Modified: branches/complement-sockios/explore/lib/sockios/ut/sockios2_test.cc =================================================================== --- branches/complement-sockios/explore/lib/sockios/ut/sockios2_test.cc 2008-06-26 05:44:26 UTC (rev 1911) +++ branches/complement-sockios/explore/lib/sockios/ut/sockios2_test.cc 2008-06-26 05:44:53 UTC (rev 1912) @@ -1,4 +1,4 @@ -// -*- C++ -*- Time-stamp: <08/06/17 13:47:21 yeti> +// -*- C++ -*- Time-stamp: <08/06/17 16:35:39 yeti> /* * This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |