Source:
<code>void start() { long start_ms = System.currentTimeMillis(); long delta_s = 0; FileInputStream adPipe = null; if(appConfig.getAutoDiscoveryEnabled()) { LOGGER.info("Auto Discovery enabled"); adPipe = newAutoDiscoveryPipe(); } while (true) { if (appConfig.getExitWatcher().shouldExit()){ LOGGER.info("Exit file detected: stopping JMXFetch."); System.exit(0); } if(adPipe == null && appConfig.getAutoDiscoveryEnabled()) { adPipe = newAutoDiscoveryPipe(); } try { if(adPipe != null) { int offset = 0; byte[] buffer = new byte[0]; <START> while (adPipe.available() > 0) { <END> int len = adPipe.available(); byte[] minibuff = new byte[len]; adPipe.read(minibuff); int oldLen = buffer.length; buffer = Arrays.copyOf(buffer, buffer.length + len); System.arraycopy(minibuff, 0, buffer, oldLen, len); } setReinit(processAutoDiscovery(buffer)); } } catch(IOException e) { LOGGER.warn("Unable to read from pipe - Service Discovery configuration may have been skipped."); } catch(Exception e) { LOGGER.warn("Unknown problem parsing auto-discovery configuration: " + e); } long start = System.currentTimeMillis(); if (this.reinit.get()) { init(true); } if (instances.size() > 0) { doIteration(); } else { LOGGER.warn("No instance could be initiated. Retrying initialization."); appConfig.getStatus().flush(); configs = getConfigs(appConfig); init(true); } long length = System.currentTimeMillis() - start; LOGGER.debug("Iteration ran in " + length + " ms"); try { int loopPeriod = appConfig.getCheckPeriod(); LOGGER.debug("Sleeping for " + loopPeriod + " ms."); Thread.sleep(loopPeriod); } catch (InterruptedException e) { LOGGER.warn(e.getMessage(), e); } } }</code><technical_language>Is a race condition adPipe.available() return 0 read writer is unblocked? safer introduce a separator for</technical_language>

Target:
void start() { long start_ms = System.currentTimeMillis(); long delta_s = 0; FileInputStream adPipe = null; if(appConfig.getAutoDiscoveryEnabled()) { LOGGER.info("Auto Discovery enabled"); adPipe = newAutoDiscoveryPipe(); } while (true) { if (appConfig.getExitWatcher().shouldExit()){ LOGGER.info("Exit file detected: stopping JMXFetch."); System.exit(0); } if(adPipe == null && appConfig.getAutoDiscoveryEnabled()) { adPipe = newAutoDiscoveryPipe(); } try { if(adPipe != null && adPipe.available() > 0) { byte[] buffer = new byte[0]; boolean terminated = false; while (!terminated) { int len = adPipe.available(); if (len > 0) { byte[] minibuff = new byte[len]; adPipe.read(minibuff); if (Bytes.indexOf(minibuff, App.AD_LEGACY_CONFIG_TERM.getBytes()) > -1 || Bytes.indexOf(minibuff, App.AD_CONFIG_TERM.getBytes()) > -1 ) { terminated = true; } int oldLen = buffer.length; buffer = Arrays.copyOf(buffer, buffer.length + len); System.arraycopy(minibuff, 0, buffer, oldLen, len); } } setReinit(processAutoDiscovery(buffer)); } } catch(IOException e) { LOGGER.warn("Unable to read from pipe - Service Discovery configuration may have been skipped."); } catch(Exception e) { LOGGER.warn("Unknown problem parsing auto-discovery configuration: " + e); } long start = System.currentTimeMillis(); if (this.reinit.get()) { init(true); } if (instances.size() > 0) { doIteration(); } else { LOGGER.warn("No instance could be initiated. Retrying initialization."); appConfig.getStatus().flush(); configs = getConfigs(appConfig); init(true); } long length = System.currentTimeMillis() - start; LOGGER.debug("Iteration ran in " + length + " ms"); try { int loopPeriod = appConfig.getCheckPeriod(); LOGGER.debug("Sleeping for " + loopPeriod + " ms."); Thread.sleep(loopPeriod); } catch (InterruptedException e) { LOGGER.warn(e.getMessage(), e); } } }