Skip to contentSkip to conversation

[399231] Fix race conditions when reading using LocalShellOutputReader(s)

PATCH Superseded Ioana Grigoropol 1 participant 1 message Jan 28, 2013

v5 of 7 Superseded Latest is v7 →

lore
Share & tools
lore
#0 lore raw

PATCH v5 1/1 [399231] Fix race conditions when reading using LocalShellOutputReader(s)

When running a remote command with a Local connection from a Linux host a new LocalHostShell is created.
At this time, a new LocalHostThread is launched, along side with two LocalShellOutputReaders (output and error). The constructors for the OutputReaders will receive a reference to the reader created by the LocalHostThread.

When calling runCommandRemote, the newly created IHostShell is returned, making it possible to create an adapter(IHostShellProcessAdapter) that will then be used to read all available output from the readers.
When the LocalShellThread finishes running its run() method, it will perform a cleanup causing both readers (output and error) to be closed.
If at this point, there are any readers, or handlers to the readers that are still trying to read data (read using handlers readers), an error will be thrown saying that the the pipe to the stream is closed ("Ensure open stream"/ "Pipe closed" errors).
After inspecting the internalReadLine, it seems that it expects a null reference of the reader in order to consider that the reading is done.
In order to ensure that this will happen once the LocalShellThread closes the stream(s), we will make sure that the reference to the readers is set to null, and that LocalShellOutputReader(s) use the same reference of the reader as the LocalShellThread. For this to happen, a reference of the thread must be kept in the reader, in order to retrieve the correct reference of the underlying reader. Since there can still be race conditions in this solution, all operations that involve the reference to now the only reader, must be performed under mutual exclusion using a shared lock between the LocalShellOutputReader and LocalShellThread.

---
 .../dstore/shells/DStoreShellOutputReader.java     |   20 +++++++-
 .../services/local/shells/LocalHostShell.java      |   12 +++--
 .../local/shells/LocalShellOutputReader.java       |   53 ++++++++++++++------
 .../services/local/shells/LocalShellThread.java    |   24 +++++++++
 .../.settings/.api_filters                         |   38 +++++++++++---
 .../shells/TerminalServiceShellOutputReader.java   |   20 ++++++++
 .../services/shells/IHostShellOutputReader.java    |   20 +++++++-
 7 files changed, 159 insertions(+), 28 deletions(-)
rse/plugins/org.eclipse.rse.services.dstore/src/org/eclipse/rse/internal/services/dstore/shells/DStoreShellOutputReader.java +19 -1
index 84c9bb9..d918b35 100644
@@ -13,10 +13,19 @@
  * 
  * Contributors:
  * David McKnight (IBM) - [286671] return null when status is null
+ * Ioana Grigoropol (Intel) - [399231] Race conditions occur when trying to read from local processes using LocalShellOutputReader
+ * Legal Message:
+ * I, Ioana Grigorpol, declare that I developed the attached code from scratch,
+ * without referencing any 3rd party materials except material licensed under
+ * the EPL.
+ * I am authorized by my employer to make this contribution under the EPL.
  *******************************************************************************/
 
 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;
@@ -167,7 +176,16 @@ public class DStoreShellOutputReader extends AbstractHostShellOutputReader imple
 		super.finish();
 		notifyResponse();
 	}
-	
+
+	public BufferedReader getReader() {
+		// TODO Auto-generated method stub
+		return null;
+	}
+
+	public Lock getReaderLock() {
+		return null;
+	}
+
 	/*
 	private void handleInput()
 	{
rse/plugins/org.eclipse.rse.services.local/src/org/eclipse/rse/internal/services/local/shells/LocalHostShell.java +9 -3
index 250a904..b2eed82 100644
@@ -13,6 +13,12 @@
  * 
  * Contributors:
  * Martin Oberhuber (Wind River) - [161838] local shell reports isActive() wrong
+ * Ioana Grigoropol (Intel) - [399231] Race conditions occur when trying to read from local processes using LocalShellOutputReader
+ * Legal Message:
+ * I, Ioana Grigorpol, declare that I developed the attached code from scratch,
+ * without referencing any 3rd party materials except material licensed under
+ * the EPL.
+ * I am authorized by my employer to make this contribution under the EPL.
  *******************************************************************************/
 
 package org.eclipse.rse.internal.services.local.shells;
@@ -36,9 +42,9 @@ public class LocalHostShell extends AbstractHostShell implements IHostShell
 	
 	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)
