Repository: nvaccess/nvda

Issue:
UIA console: cmd/powershell crashes completely with a lot of text that scrolls
### Steps to reproduce:
1. Ensure you have set Windows console support to "preferred UIA".
2. Open a terminal and execute a script/command that returns a lot of text.
3. Wait a few seconds.

### Actual behavior:
The terminal is frequently crashing (closes completely). No problem with legacy mode. I raised following errors in the log on several attempts (sometimes there is no traceback in the log):

```
Traceback (most recent call last):
  File "NVDAObjects\UIA\__init__.pyc", line 284, in __init__
  File "comtypesMonkeyPatches.pyc", line 26, in __call__
_ctypes.COMError: (-2147220991, 'An event was unable to invoke any of the subscribers', (None, None, None, 0, None))None, 0, None))
```

```
DEBUGWARNING - NVDAObjects.behaviors.LiveText._monitor (05:48:57.681) - Dynamic_WinConsoleUIAEditableTextWithAutoSelectDetectionUIA._monitorThread (6592):
Error getting lines or calculating new text
Traceback (most recent call last):
  File "NVDAObjects\behaviors.pyc", line 301, in _monitor
  File "NVDAObjects\UIA\winConsoleUIA.pyc", line 286, in _getTextLines
  File "comtypesMonkeyPatches.pyc", line 26, in __call__
_ctypes.COMError: (-2147220991, 'An event was unable to invoke any of the subscribers', (None, None, None, 0, None))
```

```
Traceback (most recent call last):
  File "NVDAObjects\__init__.pyc", line 77, in __call__
  File "NVDAObjects\UIA\__init__.pyc", line 954, in __init__
NVDAObjects.InvalidNVDAObject: no windowHandle
```

### Expected behavior:
No terminal crash.

### System configuration
#### NVDA installed/portable/running from source:
Installed

#### NVDA version:
alpha-18955,12f27acf

#### Windows version:
10 1903 (build 18990.1)

#### Name and version of other software in use when reproducing the issue:
cmd, powershell, bash, etc..

#### Other information about your system:
N/A

### Other questions
#### Does the issue still occur after restarting your PC?
Yes

#### Have you tried any other versions of NVDA? If so, please report their behaviors.
No problem in legacy mode.

CC @codeofdusk 


Discussion:
Also cc @leonardder @feerrenrut. My personal opinion is that unless this can be fixed in time for 2019.3 we should reconsider making UIA the default for terminals.
Not able to reproduce this – @lukaszgo1 does your C program from #6291 work for this?
@Andre9642 Is there any particular command  with which you are able to reproduce this consistently? Also I believe that you are heavy Braille user is this reproducible without braille display attached?
Hello,

Try this, I've posted also to #6291.

Run this in a CMD or a PowerShell session. Note that you need to have python 3 installed with the launcher.

```ps
    py -3 -c "for x in range(100000): print(x)"
```

Cheers,
Or more simply (in pure-batch):

`@for /l %x in (1, 1, 100000000) do @echo %x`

Cmd crashes around 9100 for me.
This bug is critical for me. In this case it does not matter, but during compilations it's very annoying. So I don't use UIA for Windows Console support in production environment. For this reason and other bugs, I consider UIA still in alpha Stage.
I can reproduce this with @Andre9642 's test case on my 1903 VM. @codeofdusk Are you planning to work on this? It is certainly worse that #6291 because terminal crashes completely, so it prevents users from completing whatever they were doing.
I'm now able to reproduce this, seems to be caused by 9fbd6b49015eba744dc2294642d96c481194930d.

Resolving patch:
diff --git a/source/NVDAObjects/UIA/winConsoleUIA.py b/source/NVDAObjects/UIA/winConsoleUIA.py
--- a/source/NVDAObjects/UIA/winConsoleUIA.py
+++ b/source/NVDAObjects/UIA/winConsoleUIA.py
@@ -281,12 +281,9 @@ def _get_TextInfo(self):
 
 	def _getTextLines(self):
 		# Filter out extraneous empty lines from UIA
-		return (
-			self.makeTextInfo(textInfos.POSITION_ALL)
-			._rangeObj.getText(-1)
-			.rstrip()
-			.split("\r\n")
-		)
+		ptr = self.UIATextPattern.GetVisibleRanges()
+		res = [ptr.GetElement(i).GetText(-1) for i in range(ptr.length)]
+		return res
 
 
 def findExtraOverlayClasses(obj, clsList):


Test patch: