0

My program recursivly calls the McAfee OnDemad Virusscanner for a specific path. In a loop I fill the path with several files, scan it, remove the files, fill it and so on. After a while the program suddenly hangs and I've no clue for the reason.

        Calendar cl = Calendar.getInstance();
        String cmd="uvscan -v /var/tmp/McAfee/scanFiles/"
        try {   
        long start=cl.getTimeInMillis();
        process = Runtime.getRuntime().exec(cmd, null);
        Worker worker = new Worker(process);
        worker.start(); 
        try {
            LOGGER.debug("Virusscan timeout set to {}ms", timeout);
            worker.join(timeout);
            if (worker.exit != null)
                workerRC=worker.exit;
            else {
                cl = Calendar.getInstance();
                long waitTime=cl.getTimeInMillis()-start;
                throw new TimeoutException("Virusscan timeout after " + waitTime + "ms"); 
            }
            //      else
            //          throw new TimeoutException();
        } catch(InterruptedException ex) {
            worker.interrupt();
            Thread.currentThread().interrupt();
            cl = Calendar.getInstance();
            long waitTime=cl.getTimeInMillis()-start;
            throw new TimeoutException("Virusscan timeout after " + waitTime + "ms"); 
        } 

        File scanLog = new File(ConfigReader.getScanLog());
        if (!ConfigReader.mustKeepScanLog()) scanLog.deleteOnExit();
        LOGGER.debug("ScanLog is: " + scanLog.getPath() + " - RC=" + workerRC);
        BufferedWriter bw = new BufferedWriter(new FileWriter(scanLog));
        BufferedReader stdInput = new BufferedReader(new InputStreamReader(process.getInputStream()));
        // read the output from the command             
        while ((buffer = stdInput.readLine()) != null) {
            inspectScanlog(buffer, workerRC);
            bw.write(buffer+"\n");
            LOGGER.debug("Scan STDOUT: " + buffer);
        }
        BufferedReader stdErr = new BufferedReader(new InputStreamReader(process.getErrorStream()));
        while ((buffer = stdErr.readLine()) != null) {
            LOGGER.debug("Scan STDERR: " + buffer);
        }

        bw.close();

        cl = Calendar.getInstance();
        long waitTime=cl.getTimeInMillis()-start;
        LOGGER.info("Virusscan took " + waitTime + "ms");
        if (workerRC != 0){
            int cc=12;
            ConfigReader.setMaxCC(cc);
            LOGGER.error("RC={} - Virusscan ended with error. CMD=",cc, cmd);
            if (workerRC==13) {
                LOGGER.error("RC={} - The scanner found one or more viruses or hostile objects - such as a Trojan-horse program, joke program, or test file.", cc);
            }
        }
    }
    catch (Exception e) {
        int cc=12;
        ConfigReader.setMaxCC(cc);
        LOGGER.error("RC={} - {}",cc, e.getMessage());
        e.printStackTrace();
    } finally {
        if (process!=null) {
            process.destroy(); 
        }
    }

It looks like a hang of the complete program; even the timeout (240000ms) doesn't match. The GC-log seems kind of interrupted:

258.070: [Full GC [PSYoungGen: 13456K->7362K(85120K)] [PSOldGen: 59271K->60735K(81728K)] 72727K->68098K(166848K) [PSPermGen: 16282K->16282K(30400K)], 0.3019290 secs] [Times: user=0.31 sys=0.00, real=0.31 secs] 264.208: [GC [PSYoungGen: 65466K->8773K(92224K)] 126202K->69581K(173952K), 0.0528360 secs] [Times: user=0.10 sys=0.00, real=0.05 secs] 265.445: [GC [PSYoungGen: 81774K->9133K(92096K)] 142582K->70061K(173824K), 0.0205430 secs] [Times: user=0.03 sys=0.00, real=0.02 secs] 266.916: [GC [PSYoungGen: 82515K->9698K(100096K)] 143443K->70658K(181824K), 0.0189050 secs] [Times: user=0.04 sys=0.00, real=0.02 secs] 267.817: [GC [PSYoungGen: 92311K->1436K(100480K)] 153271K->70986K(182208K), 0.0210400 secs] [Times: user=0.03 sys=0.01, real=0.02 secs] 274.208: [GC [PSYoungGen: 84072K->672K(112256K)] 153622K->71297K(193984K), 0.0029610 secs] [Times: user=0.00 sys=0.00, real=0.00 secs] 275.769: [GC [PSYoungGen: 94880K->500K(112832K)] 165505K->71732K(194560K), 0.0022440 secs]

The uvscan-command has been called 277.609 seconds after program-start.

Could anyone suggest how to get the reason for the hang?

Thanks in advance, Ulrich

Ulrich
  • 653
  • 2
  • 7
  • 23
  • worker.start() ... worker.join() ... before the process is being read. does it hang before stdInput is being created? I am not sure what class Worker is, but it's a really common bug when calling out external processes to have the process hang because that process' stdout/stderr aren't being drained at the right place. – Rob Jun 27 '13 at 16:36
  • Sorry, it is solved now. I ran the program with a shorter timeout and looked at the returned messages. And this was the problem. The verbose mode (-v) and the message flood gave the hang. Removing the verbose mode solved it. Though there were only 3000 files, so this is the number of expected messages times 100 bytes gives a total of 300KB - which isn't very much. I will consider the hint given in http://stackoverflow.com/questions/882772/capturing-stdout-when-calling-runtime-exec/882795#882795 and http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.html?page=4. Thanks a lot, Ulrich – Ulrich Jun 27 '13 at 17:22

0 Answers0