Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6795009
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T18:17:28+00:00 2026-05-26T18:17:28+00:00

I have a multi-threaded gSOAP service running with enabled http-keepalive. How can I gracefully

  • 0

I have a multi-threaded gSOAP service running with enabled http-keepalive. How can I gracefully shutdown the service when there are still clients connected?

A similar question was asked in gSoap: how to gracefully shutdown the webservice application?, but the answers do not cover the http-keepalive aspect: The soap-serve function will simply not return until the http-keepalive-session wasn’t closed by the client. Thus, step 2 in the accepted answer will block until the client decides to close the connection (or the receive-timeout expires, but a short timeout would break the desired http-keepalive behaviour here).

The examples from the gSOAP documentation suffer from the same problem.

What I tried so far was to call soap_done() for all soap structs that are hanging in a soap_serve call from the main thread to interrupt the connections waiting for http-keepalive, which works most of the time, but crashes in rare conditions (a race condition maybe), so this is no solution for me.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T18:17:29+00:00Added an answer on May 26, 2026 at 6:17 pm

    I just ran into the very same problem and I think I’ve got a solution for you.

    As you just said, the problem is that the gSoap hangs on soap_serve. This happens because gSOAP generates an internal loop for you that waits for the arrival of all keep-alive requests OR a timeout on the server-side arises.

    What I’ve done is grabbing the soap_serve function inside the automatically generated service stub. I’m going to list the original soap_serve function so that you can find it on your service stub file :

      SOAP_FMAC5 int SOAP_FMAC6 soap_serve(struct soap *soap)
        {
        #ifndef WITH_FASTCGI
            unsigned int k = soap->max_keep_alive;
        #endif
    
            do
            {
        #ifdef WITH_FASTCGI
                if (FCGI_Accept() < 0)
                {
                    soap->error = SOAP_EOF;
                    return soap_send_fault(soap);
                }
        #endif
    
                soap_begin(soap);
    
        #ifndef WITH_FASTCGI
                if (soap->max_keep_alive > 0 && !--k)
                    soap->keep_alive = 0;
        #endif
    
                if (soap_begin_recv(soap))
                {   if (soap->error < SOAP_STOP)
                    {
        #ifdef WITH_FASTCGI
                        soap_send_fault(soap);
        #else 
                        return soap_send_fault(soap);
        #endif
                    }
                    soap_closesock(soap);
    
                    continue;
                }
    
                if (soap_envelope_begin_in(soap)
                 || soap_recv_header(soap)
                 || soap_body_begin_in(soap)
                 || soap_serve_request(soap)
                 || (soap->fserveloop && soap->fserveloop(soap)))
                {
        #ifdef WITH_FASTCGI
                    soap_send_fault(soap);
        #else
                    return soap_send_fault(soap);
        #endif
                }
    
        #ifdef WITH_FASTCGI
                soap_destroy(soap);
                soap_end(soap);
            } while (1);
        #else
            } while (soap->keep_alive);
        #endif
            return SOAP_OK;
        }
    

    You should extract the body of this function and replace your old soap_serve(mySoap) call inside your thread (the thread that performs the requests and hagns because of the keep-alive) with the following:

    do
        {
            if ( Server::mustShutdown() ) {
                break;
            }
    
            soap_begin(mySoap);
    
            // If we reached the max_keep_alive we'll exit
            if (mySoap->max_keep_alive > 0 && !--k)
                mySoap->keep_alive = 0;
    
    
            if (soap_begin_recv(mySoap))
            {   if (mySoap->error < SOAP_STOP)
                {
                    soap_send_fault(mySoap);
                    break;
                }
                soap_closesock(mySoap);
    
                continue;
            }
    
            if (soap_envelope_begin_in(mySoap)
             || soap_recv_header(mySoap)
             || soap_body_begin_in(mySoap)
             || soap_serve_request(mySoap)
             || (mySoap->fserveloop && mParm_Soap->fserveloop(mySoap)))
            {
                soap_send_fault(mySoap);
                break;
            }
    
    
        } while (mySoap->keep_alive);
    

    Note the following:

    1. The Server::mustShutdown() acts as a flag that will be set to true (externally) to end all the threads. When you want to stop the server from handling new requests you’ll this function will return true and the loop will end.
    2. I’ve removed the ifdef, WITH_FASTCGI it’s not interesting for us now.
    3. When the you close the connection like this, any clients connected to the server will raise an exception. Clients written in C# for instance will throw a “The underlying connection is excepted to keep alive was closed by the server” wich makes perfect sense for us.

    But we are not done yet, thanks to what AudioComplex pointed out, the system still remains waiting for reqeuests on soap_begin_recv. But I’ve got a solution for that too 😉

    Each of the threads on the connection-handling pool creates a copy of the main soap context (via soap_copy), these threads are the ones that
    I store each of these contexts as an element on the array that resides on the main connection-handling thread.
    When terminating the main connection-handling thread (the one that serves the requests) it will go through all soap contexts and finalize “manually” the connection by using:

    for (int i = 0; i < soaps.size(); ++i) {
      soaps[i]->fclose(soaps[i]);
    }
    

    This will force the soap_serve loop to finish. It actually will stop the internal loop near line 921 of stdsoap2.cpp_

    r = select((int)soap->socket + 1, &fd, NULL, &fd, &timeout);
    

    It is not the cleanest solution (haven’t found a cleaner one) but it will definitely stop the service.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

If I have a multi-threaded program that reads a cache-type memory by reference. Can
i have a multi-threaded notification-service which uses a simple protocol. When testing it with
I have a multi-threaded application which scales well to begin with, but running on
Here's the scenario: I have a multi threaded java web application which is running
I have my own multi-threaded service which handles some commands. The service consists of
I have a multi-threaded app which is running under Linux 2.6.30 on an 8
I have a multi-threaded C++ app which does 3D rendering with the OpenSceneGraph library.
I have a multi-threaded Windows application that occasionally deadlocks. Inevitably this happens on a
I have a multi-threaded Windows C++ app written in Visual Studio 6. Within the
I have a multi threaded .NET app that uses async I/O and AsyncCallbacks to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.