rse/plugins/org.eclipse.rse.services.local/src/org/eclipse/rse/internal/services/local/shells/LocalShellOutputReader.java +39 -14

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 +24 -0
index 0a33ad4..567d0eb 100644
@@ -17,6 +17,12 @@
  * 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 
+ * Ioana Grigoropol (Intel) - [399231] Race conditions occur when trying to read from local processes using LocalShellOutputReader
+ * Legal Message:
+ * I, Ioana Grigorpol, declare that I developed the attached code from scratch,
+ * without referencing any 3rd party materials except material licensed under
+ * the EPL.
+ * I am authorized by my employer to make this contribution under the EPL.
  *******************************************************************************/
 
 package org.eclipse.rse.internal.services.local.shells;
@@ -29,6 +35,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,6 +70,7 @@ public class LocalShellThread extends Thread
 	private BufferedReader _stdInput;
 	private BufferedReader _stdError;
 
+	private Lock _lock;
 	/**
 	 * constructor for local command shell monitor
 	 * 
@@ -260,6 +269,7 @@ public class LocalShellThread extends Thread
 
 			_stdError = new BufferedReader(new InputStreamReader(_theProcess.getErrorStream()));
 
+			_lock = new ReentrantLock();
 		}
 		catch (IOException e)
 		{
@@ -438,9 +448,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 +526,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
index 19273c2..da9417a 100644
@@ -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 +20 -0
index 16364e1..67a4ccd 100644
@@ -17,12 +17,21 @@
  * 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
+ * Ioana Grigoropol (Intel) - [399231] Race conditions occur when trying to read from local processes using LocalShellOutputReader
+ * Legal Message:
+ * I, Ioana Grigorpol, declare that I developed the attached code from scratch,
+ * without referencing any 3rd party materials except material licensed under
+ * the EPL.
+ * I am authorized by my employer to make this contribution under the EPL.
+
  *******************************************************************************/
 
 package org.eclipse.rse.internal.services.shells;
 
 import java.io.BufferedReader;
 import java.io.IOException;
+import java.util.concurrent.locks.Lock;
+import java.util.concurrent.locks.ReentrantLock;
 
 import org.eclipse.rse.internal.services.Activator;
 import org.eclipse.rse.services.shells.AbstractHostShellOutputReader;
@@ -36,6 +45,7 @@ import org.eclipse.rse.services.shells.SimpleHostOutput;
 public class TerminalServiceShellOutputReader extends
 		AbstractHostShellOutputReader {
 	protected BufferedReader fReader;
+	protected Lock lock;
 	private volatile Thread fReaderThread = null;
 	private volatile boolean isCanceled = false;
 	private String fPromptChars = ">$%#]"; //Characters we accept as the end of a prompt //$NON-NLS-1$;
@@ -174,4 +184,14 @@ public class TerminalServiceShellOutputReader extends
 			fReaderThread.interrupt();
 		}
 	}
+
+	public BufferedReader getReader() {
+		return fReader;
+	}
+
+	public Lock getReaderLock() {
+		if (lock == null)
+			lock = new ReentrantLock();
+		return lock;
+	}
 }
rse/plugins/org.eclipse.rse.services/src/org/eclipse/rse/services/shells/IHostShellOutputReader.java +18 -2
index 103c31f..1399866 100644
@@ -11,11 +11,19 @@
  * Emily Bruner, Mazen Faraj, Adrian Storisteanu, Li Ding, and Kent Hawley.
  * 
  * Contributors:
- * {Name} (company) - description of contribution.
+ * Ioana Grigoropol (Intel) - [399231] Race conditions occur when trying to read from local processes using LocalShellOutputReader
+ * Legal Message:
+ * I, Ioana Grigorpol, declare that I developed the attached code from scratch,
+ * without referencing any 3rd party materials except material licensed under
+ * the EPL.
+ * I am authorized by my employer to make this contribution under the EPL.
  ********************************************************************************/
 
 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 +31,12 @@ public interface IHostShellOutputReader extends IHostShellOutputNotifier
 	public void addOutputListener(IHostShellOutputListener listener);
 	public boolean isErrorReader();
 	public void finish();
-}
\ No newline at end of file
+	/**
+	 * @since 3.2
+	 */
+	public BufferedReader getReader();
+	/**
+	 * @since 3.2
+	 */
+	public Lock getReaderLock();
+}
Signature · 1 line
1.7.9.5