Repository: nvaccess/nvda

Issue:
nvda crashes after logon screen in windows 10
<!-- Please read the text in this edit field before filling it in.
Please thoroughly read NVDA's wiki article on how to fill in this template, including how to provide the required files.
Issues may be closed if the required information is not present.
https://github.com/nvaccess/nvda/blob/master/devDocs/githubIssueTemplateExplanationAndExamples.md
Please also note that the NVDA project has a Citizen and Contributor Code of Conduct which can be found at https://github.com/nvaccess/nvda/blob/master/CODE_OF_CONDUCT.md. NV Access expects that all contributors and other community members read and abide by the rules set out in this document while participating or contributing to this project. This includes creating or commenting on issues and pull requests. 

Each of the questions and sections below start with multiple hash symbols (#). Place your answers and information on the blank line below each question.
-->

### Steps to reproduce:
I just type my pin at the logon screen. After a while nvda loads and crash and load again.
### Actual behavior:
nvda crashes after the logon screen and restart it self. it causes the windows's configuration screen to show up, the same as if I press windows key + i. 

### Expected behavior:
nvda should load and don't crash
### System configuration
#### NVDA installed/portable/running from source:
installed
#### NVDA version:
2022.1.rc1 
#### Windows version:
windows 10
#### Name and version of other software in use when reproducing the issue:
sapi 5 eloquence
#### Other information about your system:

### Other questions
#### Does the issue still occur after restarting your computer?
yes
#### Have you tried any other versions of NVDA? If so, please report their behaviors.
no
#### If NVDA add-ons are disabled, is your problem still occurring?
yes
#### Does the issue still occur after you run the COM Registration Fixing Tool in NVDA's tools menu?
I didn't test this
[nvda-old.log](https://github.com/nvaccess/nvda/files/8686132/nvda-old.log)



Discussion:
cc @michaelDCurran 
Hi, does this happen when you use a different speech synthesizer when starting NVDA? If not, then it looks something odd is going on with SAPI5 driver, as the failure has to do with the fact that by the time SAPI5 Eloquence speaks, wx.app is not even ready yet. Thanks.


You got the point!

I changed the synthesizer to one core voices, saved the profile and 
restart. This time the crash didn't occur.

But, when I start with sapi 5 eloquence, I hear the nvda's start sound, 
after I hear the message "loading nvda please wait" with eloquence and 
after it crashes.

So, if the synthesizer was not loaded, how can I hear this first message?


Em 13/05/2022 07:24, Joseph Lee escreveu:
> Hi, does this happen when you use a different speech synthesizer when 
> starting NVDA? If not, then it looks something odd is going on with 
> SAPI5 driver, as the failure has to do with the fact that by the time 
> SAPI5 Eloquence speaks, wx.app is not even ready yet. Thanks.
>
> —
> Reply to this email directly, view it on GitHub 
> <https://github.com/nvaccess/nvda/issues/13694#issuecomment-1125892043>, 
> or unsubscribe 
> <https://github.com/notifications/unsubscribe-auth/AELWSGYQU34HLMP25HTQEGTVJYUV3ANCNFSM5V25E3SA>.
> You are receiving this because you authored the thread.Message ID: 
> ***@***.***>
>
Can we consider creating our instance of `wx.App` earlier in `core.main`? This should be safe as  far as I can tell. In the long run it would be nice to change `core.callLater` not to rely on  app instance being created and / or make sapi5 driver use `nvwave` directly, but both of these are way too risky for 2022.1 at this stage IMO.
For people investigating, one thing to note also in the log:
```
DEBUGWARNING - core.main (06:31:26.214) - MainThread (13496):
Slow starting core (19.04 sec)
```


There is a long running issue where it does not crash,but comes up with no 
sound and if you  look at theold log after a restart it says there is no 
sound device found. I've had this happen on Windows 7 and 10. It is usually 
due to a lot of things trying to load at the start and not quite making it 
in time for nvda to see the audio drivers are loaded and initialized, but 
obviously, this will not allow you to use it on the start screen either to 
log in of course. The answer seems to be often solved by decluttering start 
up by removing the  crap, or sometimes by Ccleaner  being run.
  I don't think I've seen it happen as described where it runs, but falls 
over. one would imagine that if it is speaking on the log in it should speak 
everywhere. What synth iis actually used for log in? If its different from 
that used in the main program, I can see that might cause an issue. When I 
get sound issues I tend to set everything to Espeak and save the config, 
then  try it  a few times at start up.
 Brian

***@***.***
Sent via blueyonder.
Please address personal E-mail to:-
***@***.***, putting 'Brian Gaff'
in the display name field.
Newsgroup monitored: alt.comp.blind-users
----- Original Message ----- 
From: "Joseph Lee" ***@***.***>
To: "nvaccess/nvda" ***@***.***>
Cc: "Subscribed" ***@***.***>
Sent: Friday, May 13, 2022 11:24 AM
Subject: Re: [nvaccess/nvda] nvda crashes after logon screen in windows 10 
(Issue #13694)


> Hi, does this happen when you use a different speech synthesizer when 
> starting NVDA? If not, then it looks something odd is going on with SAPI5 
> driver, as the failure has to do with the fact that by the time SAPI5 
> Eloquence speaks, wx.app is not even ready yet. Thanks.
>
>
>
> -- 
> Reply to this email directly or view it on GitHub:
> https://github.com/nvaccess/nvda/issues/13694#issuecomment-1125892043
> You are receiving this because you are subscribed to this thread.
>
> Message ID: ***@***.***> 


Hmm, but in some cases this has occurred for several versions to me at 
least, and decluttering did seem to resolve it. I wonder if something else 
is also using Sappi 5, it was in my case, but I gave up on that due to lag.
 Brian

***@***.***
Sent via blueyonder.
Please address personal E-mail to:-
***@***.***, putting 'Brian Gaff'
in the display name field.
Newsgroup monitored: alt.comp.blind-users
----- Original Message ----- 
From: "Łukasz Golonka" ***@***.***>
To: "nvaccess/nvda" ***@***.***>
Cc: "Subscribed" ***@***.***>
Sent: Friday, May 13, 2022 1:29 PM
Subject: Re: [nvaccess/nvda] nvda crashes after logon screen in windows 10 
(Issue #13694)


> Can we consider creating our instance of `wx.App` earlier in `core.main`? 
> This should be safe as  far as I can tell. In the long run it would be 
> nice to change `core.callLater` not to rely on  app instance being created 
> and / or make sapi5 driver use `nvwave` directly, but both of these are 
> way too risky for 2022.1 at this stage IMO.
>
> -- 
> Reply to this email directly or view it on GitHub:
> https://github.com/nvaccess/nvda/issues/13694#issuecomment-1126004603
> You are receiving this because you are subscribed to this thread.
>
> Message ID: ***@***.***> 


Yes could the machine be one of those with a normal drive, say running a 
slowprocessor or have low ram? It is obvious to me at least that some 
machines being sold even now only have 4gigs of ram, You need 8 or greater 
for smooth boot up.
Brian

***@***.***
Sent via blueyonder.
Please address personal E-mail to:-
***@***.***, putting 'Brian Gaff'
in the display name field.
Newsgroup monitored: alt.comp.blind-users
----- Original Message ----- 
From: "Cyrille Bougot" ***@***.***>
To: "nvaccess/nvda" ***@***.***>
Cc: "Subscribed" ***@***.***>
Sent: Friday, May 13, 2022 1:29 PM
Subject: Re: [nvaccess/nvda] nvda crashes after logon screen in windows 10 
(Issue #13694)


> For people investigating, one thing to note also in the log:
> ```
> DEBUGWARNING - core.main (06:31:26.214) - MainThread (13496):
> Slow starting core (19.04 sec)
> ```
>
>
>
> -- 
> Reply to this email directly or view it on GitHub:
> https://github.com/nvaccess/nvda/issues/13694#issuecomment-1126004756
> You are receiving this because you are subscribed to this thread.
>
> Message ID: ***@***.***> 


@alexandretoco  Can you please test and confirm this is not an issue with 2021.3.5?

Resolving patch:
diff --git a/source/audioDucking.py b/source/audioDucking.py
--- a/source/audioDucking.py
+++ b/source/audioDucking.py
@@ -124,8 +124,13 @@ def _unensureDucked(delay=True):
 		import core
 		if _isDebug():
 			log.debug("Queuing _unensureDucked")
-		core.callLater(1000,_unensureDucked,False)
-		return
+		try:
+			core.callLater(1000, _unensureDucked, False)
+			return
+		except core.NVDANotInitializedError:
+			# If the wx.App has not been initialized, audio ducking callbacks
+			# will fail as they rely on wx.CallLater/wx.CallAfter
+			log.debugWarning("wx App not initialized, unducking immediately")
 	with _duckingRefCountLock:
 		_duckingRefCount-=1
 		if _isDebug():
diff --git a/source/core.py b/source/core.py
--- a/source/core.py
+++ b/source/core.py
@@ -866,13 +866,22 @@ def requestPump():
 	import wx
 	wx.CallAfter(_pump.Start,PUMP_MAX_DELAY, True)
 
+
+class NVDANotInitializedError(Exception):
+	pass
+
+
 def callLater(delay, callable, *args, **kwargs):
 	"""Call a callable once after the specified number of milliseconds.
 	As the call is executed within NVDA's core queue, it is possible that execution will take place slightly after the requested time.
 	This function should never be used to execute code that brings up a modal UI as it will cause NVDA's core to block.
-	This function can be safely called from any thread.
+	This function can be safely called from any thread once NVDA has been initialized.
 	"""
 	import wx
+	if wx.GetApp() is None:
+		# If NVDA has not fully initialized yet, the wxApp may not be initialized.
+		# wx.CallLater and wx.CallAfter requires the wxApp to be initialized.
+		raise NVDANotInitializedError("Cannot schedule callable, wx.App is not initialized")
 	if threading.get_ident() == mainThreadId:
 		return wx.CallLater(delay, _callLaterExec, callable, args, kwargs)
 	else:


Test patch: