Skip to content

Conversation

@gnodet
Copy link
Member

@gnodet gnodet commented Apr 29, 2025

This PR fixes issue #1115 where the inherited input stream is closed when a subprocess terminates on Windows.

The problem occurs because ProcessBuilder.inheritIO() causes the subprocess to inherit all standard streams (stdin, stdout, stderr) from the parent process. When the subprocess terminates, it closes these streams, which can cause issues if the parent process still needs to use them.

This fix modifies the isPosixSystemStream and systemStreamName methods in ExecTerminalProvider to handle Windows differently:

  1. For isPosixSystemStream:

    • On Windows:
      • For SystemStream.Output and SystemStream.Error, we use explicit redirects to avoid inheriting stdin
      • For SystemStream.Input, we use isWindowsSystemStream which is safer on Windows
    • On non-Windows platforms, we continue to use inheritIO() as it works fine there
  2. For systemStreamName:

    • On Windows, for SystemStream.Input, we use System.console() to check if stdin is a terminal without using ProcessBuilder
    • For all other cases, we use the original approach

This approach is targeted and minimally invasive, focusing only on fixing the Windows-specific issue while leaving the behavior unchanged for other platforms.

@gnodet gnodet force-pushed the fix-issue-1115 branch 2 times, most recently from 92bb9c4 to 5462f7a Compare April 29, 2025 17:55
@gnodet gnodet changed the title Fix issue #1115: Prevent inherited input stream from being closed on Windows fix: Prevent inherited input stream from being closed on Windows (fixes #1115) Apr 29, 2025
@gnodet gnodet added the bug label Apr 30, 2025
@gnodet gnodet changed the title fix: Prevent inherited input stream from being closed on Windows (fixes #1115) Prevent inherited input stream from being closed on Windows (fixes #1115) Apr 30, 2025
@gnodet gnodet force-pushed the fix-issue-1115 branch from 5462f7a to 295a36d Compare May 9, 2025 18:55
gnodet added 2 commits May 27, 2025 16:47
This commit implements a workaround to fix the issue with interruption handling
in nested shells. The fix clears the current pipe before creating a child shell
and restores it afterward, ensuring that when Ctrl+C is pressed in a child shell
(e.g., when running 'sh' followed by 'ttop'), the interruption signal is properly
propagated to the child process.

The implementation uses reflection to access the Pipe.setCurrentPipe method in
Felix Gogo runtime, which is currently private. The proper fix has been submitted
to the Felix project in two PRs:
1. apache/felix-dev#411 - Make Pipe.setCurrentPipe public
2. apache/felix-dev#412 - Update Posix.runShell to handle nested shells

Once these PRs are merged and released, the modified Shell.java and Posix.java
classes should be removed from JLine and the official Felix Gogo JLine
implementation should be used instead.

Fixes #1143
#1115)

The problem occurs because ProcessBuilder.inheritIO() causes the subprocess to inherit all standard streams (stdin, stdout, stderr) from the parent process. When the subprocess terminates, it closes these streams, which can cause issues if the parent process still needs to use them.

This fix modifies the isPosixSystemStream and systemStreamName methods in ExecTerminalProvider to handle Windows differently:

1. For isPosixSystemStream:
   - On Windows:
     - For SystemStream.Output and SystemStream.Error, we use explicit redirects to avoid inheriting stdin
     - For SystemStream.Input, we use isWindowsSystemStream which is safer on Windows
   - On non-Windows platforms, we continue to use inheritIO() as it works fine there

2. For systemStreamName:
   - On Windows, for SystemStream.Input, we use System.console() to check if stdin is a terminal without using ProcessBuilder
   - For all other cases, we use the original approach

This approach is targeted and minimally invasive, focusing only on fixing the Windows-specific issue while leaving the behavior unchanged for other platforms.
@gnodet gnodet marked this pull request as draft July 7, 2025 06:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants