Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.

Bug 252060

Summary: InputStream from HostShellProcessAdapter incorrectly throws IOException
Product: [Tools] Target Management Reporter: Greg Watson <g.watson>
Component: RSEAssignee: Anna Dushistova <anna.dushistova>
Status: RESOLVED FIXED QA Contact: Martin Oberhuber <mober.at+eclipse>
Severity: normal    
Priority: P3 Keywords: helpwanted
Version: 3.0.1   
Target Milestone: 3.1 M4   
Hardware: PC   
OS: Mac OS X - Carbon (unsup.)   
Whiteboard:
Bug Depends on:    
Bug Blocks: 251531    
Attachments:
Description Flags
junit test for this bug
none
patch to fix problem anna.dushistova: iplog+, anna.dushistova: review+

Description Greg Watson CLA 2008-10-24 15:41:45 EDT
If I create a shell command and wrap it in a HostShellProcessAdapter, the InputStream throws an IOException when the shell exits (e.g. if the command is "ls; exit") because the PipedInputStream is broken. This seems to be because HostShellProcessAdapter#shellOutputChanged is not checking for an empty event and closing the corresponding PipedOutputStream.

The problem seems to be related to bug #158786.
Comment 1 Martin Oberhuber CLA 2008-11-12 08:51:36 EST
Greg - since you folks seem to have an environment which exposes the issue, could you try coming up with a patch and a unit test?

Anna - assigning to you since this is your area of expertise, it should be pretty simple to write a unittest for this, and you started creating the ShellServiceUnitTests.
Comment 2 Anna Dushistova CLA 2008-12-11 07:07:33 EST
Created attachment 120183 [details]
junit test for this bug

Here is a junit test. The issue is reproduced on my Linux machine with both local and ssh connection types.
Comment 3 Martin Oberhuber CLA 2008-12-11 09:18:48 EST
Comment on attachment 120183 [details]
junit test for this bug

Can you please commit the test? There's no need attaching unittests. Having them in CVS right away saves us all the time with bugzilla, creating/applying/reviewing the patch etc. 

