Fix race conditions for reading output from local processes
PATCH v2 1/1 Fix race conditions for reading output from local processes
When creating a new LocalHostShell a new LocalHostThread is created, along side with two LocalShellOutputReaders (output and error).
Both readers will receive a reference to one of the localhostThread's readers.
When the thread finishes its task, the reader is closed. If any of the readers is "late" trying to read data, will through an error similar to "Ensure open stream" or "Pipe closed". Any reading done on the reader should be done atomically, such that no reader should be caught off-guard when the stream closes.
Also, the readers should reference the thread's reader, otherwise nulling this reference will be useless.
Signed-off-by Ioana Grigoropol <[email protected]>
--- .../dstore/shells/DStoreShellOutputReader.java | 52 ++++++++++++-------- .../services/local/shells/LocalHostShell.java | 22 ++++----- .../local/shells/LocalShellOutputReader.java | 52 ++++++++++++++------ .../services/local/shells/LocalShellThread.java | 24 +++++++-- .../.settings/.api_filters | 38 +++++++++++--- .../shells/TerminalServiceShellOutputReader.java | 25 +++++++--- .../services/shells/IHostShellOutputReader.java | 21 ++++++-- 7 files changed, 162 insertions(+), 72 deletions(-)
rse/plugins/org.eclipse.rse.services.dstore/src/org/eclipse/rse/internal/services/dstore/shells/DStoreShellOutputReader.java +32 -20
@@ -7,16 +7,19 @@ * * Initial Contributors: * The following IBM employees contributed to the Remote System Explorer - * component that contains this file: David McKnight, Kushal Munir, - * Michael Berger, David Dykstal, Phil Coulthard, Don Yantzi, Eric Simpson, + * component that contains this file: David McKnight, Kushal Munir, + * Michael Berger, David Dykstal, Phil Coulthard, Don Yantzi, Eric Simpson, * Emily Bruner, Mazen Faraj, Adrian Storisteanu, Li Ding, and Kent Hawley. - * + * * Contributors: * David McKnight (IBM) - [286671] return null when status is null *******************************************************************************/ package org.eclipse.rse.internal.services.dstore.shells; +import java.io.BufferedReader; +import java.util.concurrent.locks.Lock; + import org.eclipse.dstore.core.model.DataElement; import org.eclipse.dstore.extra.DomainEvent; import org.eclipse.dstore.extra.IDomainListener;
@@ -41,13 +44,13 @@ public class DStoreShellOutputReader extends AbstractHostShellOutputReader imple _status.getDataStore().getDomainNotifier().addDomainListener(this); } } - + public String getWorkingDirectory() { String pwd = _status.getSource(); return pwd; } - + protected IHostOutput internalReadLine() { if (_status != null && _keepRunning)
@@ -57,9 +60,9 @@ public class DStoreShellOutputReader extends AbstractHostShellOutputReader imple while (newSize > _statusOffset) { DataElement line = _status.get(_statusOffset++); - - - + + + String type = line.getType(); boolean isError = type.equals("error") || type.equals("stderr"); //$NON-NLS-1$ //$NON-NLS-2$ if (_isErrorReader && isError)
@@ -71,13 +74,13 @@ public class DStoreShellOutputReader extends AbstractHostShellOutputReader imple return new DStoreHostOutput(line); } } - - + + try { if (_hostShell.isActive()) { - waitForResponse(); + waitForResponse(); return internalReadLine(); } else
@@ -86,7 +89,7 @@ public class DStoreShellOutputReader extends AbstractHostShellOutputReader imple } } catch (Exception e) - { + { e.printStackTrace(); } }
@@ -128,7 +131,7 @@ public class DStoreShellOutputReader extends AbstractHostShellOutputReader imple // for now, this is pulled via internalReadLine() notifyResponse(); } - + } /**
@@ -138,16 +141,16 @@ public class DStoreShellOutputReader extends AbstractHostShellOutputReader imple { try { - wait(); + wait(); } catch (InterruptedException e) { e.printStackTrace(); } } - + /** - * Causes all threads waiting for this + * Causes all threads waiting for this * to wake up. */ public synchronized void notifyResponse()
@@ -161,13 +164,22 @@ public class DStoreShellOutputReader extends AbstractHostShellOutputReader imple e.printStackTrace(); } } - + public void finish() { super.finish(); notifyResponse(); } - + + public BufferedReader getReader() { + // TODO Auto-generated method stub + return null; + } + + public Lock getReaderLock() { + return null; + } + /* private void handleInput() {
@@ -177,7 +189,7 @@ public class DStoreShellOutputReader extends AbstractHostShellOutputReader imple int currentSize = _linesOfOutput.size(); for (int loop = currentSize; loop < totalSize; loop++) - { + { DataElement result = (DataElement) results.get(loop); addLine(result.getName()); }
@@ -185,4 +197,4 @@ public class DStoreShellOutputReader extends AbstractHostShellOutputReader imple */ } - +
rse/plugins/org.eclipse.rse.services.local/src/org/eclipse/rse/internal/services/local/shells/LocalHostShell.java +10 -12
@@ -7,10 +7,10 @@ * * Initial Contributors: * The following IBM employees contributed to the Remote System Explorer - * component that contains this file: David McKnight, Kushal Munir, - * Michael Berger, David Dykstal, Phil Coulthard, Don Yantzi, Eric Simpson, + * component that contains this file: David McKnight, Kushal Munir, + * Michael Berger, David Dykstal, Phil Coulthard, Don Yantzi, Eric Simpson, * Emily Bruner, Mazen Faraj, Adrian Storisteanu, Li Ding, and Kent Hawley. - * + * * Contributors: * Martin Oberhuber (Wind River) - [161838] local shell reports isActive() wrong *******************************************************************************/
@@ -18,8 +18,6 @@ package org.eclipse.rse.internal.services.local.shells; import org.eclipse.core.runtime.IProgressMonitor; -import org.eclipse.rse.internal.services.local.shells.LocalShellOutputReader; -import org.eclipse.rse.internal.services.local.shells.LocalShellThread; import org.eclipse.rse.services.shells.AbstractHostShell; import org.eclipse.rse.services.shells.IHostShell; import org.eclipse.rse.services.shells.IHostShellOutputReader;
@@ -33,19 +31,19 @@ public class LocalHostShell extends AbstractHostShell implements IHostShell private LocalShellThread _shellThread; private LocalShellOutputReader _stdoutHandler; private LocalShellOutputReader _stderrHandler; - + public LocalHostShell(String initialWorkingDirectory, String invocation, String encoding, String[] environment) { - _shellThread = new LocalShellThread(initialWorkingDirectory, invocation, encoding, environment); - _stdoutHandler = new LocalShellOutputReader(this, _shellThread.getOutputStream(), false); - _stderrHandler = new LocalShellOutputReader(this, _shellThread.getErrorStream(),true); + _shellThread = new LocalShellThread(initialWorkingDirectory, invocation, encoding, environment); + _stdoutHandler = new LocalShellOutputReader(this, _shellThread.getOutputStream(), _shellThread, false); + _stderrHandler = new LocalShellOutputReader(this, _shellThread.getErrorStream(), _shellThread, true); } - + protected void run(IProgressMonitor monitor) { _shellThread.start(); } - + /* (non-Javadoc) * @see org.eclipse.rse.services.shells.IHostShell#isActive()
@@ -68,7 +66,7 @@ public class LocalHostShell extends AbstractHostShell implements IHostShell { _shellThread.sendInput(command); } - + public IHostShellOutputReader getStandardOutputReader() { return _stdoutHandler;
rse/plugins/org.eclipse.rse.services.local/src/org/eclipse/rse/internal/services/local/shells/LocalShellOutputReader.java +36 -16
130 lines not on this page · raw message
rse/plugins/org.eclipse.rse.services.local/src/org/eclipse/rse/internal/services/local/shells/LocalShellThread.java +21 -3
@@ -16,7 +16,7 @@ * Lothar Werzinger (Tradescape) - [161838] Support terminating local shells * David McKnight (IBM) - [189387] Use specified encoding for shell output * Martin Oberhuber (Wind River) - [161838] local shell reports isActive() wrong - * Anna Dushistova (MontaVsita) - [249354] Incorrect behaviour of local shells subsystem runCommand method + * Anna Dushistova (MontaVsita) - [249354] Incorrect behaviour of local shells subsystem runCommand method *******************************************************************************/ package org.eclipse.rse.internal.services.local.shells;
@@ -29,6 +29,8 @@ import java.io.InputStreamReader; import java.io.OutputStream; import java.io.OutputStreamWriter; import java.net.URL; +import java.util.concurrent.locks.Lock; +import java.util.concurrent.locks.ReentrantLock; import org.eclipse.core.runtime.FileLocator;
@@ -62,9 +64,10 @@ public class LocalShellThread extends Thread private BufferedReader _stdInput; private BufferedReader _stdError; + private Lock _lock; /** * constructor for local command shell monitor - * + * * @param cwd initial working directory * @param invocation launch shell command * @param encoding encoding to use or <code>null</code> for default
@@ -192,7 +195,7 @@ public class LocalShellThread extends Thread args = new String[1]; args[0] = _invocation; _theProcess = Runtime.getRuntime().exec(args[0], envVars, theDirectory); - } else { + } else { args = new String[3]; args[0] = theShell; args[1] = "-c";//$NON-NLS-1$
@@ -260,6 +263,7 @@ public class LocalShellThread extends Thread _stdError = new BufferedReader(new InputStreamReader(_theProcess.getErrorStream())); + _lock = new ReentrantLock(); } catch (IOException e) {
@@ -438,9 +442,14 @@ public class LocalShellThread extends Thread _isDone = true; try { + _lock.lock(); _stdInput.close(); _stdError.close(); + _stdInput = null; + _stdError = null; + + _lock.unlock(); if (_theProcess != null) {
@@ -511,4 +520,13 @@ public class LocalShellThread extends Thread } + public Lock getLock() { + return _lock; + } + + + public void setLock(Lock _lock) { + this._lock = _lock; + } + }
rse/plugins/org.eclipse.rse.services/.settings/.api_filters +30 -8
@@ -1,5 +1,13 @@ <?xml version="1.0" encoding="UTF-8" standalone="no"?> <component id="org.eclipse.rse.services" version="2"> + <resource path="META-INF/MANIFEST.MF"> + <filter id="923795461"> + <message_arguments> + <message_argument value="3.2.200"/> + <message_argument value="3.2.200"/> + </message_arguments> + </filter> + </resource> <resource path="META-INF/MANIFEST.MF" type="org.eclipse.rse.internal.services.terminals.AbstractTerminalService"> <filter id="305324134"> <message_arguments>
@@ -32,13 +40,13 @@ <filter id="305324134"> <message_arguments> <message_argument value="org.eclipse.rse.internal.services.terminals.BaseShellDecorator"/> - <message_argument value="org.eclipse.rse.services_3.1.1"/> + <message_argument value="org.eclipse.rse.services_3.1.0"/> </message_arguments> </filter> <filter id="305324134"> <message_arguments> <message_argument value="org.eclipse.rse.internal.services.terminals.BaseShellDecorator"/> - <message_argument value="org.eclipse.rse.services_3.1.0"/> + <message_argument value="org.eclipse.rse.services_3.1.1"/> </message_arguments> </filter> </resource>
@@ -46,13 +54,13 @@ <filter id="305324134"> <message_arguments> <message_argument value="org.eclipse.rse.internal.services.terminals.IBaseShell"/> - <message_argument value="org.eclipse.rse.services_3.1.1"/> + <message_argument value="org.eclipse.rse.services_3.1.0"/> </message_arguments> </filter> <filter id="305324134"> <message_arguments> <message_argument value="org.eclipse.rse.internal.services.terminals.IBaseShell"/> - <message_argument value="org.eclipse.rse.services_3.1.0"/> + <message_argument value="org.eclipse.rse.services_3.1.1"/> </message_arguments> </filter> </resource>
@@ -88,13 +96,13 @@ <filter id="305324134"> <message_arguments> <message_argument value="org.eclipse.rse.internal.services.terminals.TerminalShellDecorator"/> - <message_argument value="org.eclipse.rse.services_3.1.1"/> + <message_argument value="org.eclipse.rse.services_3.1.0"/> </message_arguments> </filter> <filter id="305324134"> <message_arguments> <message_argument value="org.eclipse.rse.internal.services.terminals.TerminalShellDecorator"/> - <message_argument value="org.eclipse.rse.services_3.1.0"/> + <message_argument value="org.eclipse.rse.services_3.1.1"/> </message_arguments> </filter> </resource>
@@ -109,13 +117,27 @@ <filter id="305365105"> <message_arguments> <message_argument value="org.eclipse.rse.internal.services.terminals.ProcessBaseShell"/> - <message_argument value="org.eclipse.rse.services_3.1.1"/> + <message_argument value="org.eclipse.rse.services_3.1.0"/> </message_arguments> </filter> <filter id="305365105"> <message_arguments> <message_argument value="org.eclipse.rse.internal.services.terminals.ProcessBaseShell"/> - <message_argument value="org.eclipse.rse.services_3.1.0"/> + <message_argument value="org.eclipse.rse.services_3.1.1"/> + </message_arguments> + </filter> + </resource> + <resource path="src/org/eclipse/rse/services/shells/IHostShellOutputReader.java" type="org.eclipse.rse.services.shells.IHostShellOutputReader"> + <filter id="403804204"> + <message_arguments> + <message_argument value="org.eclipse.rse.services.shells.IHostShellOutputReader"/> + <message_argument value="getReader()"/> + </message_arguments> + </filter> + <filter id="403804204"> + <message_arguments> + <message_argument value="org.eclipse.rse.services.shells.IHostShellOutputReader"/> + <message_argument value="getReaderLock()"/> </message_arguments> </filter> </resource>
rse/plugins/org.eclipse.rse.services/src/org/eclipse/rse/internal/services/shells/TerminalServiceShellOutputReader.java +17 -8
@@ -7,13 +7,13 @@ * * Initial Contributors: * The following IBM employees contributed to the Remote System Explorer - * component that contains this file: David McKnight, Kushal Munir, - * Michael Berger, David Dykstal, Phil Coulthard, Don Yantzi, Eric Simpson, + * component that contains this file: David McKnight, Kushal Munir, + * Michael Berger, David Dykstal, Phil Coulthard, Don Yantzi, Eric Simpson, * Emily Bruner, Mazen Faraj, Adrian Storisteanu, Li Ding, and Kent Hawley. - * + * * Contributors: - * Martin Oberhuber (Wind River) - Adapted from LocalShellOutputReader. - * Martin Oberhuber (Wind River) - Added vt100 escape sequence ignoring. + * Martin Oberhuber (Wind River) - Adapted from LocalShellOutputReader. + * Martin Oberhuber (Wind River) - Added vt100 escape sequence ignoring. * Anna Dushistova (MontaVista) - adapted from SshShellOutputReader * Anna Dushistova (MontaVista) - [240523] [rseterminals] Provide a generic adapter factory that adapts any ITerminalService to an IShellService * Rob Stryker (JBoss) - [335059] TerminalServiceShellOutputReader logs error when hostShell.exit() is called
@@ -23,6 +23,7 @@ package org.eclipse.rse.internal.services.shells; import java.io.BufferedReader; import java.io.IOException; +import java.util.concurrent.locks.Lock; import org.eclipse.rse.internal.services.Activator; import org.eclipse.rse.services.shells.AbstractHostShellOutputReader;
@@ -125,8 +126,8 @@ public class TerminalServiceShellOutputReader extends // in the Buffer...If not, then we assume it is waiting for // input. if (!done && !fReader.ready()) { - // wait to make sure -- max. 500 msec to wait for new chars - // if we are not at a CRLF seems to be appropriate for the + // wait to make sure -- max. 500 msec to wait for new chars + // if we are not at a CRLF seems to be appropriate for the // Pipes and Threads in ssh. long waitIncrement = 500; // Check if we think we are at a prompt
@@ -163,7 +164,7 @@ public class TerminalServiceShellOutputReader extends } return new SimpleHostOutput(theLine.toString()); } - + /** * Stop the reader Thread, forcing internalReadLine() to return. * Does not close the Stream.
@@ -174,4 +175,12 @@ public class TerminalServiceShellOutputReader extends fReaderThread.interrupt(); } } + + public BufferedReader getReader() { + return fReader; + } + + public Lock getReaderLock() { + return null; + } }
rse/plugins/org.eclipse.rse.services/src/org/eclipse/rse/services/shells/IHostShellOutputReader.java +16 -5
@@ -1,21 +1,24 @@ /******************************************************************************** * Copyright (c) 2006 IBM Corporation. All rights reserved. * This program and the accompanying materials are made available under the terms - * of the Eclipse Public License v1.0 which accompanies this distribution, and is + * of the Eclipse Public License v1.0 which accompanies this distribution, and is * available at http://www.eclipse.org/legal/epl-v10.html - * + * * Initial Contributors: * The following IBM employees contributed to the Remote System Explorer - * component that contains this file: David McKnight, Kushal Munir, - * Michael Berger, David Dykstal, Phil Coulthard, Don Yantzi, Eric Simpson, + * component that contains this file: David McKnight, Kushal Munir, + * Michael Berger, David Dykstal, Phil Coulthard, Don Yantzi, Eric Simpson, * Emily Bruner, Mazen Faraj, Adrian Storisteanu, Li Ding, and Kent Hawley. - * + * * Contributors: * {Name} (company) - description of contribution. ********************************************************************************/ package org.eclipse.rse.services.shells; +import java.io.BufferedReader; +import java.util.concurrent.locks.Lock; + public interface IHostShellOutputReader extends IHostShellOutputNotifier { public IHostOutput readLine();
@@ -23,4 +26,12 @@ public interface IHostShellOutputReader extends IHostShellOutputNotifier public void addOutputListener(IHostShellOutputListener listener); public boolean isErrorReader(); public void finish(); + /** + * @since 3.2 + */ + public BufferedReader getReader(); + /** + * @since 3.2 + */ + public Lock getReaderLock(); }
Signature · 1 line
1.7.9.5