Problem with popen



I'm having a problem with popen which is quite hard to explain. The
beavior I'm seeing looks fairly random but must be related to a
resource leak of some kind.

Under linux x86 I'm running a stress test which opens some tcp server
applications and then about 70 clients in each their thread. The main
thread is then spawning those new threads and waiting for them to join
again. All this works perfectly fine. In this process the main thread
additionally stops the external server application and restarts it at
random intervals. This is to test how the clients behavior in a
stressful situation when the server goes down behind their back.
The clients connect using tcp as well and are just a library used from
inside the application. One of the clients is a external application
which is spawned as a synchronous process using popen, fread and
pclose.
The server however is started as an async process using fork and then
execl.

What I'm seeing is that after 20 minutes run when everything is done
and everything has settled then popen doesn't work anymore. When I
start up another "tcp statistics client" using the same method of
popen, fread and pclose then popen just opens the process, fread
returns NULL and pclose returns without error.
So obviously it may seem as the process would be mal functioning.
However manual tests have shown that if the same external process is
run on commandline then it works perfectly fine while fread still
returns NULL.
So what I'm thinking is that some internal buffer got overflow and
doesn't have capacity for more text. I wonder if this could be the
result of a resource leak in my code or not.
Also what I notice is that when less than 70 threads are spawned then
it's more likely to work. Finally when the servers are not restarted
(async code not invoked) then it's also more likely to succeed.

It's a kind of weird problem but I was initially suspecting some
stream not to be flushed. fflush didn't help anything though. Maybe
it's related to not-closed handles with execl code.

At the end I've posted the C++ of this code for spawning and closing
sync and async processes.
Any input is welcome.

Thanks.

-- Henrik

-----------------------------

CProcess::CProcess(string strCommand, enum SyncType eSyncType, int
nTimeout)
{
m_strCommand = strCommand;

m_bStarted = false;
m_eSyncType = eSyncType;
}

CProcess::~CProcess()
{
Stop();
}

bool CProcess::Run()
{
if (m_bStarted)
return false;

int nNoWhere;

if (m_eSyncType == SYNC)
{
if ((m_fp = popen((char *) m_strCommand.c_str(), "r")) == NULL)
return false;
}

if (m_eSyncType == ASYNC)
{
m_PID = fork();

switch (m_PID)
{
case -1: /* Error */
return false;
break;

case 0: /* Child */
if (setpgid(getpid(), getpid()) != 0)
exit(1);

nNoWhere = open("/dev/null", O_RDWR, 0600);
dup2(nNoWhere, STDOUT_FILENO);
dup2(nNoWhere, STDERR_FILENO);

execl("/bin/sh", "/bin/sh", "-c", m_strCommand.c_str(), NULL);
_exit(EXIT_FAILURE);
break;

default: /* Parent */
break;
}
}

m_bStarted = true;

if (m_eSyncType == SYNC)
{
return Wait();
}
else
{
return true;
}
}

void CProcess::Stop()
{
if (m_bStarted)
{
if (m_eSyncType == ASYNC)
{
killpg(m_PID, SIGTERM);
}

m_bStarted = false;
}
}

bool CProcess::Wait()
{
char szOutput[1024];

if (!m_bStarted)
return false;

if (m_eSyncType == SYNC)
{
while (true)
{
if (fgets(szOutput, sizeof(szOutput)-1, m_fp) == NULL)
break;

m_strOutput += szOutput;
}

if (pclose(m_fp) == -1)
return false;
}

return true;
}

.



Relevant Pages

  • Re: forcing an nfs sync
    ... >I used the sync option in the /etc/exports file of the server, ... and clients still didn't see immediate changes in the file on ... >the server. ... >"sync" did not affect the overall throughput, ...
    (comp.protocols.nfs)
  • Re: Domain Time Syncing
    ... may want to start with the event logs on the clients that don't work. ... problem of why the server wasn't sync'ing with an outside source. ... see if their time will sync to the PDC. ... "The time service detected a time difference of greater than 128 ...
    (microsoft.public.windows.server.active_directory)
  • Remote Connection Issue/Question
    ... I have and SBS 2003 R2 Std server with several XP Pro clients. ... off-line/sync folder with a folder on the server. ... network then just sync the folder (either manually or by scheduling at the ...
    (microsoft.public.windows.server.sbs)
  • Re: Problem
    ... branch office VPN that will tunnel the traffic or the clients in location B ... need to connect individually via VPN to the SBS server at location A. ... When I log in it says it can't sync the files to the domain so it will ... was in the old router which I had to reset. ...
    (microsoft.public.windows.server.sbs)
  • Re: [SLE] NFS Performance issue
    ... > I have diskless workstations that boot ltsp and access an application ... > Could you elaborate a little more on the pro's and con's of sync vs. ... With async, the server simply ...
    (SuSE)