Skip to content

EventHandler not work after use eb->runInEventBaseThread #35

Closed

Description

@junstor

In Duplex Server. I use server->setServerEventHandler function to add a EventHandler. I want to do some work after a connection destroyed. But if I use eb->runInEventBaseThread to push a msg to the client, the EventHandler will not work after that(maybe after the HeaderClientChannel::sendRequest is called). How can I make it work?

Activity

  1. alandau commented on Sep 29, 2014

    @alandau
    Contributor

    When you say 'Duplex server', do you mean one set up with ->setDuplex(true)?
    Are you running eb->runInEventBaseThread on the server-side server or on the client-side server? Can you provide some example code showing what you are doing and what is not working?

  2. junstor commented on Sep 30, 2014

    @junstor
    Author

    This is my example code :

    class TestServerInterface : public TestServerSvIf {
    
        void async_tm_test(std::unique_ptr<apache::thrift::HandlerCallback<bool>> callback,
                                       int64_t id) override {
            auto callbackp = callback.release();
            auto ctx = callbackp->getConnectionContext()->getConnectionContext();
            auto client = ctx->getDuplexClient<DuplexClientAsyncClient>();
            auto eb = callbackp->getEventBase();
            eb->runInEventBaseThread([=]() mutable {
                client->test_someting([](ClientReceiveState&& state) {
                    bool res = TestClientAsyncClient::recv_test_something(state);
                }, "teststr");
            });
            callbackp->resultInThread(true);
        }
    };
    
    class TestServerEventHandler
        : public server::TServerEventHandler
        , public TProcessorEventHandler
        , public TProcessorEventHandlerFactory
        , public std::enable_shared_from_this<TestServerEventHandler> {
    
    public:
    
        std::shared_ptr<TProcessorEventHandler> getEventHandler() {
            return shared_from_this();
        }
    
        void connectionDestroyed(TConnectionContext* ctx) {
            std::cout << "aaaaaaa" << std::endl;
        }
    };
    
    // create a server .......
    
    server->setDuplex(true);
    server->setInterface(std::unique_ptr<TestServerInterface>(new TestServerInterface));
    
    auto serverHandler = std::make_shared<TestServerEventHandler>(); 
    TProcessorBase::addProcessorEventHandlerFactory(serverHandler);
    server->setServerEventHandler(serverHandler);
    
    server->serve();

    I set a event handler and print "aaaa" when a connection destroyed. But I found that if a client connected and run the server's "async_tm_test" function, then the client close the connection, the TestServerEventHandler will not work, no "aaaa" printed.

    Now, I use this code : (I defined a new callback and setCloseCallback in function "async_tm_test")

    client->getChannel()->setCloseCallback(new NewCloseCallback());

    When the client close the connection, the new callback function works.

    So I don't know what is the difference between "server->setServerEventHandler" and "client->getChannel()->setCloseCallback", and why the setServerEventHandler doesn't work before.

    Thanks.

  3. alandau commented on Sep 30, 2014

    @alandau
    Contributor

    Thanks for the detailed description. I was able to repro and fix.

    The issue was that the server didn't close the connection when the client closed (so it remained half-open). Since the server's end of the connection remained open, the connectionDestroyed() event didn't fire. It would fire after an idle timeout though. I fixed this and it should appear on github at the next sync.

    As for setCloseCallback(), it is firing on the client. Since the client DID close its end of the connection, the event was firing.

  4. alandau commented on Oct 1, 2014

    @alandau
    Contributor

    Fixed in 4f8a83c

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions