facebook / wangle

Wangle is a framework providing a set of common client/server abstractions for building services in a consistent, modular, and composable way.
Apache License 2.0
3.04k stars 536 forks source link

separate IOThreadPool for Echo example. #45

Open ghost opened 8 years ago

ghost commented 8 years ago

trying to test the separate IOThreadPool. Patch like below for EchoClient.cpp std::shared_ptrwangle::IOThreadPoolExecutor ioPool = std::make_sharedwangle::IOThreadPoolExecutor(2); std::shared_ptr pipeFact = std::make_shared();

ClientBootstrap client; client.group(ioPool); client.pipelineFactory(pipeFact);

But when EchoClient exits, assert error showed up. I0423 21:57:13.939635 54128 AsyncSocket.cpp:519] AsyncSocket::setReadCallback() this=0x7f2b88001950, fd=11, callback=0, state=2 EchoClient: io/async/AsyncSocket.cpp:558: virtual void folly::AsyncSocket::setReadCB(folly::AsyncTransportWrapper::ReadCallback*): Assertion `eventBase_->isInEventBaseThread()' failed.

Looks Pipeline/AsyncSocket destruction was called by the main thread, that was why AsyncSocket assert.

Added the patch to ClientBootstrap to avoid this assert. Is it the correct way to handle it?

Basically the patch kept the EventBase that was selected to setup the socket with server at connect() in ClientBootstrap class. And destruct pipeline in the EventBase thread in ~ClientBootstrap. virtual ~ClientBootstrap() { if (base && group.get() && pipeline) { // Pipeline was already created, destroy from the EventBase thread // as AsyncSocket destruct could only be called from its EventBase thread VLOG(5) << " ~ClientBootstrap " << this << ", destruct pipeline in EventBase thread"; base->runImmediatelyOrRunInEventBaseThreadAndWait([&](){ pipeline.reset(); }); } };

djwatson commented 8 years ago

Yea this has come up once or twice - I think the best way is use EventBaseHandler, and on detach use runImmediatlyOrRunIneventbasethread