Hung connections

RCF support and general discussion.
Post Reply
Raider
Posts: 12
Joined: Tue Mar 26, 2013 2:55 pm

Hung connections

Post by Raider »

I'm having a huge trouble with hung connections on application running 24/7. Here is the short example to reproduce the problem.

Server.cpp

Code: Select all

#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
#include <RCF/RCF.hpp>
#include "MyService.hpp"

class Session
{
public:
	Session()
	{
		std::cout << "+ Session #" << this << std::endl;
	}

	~Session()
	{
		std::cout << "- Session #" << this << std::endl;
	}
};

class MyServiceImpl
{
public:
    // Reverses the order of strings in the vector.
    void reverse(std::vector<std::string> &v)
    {
        RCF::getCurrentRcfSession().getSessionObject<Session>(true);

        std::cout << "Reversing a vector of strings...\n";
        std::vector<std::string> w;
        std::copy(v.rbegin(), v.rend(), std::back_inserter(w));
        v.swap(w);
    }
};

int main()
{
    RCF::RcfInitDeinit rcfInit;

    // Start a TCP server on port 50001, and expose MyServiceImpl.
    MyServiceImpl myServiceImpl;
    RCF::RcfServer server( RCF::TcpEndpoint("0.0.0.0", 50001) );
    server.bind<MyService>(myServiceImpl);
    server.start();

    std::cout << "Press Enter to exit..." << std::endl;
    std::cin.get();

    return 0;
}
Client.cpp

Code: Select all

#include <algorithm>
#include <iostream>
#include <iterator>
#include <string>
#include <vector>

#include "MyService.hpp"

#include <RCF/RCF.hpp>

int main()
{
    RCF::RcfInitDeinit rcfInit;

    try
    {
        // Setup a vector of strings.
        std::vector<std::string> v;
        v.push_back("one");
        v.push_back("two");
        v.push_back("three");

        // Print them out.
        std::cout << "Before:\n";
        std::copy(
            v.begin(), 
            v.end(), 
            std::ostream_iterator<std::string>(std::cout, "\n"));

        // Make the call.
        RcfClient<MyService> Client( RCF::TcpEndpoint("server", 50001) ); // put server address here
        Client.reverse(v);

        // Print them out again. This time they are in reverse order.
        std::cout << "\nAfter:\n";
        std::copy(
            v.begin(), 
            v.end(), 
            std::ostream_iterator<std::string>(std::cout, "\n"));

	    std::cout << "Press Enter to exit..." << std::endl;
	    std::cin.get();
    }
    catch(const RCF::Exception & e)
    {
        std::cout << "Caught exception:\n";
        std::cout << e.getError().getErrorString() << std::endl;
        return 1;
    }

    return 0;
}
Normal execution leads to construction and destruction of Session object. But in case of some troubles with a client, such as:
1. power off of client machine (without OS shut down, just instant power off)
2. disconnection of physycal network
3. turning off wi-fi collection
etc... (try these thing instead of pressing Enter in client)

In such cases Session object is not destroyed (see server's output) and connection stays established:

Code: Select all

C:\>netstat -n

...
  TCP    192.168.64.129:50001   192.168.64.128:1040    ESTABLISHED
This lead to leak of Session objects (and available TCP ports too?). How to avoid this?

jarl
Posts: 238
Joined: Mon Oct 03, 2011 4:53 am
Contact:

Re: Hung connections

Post by jarl »

You'll need to apply a session timeout to your server. Here's how to do it in RCF 2.0:

Code: Select all

	RCF::RcfServer server;

	// Sessions idle for 5 minutes will be terminated.
	server.setSessionTimeoutMs(5*60*1000); 

	// Scan for idle sessions every 60 seconds.
	server.setSessionHarvestingIntervalMs(1*60*1000);

	server.start();
Kind Regards

Jarl Lindrud
Delta V Software
http://www.deltavsoft.com

Raider
Posts: 12
Joined: Tue Mar 26, 2013 2:55 pm

Re: Hung connections

Post by Raider »

Thanks for solution! How to do such thing with 1.3.1?

Raider
Posts: 12
Joined: Tue Mar 26, 2013 2:55 pm

Re: Hung connections

Post by Raider »

Jarl, please suggest a solution for 1.3.1. Thanks a lot!

jarl
Posts: 238
Joined: Mon Oct 03, 2011 4:53 am
Contact:

Re: Hung connections

Post by jarl »

For 1.3.1 , session timeouts are configured like this:

Code: Select all

#include <RCF/SessionTimeoutService.hpp>

            RCF::RcfServer(...);      

            const boost::uint32_t SessionTimeoutMs = 5*60*1000;
            const boost::uint32_t ReapingIntervalMs = 1*60*1000;

            RCF::SessionTimeoutServicePtr stsPtr( 
                new RCF::SessionTimeoutService(SessionTimeoutMs, ReapingIntervalMs) );

            server.addService(stsPtr);

            server.start();
Kind Regards

Jarl Lindrud
Delta V Software
http://www.deltavsoft.com

spino
Posts: 3
Joined: Fri Oct 28, 2011 12:22 pm

Re: Hung connections

Post by spino »

Jarl, i'm can't find setSessionHarvestingIntervalMs function in RCF 2.0.0.2679. Maybe you mean the setServerObjectHarvestingIntervalS ?

jarl
Posts: 238
Joined: Mon Oct 03, 2011 4:53 am
Contact:

Re: Hung connections

Post by jarl »

Sorry about that. In 2.0.0.2679 it is called setSessionReapingIntervalMs() - it's been renamed recently in the codebase, but those changes haven't yet been released.
Kind Regards

Jarl Lindrud
Delta V Software
http://www.deltavsoft.com

Post Reply