Skip to content

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

Merged
jeevb merged 2 commits into
mainfrom
jeev/fix-code-server-bind-port-override
Jul 7, 2026
Merged

jeevb merged 2 commits into
mainfrom
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:{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.

jeevb added 2 commits July 7, 2026 14:17
code-server also reads the PORT env var when resolving its bind address.
An inherited PORT (e.g. injected by Kubernetes) would override the explicit
--bind-addr flag, so pin PORT to VSCODE_PORT for the code-server subprocess.

- execute_command now accepts an optional env dict, merged on top of
  os.environ and passed to create_subprocess_shell.
- _start_vscode_server passes env={"PORT": str(VSCODE_PORT)} and uses the
  VSCODE_PORT constant for --bind-addr.
- Collapse the erroneous double asyncio.run in the child-process target to a
  single call while threading env through.

Ports flytekit PR #3450 to the async flyte-sdk debug helpers.

Signed-off-by: Jeev B <jeevb@users.noreply.github.com>
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, so child_process.start() would raise
PicklingError and the code-server debug feature would fail in-container.

- Hoist the target to a module-level _run_code_server function.
- Add a regression test that exercises _start_vscode_server, asserting the
  child process pins PORT to VSCODE_PORT and that the target is picklable
  (guards against reintroducing a lambda).

Addresses code-review findings on PR branch.

Signed-off-by: Jeev B <jeevb@users.noreply.github.com>
@jeevb
jeevb force-pushed the jeev/fix-code-server-bind-port-override branch from cdd188b to 739c9b3 Compare July 7, 2026 20:17
@jeevb
jeevb enabled auto-merge (squash) July 7, 2026 20:19
@jeevb
jeevb merged commit c18edb1 into main Jul 7, 2026
41 checks passed
@jeevb
jeevb deleted the jeev/fix-code-server-bind-port-override branch July 7, 2026 20:41
samhita-alla pushed a commit that referenced this pull request Jul 15, 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>
Signed-off-by: Samhita Alla <aallasamhita@gmail.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