KAIMONSLATE_PORT is captured at precompile time, so a launcher can't pin the hub port #6

Closed
opened 2026-07-20 02:52:50 -07:00 by disberd · 0 comments
disberd commented 2026-07-20 02:52:50 -07:00 (Migrated from github.com)

Hello, I found a potential bug in the custom port customization for the KaimonSlate server. Here is the AI assisted report below

Summary

The hub port is defined as a top-level const that reads the environment in the module
body (src/KaimonSlate.jl:148):

const _PORT = something(tryparse(Int, get(ENV, "KAIMONSLATE_PORT", "8765")), 8765)

Top-level module code is evaluated during precompilation, so this value is baked into
the precompiled image and is not re-read when the module loads in a new process
(see the Julia manual,
Module initialization and precompilation,
which is why runtime state like environment variables must be read from __init__).
KAIMONSLATE_PORT therefore only takes effect if it happened to be set in the environment
that produced the .ji; setting it in the launcher/process environment at start has no
effect once a precompiled cache exists. This contradicts the comment just above it
(lines 146–147: "configurable via the KAIMONSLATE_PORT env var … so a config UI / launcher
can pin it
").

It's especially easy to hit with the Kaimon-managed extension environment (Kaimon.jl #65):
KaimonSlate precompiles on the first using inside the spawned extension, so whichever
environment that first spawn had fixes the port for every later spawn — even though each
spawn inherits the (correct) launcher env. Observed: the process's /proc/<pid>/environ
shows KAIMONSLATE_PORT=<TARGET PORT>, while the hub log line reads
Kaimon Slate hub url = http://127.0.0.1:8765 (the baked-in default).

Affected code

src/KaimonSlate.jl:148 (the const), consumed at:

  • _base()src/KaimonSlate.jl:152
  • _hub()start_hub(; port = _PORT)src/KaimonSlate.jl:157
  • the start-failure message — src/app.jl:711

Expected

KAIMONSLATE_PORT is read at process start, so a launcher / config UI can pin the port
reliably regardless of the precompile-time environment.

Working fix (tried locally on my fork)

Reading the port at runtime in __init__ (which is skipped during precompilation), holding
it in a Ref, fixes it for me:

const _PORT = Ref(8765)

function __init__()
    _PORT[] = something(tryparse(Int, get(ENV, "KAIMONSLATE_PORT", "8765")), 8765)
    # …existing body…
end

and using _PORT[] at the call sites. Happy to open a PR.

Hello, I found a potential bug in the custom port customization for the KaimonSlate server. Here is the AI assisted report below ## Summary The hub port is defined as a top-level `const` that reads the environment in the module body (`src/KaimonSlate.jl:148`): ```julia const _PORT = something(tryparse(Int, get(ENV, "KAIMONSLATE_PORT", "8765")), 8765) ``` Top-level module code is evaluated during **precompilation**, so this value is baked into the precompiled image and is **not** re-read when the module loads in a new process (see the Julia manual, [Module initialization and precompilation](https://docs.julialang.org/en/v1/manual/modules/#Module-initialization-and-precompilation), which is why runtime state like environment variables must be read from `__init__`). `KAIMONSLATE_PORT` therefore only takes effect if it happened to be set in the environment that produced the `.ji`; setting it in the launcher/process environment at start has no effect once a precompiled cache exists. This contradicts the comment just above it (lines 146–147: "*configurable via the KAIMONSLATE_PORT env var … so a config UI / launcher can pin it*"). It's especially easy to hit with the Kaimon-managed extension environment (Kaimon.jl #65): KaimonSlate precompiles on the first `using` inside the spawned extension, so whichever environment that first spawn had fixes the port for every later spawn — even though each spawn inherits the (correct) launcher env. Observed: the process's `/proc/<pid>/environ` shows `KAIMONSLATE_PORT=<TARGET PORT>`, while the hub log line reads `Kaimon Slate hub url = http://127.0.0.1:8765` (the baked-in default). ## Affected code `src/KaimonSlate.jl:148` (the const), consumed at: - `_base()` — `src/KaimonSlate.jl:152` - `_hub()` → `start_hub(; port = _PORT)` — `src/KaimonSlate.jl:157` - the start-failure message — `src/app.jl:711` ## Expected `KAIMONSLATE_PORT` is read at process start, so a launcher / config UI can pin the port reliably regardless of the precompile-time environment. ## Working fix (tried locally on my fork) Reading the port at runtime in `__init__` (which is skipped during precompilation), holding it in a `Ref`, fixes it for me: ```julia const _PORT = Ref(8765) function __init__() _PORT[] = something(tryparse(Int, get(ENV, "KAIMONSLATE_PORT", "8765")), 8765) # …existing body… end ``` and using `_PORT[]` at the call sites. Happy to open a PR.
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
kahliburke/KaimonSlate.jl#6
No description provided.