Testcases can be committed by anybody at any time.
Comment 4 Anna Dushistova CLA 2008-12-11 09:28:51 EST
(In reply to comment #3)
> (From update of attachment 120183 [details])
> Can you please commit the test? There's no need attaching unittests. Having
> them in CVS right away saves us all the time with bugzilla,
> creating/applying/reviewing the patch etc. 
> 
> Testcases can be committed by anybody at any time.

Done. Note that it constantly fails now because of that broken pipe exception. 

Comment 5 Greg Watson CLA 2008-12-11 09:44:52 EST
Here is the patch I use:

### Eclipse Workspace Patch 1.0
#P org.eclipse.rse.services
Index: src/org/eclipse/rse/services/shells/HostShellProcessAdapter.java
===================================================================
RCS file: /cvsroot/dsdp/org.eclipse.tm.rse/plugins/org.eclipse.rse.services/src/org/eclipse/rse/services/shells/HostShellProcessAdapter.java,v
retrieving revision 1.3
diff -u -r1.3 HostShellProcessAdapter.java
--- src/org/eclipse/rse/services/shells/HostShellProcessAdapter.java	16 Nov 2006 10:17:05 -0000	1.3
+++ src/org/eclipse/rse/services/shells/HostShellProcessAdapter.java	11 Dec 2008 14:44:13 -0000
@@ -155,12 +155,19 @@
 	public void shellOutputChanged(IHostShellChangeEvent event) {
 		IHostOutput[] input = event.getLines();
 		OutputStream outputStream = event.isError() ? hostShellError : hostShellInput;
-		try {
-		for(int i = 0; i < input.length; i++) {
-			outputStream.write(input[i].getString().getBytes());
-			outputStream.write('\n');
-			outputStream.flush();
+		if (input.length == 0) {
+			try {
+				outputStream.close();
+			} catch (IOException e) {
+			}
+			return;
 		}
+		try {
+			for(int i = 0; i < input.length; i++) {
+				outputStream.write(input[i].getString().getBytes());
+				outputStream.write('\n');
+				outputStream.flush();
+			}
 		} catch(IOException e) {
 			// Ignore
 		}

Comment 6 Anna Dushistova CLA 2008-12-11 11:10:45 EST
Works for me as well.

(In reply to comment #5)
> Here is the patch I use:
> 
> ### Eclipse Workspace Patch 1.0
> #P org.eclipse.rse.services
> Index: src/org/eclipse/rse/services/shells/HostShellProcessAdapter.java
> ===================================================================
> RCS file:
> /cvsroot/dsdp/org.eclipse.tm.rse/plugins/org.eclipse.rse.services/src/org/eclipse/rse/services/shells/HostShellProcessAdapter.java,v
> retrieving revision 1.3
> diff -u -r1.3 HostShellProcessAdapter.java
> --- src/org/eclipse/rse/services/shells/HostShellProcessAdapter.java    16 Nov
> 2006 10:17:05 -0000      1.3
> +++ src/org/eclipse/rse/services/shells/HostShellProcessAdapter.java    11 Dec
> 2008 14:44:13 -0000
> @@ -155,12 +155,19 @@
>         public void shellOutputChanged(IHostShellChangeEvent event) {
>                 IHostOutput[] input = event.getLines();
>                 OutputStream outputStream = event.isError() ? hostShellError :
> hostShellInput;
> -               try {
> -               for(int i = 0; i < input.length; i++) {
> -                       outputStream.write(input[i].getString().getBytes());
> -                       outputStream.write('\n');
> -                       outputStream.flush();
> +               if (input.length == 0) {
> +                       try {
> +                               outputStream.close();
> +                       } catch (IOException e) {
> +                       }
> +                       return;
>                 }
> +               try {
> +                       for(int i = 0; i < input.length; i++) {
> +                              
> outputStream.write(input[i].getString().getBytes());
> +                               outputStream.write('\n');
> +                               outputStream.flush();
> +                       }
>                 } catch(IOException e) {
>                         // Ignore
>                 }
> 

Comment 7 Anna Dushistova CLA 2008-12-15 09:54:06 EST
Greg, could you please create a patch with proper copyright, so that we could apply it? 
Comment 8 Greg Watson CLA 2008-12-15 10:46:12 EST
Created attachment 120481 [details]
patch to fix problem

Here is a new patch with the copyright updated.
Comment 9 Anna Dushistova CLA 2008-12-15 12:20:27 EST
Thanks!
We also need a Legal
Disclaimer from you indicating that you contribute your work under EPL - see
http://www.eclipse.org/dsdp/tm/development/committer_howto.php#external_contrib
for details.

(In reply to comment #8)
> Created an attachment (id=120481) [details]
> patch to fix problem
> 
> Here is a new patch with the copyright updated.
> 

Comment 10 Greg Watson CLA 2008-12-17 12:34:50 EST
I'm already an Eclipse committer, so I don't think I need to do this.
Comment 11 Martin Oberhuber CLA 2008-12-17 12:48:12 EST
(In reply to comment #10)
> I'm already an Eclipse committer, so I don't think I need to do this.

Greg - you are not a committer on our project, so I respectfully ask you to post the message. While it may seem tiresome and unnecessary, we use this as a constant reminder to ourselves about some extremely important key principles of every contribution. 

Please do take your time to copy & paste the message from the link that Anna provided. We're treating all our contributors equal in this case. I believe that starting to make exceptions to this policy we have would reduce its usefulness. Thanks!

Comment 12 Martin Oberhuber CLA 2008-12-17 12:53:33 EST
PS More officially, and pointing at the IP Poster:
   http://www.eclipse.org/legal/EclipseLegalProcessPoster.pdf

* Fig 1 - you are not a committer on the same project under supervision of PMC
* Fig 2 - you are not an employee of the submitting committer
--> Therefore, for you, Fig 3 / Fig 9 applies for you, and we are requested
    to "confirm that the contribution...." which is exactly what we're doing
    by requesting you submit that legal statement.

Thanks!
Comment 13 Greg Watson CLA 2008-12-19 08:09:54 EST
I, Greg Watson, declare that I developed 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.
Comment 14 Anna Dushistova CLA 2008-12-19 08:53:30 EST
Patch is checked in, thanks!