Skip to content

Commit

Permalink
Require backend ACK before sending initial input
Browse files Browse the repository at this point in the history
  • Loading branch information
aivarannamaa committed Apr 7, 2023
1 parent a6ea8a6 commit 4104729
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 2 deletions.
2 changes: 2 additions & 0 deletions thonny/plugins/cpython_backend/cp_launcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
I could also do python -c "from backend import MainCPythonBackend: MainCPythonBackend().mainloop()",
but looks like this gives relative __file__-s on imported modules.)
"""
from thonny.running import PROCESS_ACK

# NB! This module can be also imported (when querying its path for uploading)
if __name__ == "__main__":
Expand Down Expand Up @@ -39,6 +40,7 @@

thonny.prepare_thonny_user_dir()
thonny.configure_backend_logging()
print(PROCESS_ACK)

target_cwd = sys.argv[1]
report_time("Before constructing backend")
Expand Down
2 changes: 2 additions & 0 deletions thonny/plugins/cpython_ssh/cps_back.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
MessageFromBackend,
serialize_message,
)
from thonny.running import PROCESS_ACK

logger = getLogger("thonny.plugins.cpython_ssh.cps_back")

Expand Down Expand Up @@ -194,6 +195,7 @@ def _upload_main_backend(self):

if __name__ == "__main__":
thonny.configure_backend_logging()
print(PROCESS_ACK)
args = ast.literal_eval(sys.argv[1])
backend = SshCPythonBackend(**args)
backend.mainloop()
2 changes: 2 additions & 0 deletions thonny/plugins/ev3/ev3_back.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import thonny
from thonny.plugins.micropython.os_mp_backend import SshUnixMicroPythonBackend
from thonny.running import PROCESS_ACK

logger = getLogger("thonny.plugins.ev3.ev3_back")

Expand All @@ -19,6 +20,7 @@ def _get_sys_path_for_analysis(self) -> Optional[List[str]]:
if __name__ == "__main__":
THONNY_USER_DIR = os.environ["THONNY_USER_DIR"]
thonny.configure_backend_logging()
print(PROCESS_ACK)

import ast

Expand Down
2 changes: 2 additions & 0 deletions thonny/plugins/micropython/bare_metal_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
RAW_SUBMIT_MODE,
)
from thonny.plugins.micropython.webrepl_connection import WebReplConnection
from thonny.running import PROCESS_ACK

RAW_PASTE_COMMAND = b"\x05A\x01"
RAW_PASTE_CONFIRMATION = b"R\x01"
Expand Down Expand Up @@ -1716,6 +1717,7 @@ class RawPasteNotSupportedError(RuntimeError):

def launch_bare_metal_backend(backend_class: Callable[..., BareMetalMicroPythonBackend]) -> None:
thonny.configure_backend_logging()
print(PROCESS_ACK)

import ast

Expand Down
2 changes: 2 additions & 0 deletions thonny/plugins/micropython/os_mp_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
ends_overlap,
)
from thonny.plugins.micropython.mp_common import PASTE_SUBMIT_MODE
from thonny.running import PROCESS_ACK

# Can't use __name__, because it will be "__main__"
logger = getLogger("thonny.plugins.micropython.os_mp_backend")
Expand Down Expand Up @@ -383,6 +384,7 @@ def _create_pipkin_adapter(self):
if __name__ == "__main__":
THONNY_USER_DIR = os.environ["THONNY_USER_DIR"]
thonny.configure_backend_logging()
print(PROCESS_ACK)

import ast

Expand Down
19 changes: 17 additions & 2 deletions thonny/running.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@
from thonny.ui_utils import select_sequence, show_dialog
from thonny.workdlg import WorkDialog

PROCESS_ACK = "OK"

logger = getLogger(__name__)

WINDOWS_EXE = "python.exe"
Expand Down Expand Up @@ -1058,17 +1060,30 @@ def _start_background_process(self, clean=None, extra_args=[]):
encoding="utf-8",
)

self._send_initial_input()
# read success acknowledgement
ack = self._proc.stdout.readline()

# setup asynchronous output listeners
Thread(target=self._listen_stdout, args=(self._proc.stdout,), daemon=True).start()
Thread(target=self._listen_stderr, args=(self._proc.stderr,), daemon=True).start()

# only attempt initial input if process started nicely,
# otherwise can't read the error from stderr
if ack.strip() == PROCESS_ACK:
self._send_initial_input()
else:
get_shell().print_error(
f"INTERNAL ERROR, got {ack!r} instead of {PROCESS_ACK!r}\n---\n"
)

def _send_initial_input(self) -> None:
# Used for sending data sending for startup, which can't be send by other means
# (e.g. don't want the password to end up in logs)

pass

def _get_launch_cwd(self):
return self.get_cwd() if self.uses_local_filesystem() else None
return get_workbench().get_local_cwd()

def _get_launcher_with_args(self):
raise NotImplementedError()
Expand Down

0 comments on commit 4104729

Please sign in to comment.