Repository: spyder-ide/spyder

Issue:
Spyder claims to be "Connecting to kernel" while running a file
<!--- **PLEASE READ:** When submitting here, please ensure you've completed the following checklist and checked the boxes to confirm. Issue reports without it may be closed. Thanks! --->

### Issue Report Checklist

* [ ] Searched the [issues page](https://github.com/spyder-ide/spyder/issues?q=is%3Aissue) for similar reports
* [ ] Read the relevant sections of the [Spyder Troubleshooting Guide](https://github.com/spyder-ide/spyder/wiki/Troubleshooting-Guide-and-FAQ) and followed its advice
* [ ] Reproduced the issue after updating with ``conda update spyder`` (or ``pip``, if not using Anaconda)
* [ ] Could not reproduce inside ``jupyter qtconsole`` (if console-related)
* [ ] Tried basic troubleshooting (if a bug/error)
    * [ ] Restarted Spyder
    * [ ] Reset preferences with ``spyder --reset``
    * [ ] Reinstalled the latest version of [Anaconda](https://www.anaconda.com/download/)
    * [ ] Tried the other applicable steps from the Troubleshooting Guide
* [ ] Completed the **Problem Description**, **Steps to Reproduce** and **Version** sections below


## Problem Description

Spyder claims to be Connecting to kernel while running a file

### What steps reproduce the problem?

1.  create a file with:
```
import time
time.sleep(10)
print('Done')
```
2. Open a new kernel
3. Before the kernel is loaded, press the "Run File" button

### What is the expected output? What do you see instead?
I expect spyder to connect, then run the file. Instead, "Connecting to kernel" is shown until the processing is finished.


## Versions
<!--- You can get this information from Help > About Spyder...
or (if Spyder won't launch) the "conda list" command
from the Anaconda Prompt/Terminal/command line. --->

* Spyder version: 4.x
* Python version: 3.7.7
* Qt version: Qt 5.12.6
* PyQt version: PyQt5 5.12.3
* Operating System name/version: Darwin 19.5.0 

### Dependencies
<!--- Please go to the menu entry Help > Dependencies,
press the Copy to clipboard button and paste below --->

```
PASTE DEPENDENCIES HERE
```



Discussion:
I can confirm this happens(I had to use the run hot key because my kernel started too soon)

This doesn't seem to happen on kernel restarts. Nothing is run.

> I expect spyder to connect, then run the file. Instead, "Connecting to kernel" is shown until the processing is finished.

The command cache would help here. The console can check for cached commands when it's ready. 

How does the "run in dedicated console" deal with this? It opens and console and runs right away.
Thanks @impact27 for finding the issue and @bcolsen for the confirmation. I was able to reproduce this too :+1:
> I expect spyder to connect, then run the file. Instead, "Connecting to kernel" is shown until the processing is finished.

Most probably an execute message is sent to the kernel before the first prompt is shown, which prevents us from hiding the loading page (we hide that page as soon as the first prompt appears).

However, I don't know how to solve this.
Maybe the kernel sends the header text when it is ready? (The python version and all) Alternatively we could disable the execute function until the console is ready 


> Alternatively we could disable the execute function until the console is ready

Yeah, I think that's a simpler and easier way to fix this.

Resolving patch:
diff --git a/spyder/plugins/ipythonconsole/widgets/client.py b/spyder/plugins/ipythonconsole/widgets/client.py
--- a/spyder/plugins/ipythonconsole/widgets/client.py
+++ b/spyder/plugins/ipythonconsole/widgets/client.py
@@ -316,6 +316,9 @@ def _before_prompt_is_ready(self):
         self._show_loading_page()
         self.shellwidget.sig_prompt_ready.connect(
             self._when_prompt_is_ready)
+        # If remote execution, the loading page should be hidden as well
+        self.shellwidget.sig_remote_execute.connect(
+            self._when_prompt_is_ready)
 
     def _when_prompt_is_ready(self):
         """Configuration after the prompt is shown."""
@@ -330,6 +333,8 @@ def _when_prompt_is_ready(self):
 
         self.shellwidget.sig_prompt_ready.disconnect(
             self._when_prompt_is_ready)
+        self.shellwidget.sig_remote_execute.disconnect(
+            self._when_prompt_is_ready)
 
     def enable_stop_button(self):
         self.stop_button.setEnabled(True)
diff --git a/spyder/plugins/ipythonconsole/widgets/shell.py b/spyder/plugins/ipythonconsole/widgets/shell.py
--- a/spyder/plugins/ipythonconsole/widgets/shell.py
+++ b/spyder/plugins/ipythonconsole/widgets/shell.py
@@ -60,6 +60,7 @@ class ShellWidget(NamepaceBrowserWidget, HelpWidget, DebuggingWidget,
     sig_kernel_restarted_message = Signal(str)
     sig_kernel_restarted = Signal()
     sig_prompt_ready = Signal()
+    sig_remote_execute = Signal()
 
     # For global working directory
     sig_change_cwd = Signal(str)
@@ -688,6 +689,11 @@ def _prompt_started_hook(self):
             self._highlighter.highlighting_on = True
             self.sig_prompt_ready.emit()
 
+    def _handle_execute_input(self, msg):
+        """Handle an execute_input message"""
+        super(ShellWidget, self)._handle_execute_input(msg)
+        self.sig_remote_execute.emit()
+
     #---- Qt methods ----------------------------------------------------------
     def focusInEvent(self, event):
         """Reimplement Qt method to send focus change notification"""


Test patch: