You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@thrift.apache.org by 刘浪 <86...@qq.com> on 2013/04/15 06:56:47 UTC

How to stop thrift service itself

Recently, I find a Small probability thing in thrift-0.8.0.
it ocurred 
"apache::thrift::concurrency::Mutex::impl::~impl():
Assertion 'ret == 0' failed."

I just want to start thrift service, and than stop thrift service.
This is my example:

template <class EOFST, class EOFSProcessT>
class ncTEOFSServerThread
{
public:
	ncTEOFSServerThread (int port)
		: _tid (0)
		, _server (0)
		, _port (port)
	{
	}

	~ncTEOFSServerThread (void)
	{
		reinterpret_cast<TThreadPoolServer*>(_server)->stop ();
		pthread_join (_tid, NULL);  // Error accur
		_server = 0;
		pthread_cancel (_tid);
	}

	static String CmdSystem (const String& cmd)
	{
		String tRet;
		FILE *fpRead;

		fpRead = ::popen (cmd.getCStr (), _T("r"));

		char str [1024] = {0};
		while (::fgets (str, 1024 - 1, fpRead) != NULL) {
			tRet += String (str);
			memset (str, 0, sizeof(str));
		}

		if (fpRead != NULL)
			::pclose (fpRead);

		return tRet;
	}

	static bool HasBindPort (int port)
	{
		String cmd;
		cmd.format (_T("netstat -anp | egrep \"\\\\b(%s)\\\\b\" | egrep LISTEN"), String::toString (port).getCStr ());
		String cmdRet = CmdSystem (cmd);
		if (cmdRet.isEmpty ())
			return false;
		else
			return true;
	}

	class ncEmptyDeleter
	{
	public:
		void operator() (EOFST*)
		{
		}
	};

	static void *RunFunc (void* args)
	{
		try {
			ncTEOFSServerThread* pThisThread = static_cast <ncTEOFSServerThread*> (args);
			shared_ptr<TProtocolFactory> protocolFactory (new TBinaryProtocolFactory ());
			shared_ptr<EOFST> handler (EOFST::GetInstance (), ncEmptyDeleter ());
			shared_ptr<TProcessor> processor (new EOFSProcessT (handler));
			shared_ptr<TServerTransport> serverTransport (new TServerSocket (pThisThread->_port));
			shared_ptr<TTransportFactory> transportFactory (new TBufferedTransportFactory ());

			shared_ptr<ThreadManager> threadManager = ThreadManager::newSimpleThreadManager (15);
			shared_ptr<PosixThreadFactory> threadFactory = shared_ptr<PosixThreadFactory>(new PosixThreadFactory ());
			threadManager->threadFactory (threadFactory);
			threadManager->start ();

			auto_ptr<TThreadPoolServer> server (new TThreadPoolServer (processor,
																	   serverTransport,
																	   transportFactory,
																	   protocolFactory,
																	   threadManager));

			pThisThread->_server = server.get ();
			server->serve();
		}
		catch (ncTEOFSException& e) {
			printf ("ncTException Error: %s", e.expMsg.c_str());
		}
	}

	void Start (void)
	{
		if (pthread_create (&_tid, NULL, RunFunc, this) != 0)
			throw ncTEOFSException ("Create thread Error.");
		int count = 0;
		bool hasBindPort = false;
		do {
			hasBindPort = HasBindPort (_port);

			if (hasBindPort) {
				break;
			}
			Sleep (100);
			if (++count == 300) {
				break;
			}
		} while (true);

		if (hasBindPort == false) {
			throw ncTEOFSException ("Not Bind");
		}
	}

private:
	pthread_t _tid;
	void* _server;
	int _port;
};


when I do this code:

{
ncTEOFSServerThread<ncTEOFSIfImpl, ncTEOFSProcessor>* thread = new new ncTEOFSServerThread<ncTEOFSIfImpl, ncTEOFSProcessor> (9061);
delete thread;
}

it sometimes (I use shell script run 500 times, and it will occured 3 times.)occured this error:

apache::thrift::concurrency::Mutex::impl::~impl():
Assertion 'ret == 0' failed.

After debugging, I find it ret equals 16. It is EBUSY.
I find that it happened that SimpleThreadManager destructor function.

How can I resolved it. Think you very much.