Skip to content

fix(interactive): pin PORT env var so code-server binds to the correct port#3450

Merged
pingsutw merged 1 commit into
masterfrom
jeev/fix-code-server-bind-port-override
Jul 7, 2026
Merged

fix(interactive): pin PORT env var so code-server binds to the correct port#3450
pingsutw merged 1 commit into
masterfrom
jeev/fix-code-server-bind-port-override

Conversation

@jeevb

@jeevb jeevb commented Jul 7, 2026

Copy link
Copy Markdown
Contributor

Problem

When the pod/image has a PORT environment variable set (common in Kubernetes), the code-server subprocess binds to the wrong port despite being launched with --bind-addr 0.0.0.0:{self.port}.

Root cause

code-server reads the PORT environment variable as one of its bind-address sources. execute_command launches the subprocess via subprocess.Popen(cmd, shell=True, ...), which inherits the parent process's full environment — so an inherited PORT overrides the explicit --bind-addr flag.

Fix

  • execute_command now accepts an optional env dict, merged on top of os.environ (preserving other inherited vars) and passed to Popen(..., env=...).
  • The vscode decorator passes env={"PORT": str(self.port)} to the code-server subprocess, pinning PORT so an inherited value can't override --bind-addr.

Tests

  • test_vscode_passes_port_env_to_child_process — asserts the child process receives env={"PORT": "1234"} even when the surrounding environment has PORT=9999.
  • test_execute_command_passes_env_to_subprocess — asserts the override reaches Popen while other inherited vars survive.

All 19 interactive unit tests pass.

…t port

code-server reads the PORT environment variable as one of its bind-address
sources. When a pod/image sets PORT (common in Kubernetes), it overrode the
explicit --bind-addr flag and code-server bound to the wrong port.

Pass env={"PORT": str(self.port)} to the code-server subprocess so the
inherited PORT can't override --bind-addr. execute_command now accepts an
optional env dict that is merged on top of os.environ.

Signed-off-by: Jeev B <jeevb@users.noreply.github.com>
@jeevb jeevb force-pushed the jeev/fix-code-server-bind-port-override branch from fd83ca0 to 46a879e Compare July 7, 2026 17:39
@pingsutw pingsutw merged commit d2b2e45 into master Jul 7, 2026
56 checks passed
jeevb added a commit to flyteorg/flyte-sdk that referenced this pull request Jul 7, 2026
…#1286)

## Problem

When the pod/image has a `PORT` environment variable set (common in
Kubernetes), the `code-server` subprocess binds to the wrong port
despite being launched with `--bind-addr 0.0.0.0:{VSCODE_PORT}`.

## Root cause

`code-server` reads the `PORT` environment variable as one of its
bind-address sources. The subprocess inherits the parent process's full
environment, so an inherited `PORT` overrides the explicit `--bind-addr`
flag.

## Fix

- `execute_command` now accepts an optional `env` dict, merged on top of
`os.environ` (preserving other inherited vars) and passed to
`asyncio.create_subprocess_shell(..., env=...)`.
- `_start_vscode_server` passes `env={"PORT": str(VSCODE_PORT)}` to the
code-server subprocess, pinning `PORT` so an inherited value can't
override `--bind-addr`. The hardcoded `6060` is also replaced with the
existing `VSCODE_PORT` constant for consistency.

This ports flytekit PR flyteorg/flytekit#3450 to the async flyte-sdk
debug helpers.

## Additional fix (from code review)

The `multiprocessing` target was a lambda, which is **not picklable**
under the `spawn` (macOS default) and `forkserver` start methods. Python
3.14 makes `forkserver` the default on Linux (POSIX), so
`child_process.start()` would raise `PicklingError` and the code-server
debug feature would fail in-container. The target is hoisted to a
module-level `_run_code_server` function. (This also collapses a
pre-existing double-`asyncio.run` bug in the lambda body.)

## Tests

- `test_execute_command_passes_env_to_subprocess` — asserts the override
reaches `create_subprocess_shell` while other inherited vars survive.
- `test_execute_command_without_env_inherits_process_env` — asserts
`env=None` (inherit parent) when no override is given.
- `test_start_vscode_server_pins_port_env_with_picklable_target` —
drives `_start_vscode_server` and asserts the child process pins `PORT`
to `VSCODE_PORT`, the cmd uses `--bind-addr 0.0.0.0:6060`, and the
target is picklable (guards against reintroducing a lambda).

All 23 tests in `tests/flyte/test_debug.py` pass; lint and format clean.

---------

Signed-off-by: Jeev B <jeevb@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants