From 16e99cb58b29ec2cab314d379e45f13e85b27a35 Mon Sep 17 00:00:00 2001 From: High Date: Thu, 28 Nov 2024 14:14:39 +0100 Subject: [PATCH 1/2] Win Support for LocalProcessProxy Basic Windows support for LocalProcessProxy --- enterprise_gateway/services/kernels/handlers.py | 12 +++++++++++- .../services/processproxies/processproxy.py | 4 ++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/enterprise_gateway/services/kernels/handlers.py b/enterprise_gateway/services/kernels/handlers.py index 8f4299a4a..aa9df51e0 100644 --- a/enterprise_gateway/services/kernels/handlers.py +++ b/enterprise_gateway/services/kernels/handlers.py @@ -58,7 +58,17 @@ async def post(self): raise tornado.web.HTTPError(400) # Transfer inherited environment variables from current process - env = {key: value for key, value in os.environ.items() if key in self.inherited_envs} + if os.name == "nt": + # We need to ensure we pass all needed environment variables to the kernel + # Users can use inherited_envs to add custom envrioment variables, + # and we ensure PATH, SYSTEMROOT, TEMP, USERPROFILE, WINDIR, COMSPEC, APPDATA, LOCALAPPDATA, PROGRAMDATA and PROGRAMFILES are included + # this allows a more out-of-the-box experience for users + required_envs: set[str] = {"PATH", "SYSTEMROOT", "TEMP", "USERPROFILE", "WINDIR", "COMSPEC", + "APPDATA", "LOCALAPPDATA", "PROGRAMDATA", "PROGRAMFILES"} + env = {key: value for key, value in os.environ.items() if + key in self.inherited_envs or key in required_envs} + else: + env = {key: value for key, value in os.environ.items() if key in self.inherited_envs} # Allow all KERNEL_* envs and those specified in client_envs and set from client. If this EG # instance is configured to accept all envs in the payload (i.e., client_envs == '*'), go ahead diff --git a/enterprise_gateway/services/processproxies/processproxy.py b/enterprise_gateway/services/processproxies/processproxy.py index 405adfbca..fe49be0eb 100644 --- a/enterprise_gateway/services/processproxies/processproxy.py +++ b/enterprise_gateway/services/processproxies/processproxy.py @@ -1043,6 +1043,8 @@ def __init__(self, kernel_manager: RemoteKernelManager, proxy_config: dict): # """Initialize the proxy.""" super().__init__(kernel_manager, proxy_config) kernel_manager.ip = localinterfaces.LOCALHOST + if os.name == "nt": + self.win32_interrupt_event = None async def launch_process( self, kernel_cmd: str, **kwargs: dict[str, Any] | None @@ -1059,6 +1061,8 @@ async def launch_process( except OSError: pass self.ip = local_ip + if os.name == "nt": # if operating system is Windows then link the win32_interrupt_event from the kernel + self.win32_interrupt_event = self.local_proc.win32_interrupt_event self.log.info( "Local kernel launched on '{}', pid: {}, pgid: {}, KernelID: {}, cmd: '{}'".format( self.ip, self.pid, self.pgid, self.kernel_id, kernel_cmd From 70f7469120f2900ec840207a4c80389f5312af9e Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 28 Nov 2024 14:04:49 +0000 Subject: [PATCH 2/2] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- .../services/kernels/handlers.py | 25 +++++++++++++++---- .../services/processproxies/processproxy.py | 4 ++- 2 files changed, 23 insertions(+), 6 deletions(-) diff --git a/enterprise_gateway/services/kernels/handlers.py b/enterprise_gateway/services/kernels/handlers.py index aa9df51e0..15af66e5e 100644 --- a/enterprise_gateway/services/kernels/handlers.py +++ b/enterprise_gateway/services/kernels/handlers.py @@ -63,12 +63,27 @@ async def post(self): # Users can use inherited_envs to add custom envrioment variables, # and we ensure PATH, SYSTEMROOT, TEMP, USERPROFILE, WINDIR, COMSPEC, APPDATA, LOCALAPPDATA, PROGRAMDATA and PROGRAMFILES are included # this allows a more out-of-the-box experience for users - required_envs: set[str] = {"PATH", "SYSTEMROOT", "TEMP", "USERPROFILE", "WINDIR", "COMSPEC", - "APPDATA", "LOCALAPPDATA", "PROGRAMDATA", "PROGRAMFILES"} - env = {key: value for key, value in os.environ.items() if - key in self.inherited_envs or key in required_envs} + required_envs: set[str] = { + "PATH", + "SYSTEMROOT", + "TEMP", + "USERPROFILE", + "WINDIR", + "COMSPEC", + "APPDATA", + "LOCALAPPDATA", + "PROGRAMDATA", + "PROGRAMFILES", + } + env = { + key: value + for key, value in os.environ.items() + if key in self.inherited_envs or key in required_envs + } else: - env = {key: value for key, value in os.environ.items() if key in self.inherited_envs} + env = { + key: value for key, value in os.environ.items() if key in self.inherited_envs + } # Allow all KERNEL_* envs and those specified in client_envs and set from client. If this EG # instance is configured to accept all envs in the payload (i.e., client_envs == '*'), go ahead diff --git a/enterprise_gateway/services/processproxies/processproxy.py b/enterprise_gateway/services/processproxies/processproxy.py index fe49be0eb..eb62f83c4 100644 --- a/enterprise_gateway/services/processproxies/processproxy.py +++ b/enterprise_gateway/services/processproxies/processproxy.py @@ -1061,7 +1061,9 @@ async def launch_process( except OSError: pass self.ip = local_ip - if os.name == "nt": # if operating system is Windows then link the win32_interrupt_event from the kernel + if ( + os.name == "nt" + ): # if operating system is Windows then link the win32_interrupt_event from the kernel self.win32_interrupt_event = self.local_proc.win32_interrupt_event self.log.info( "Local kernel launched on '{}', pid: {}, pgid: {}, KernelID: {}, cmd: '{}'".format(