bug: Agent process environment leaks stale $SHELL into sessions after chsh in startup script
还没有人认领这个 Issue。
评估
调研方向
从 agent/agentssh/agentssh.go 中的 CommandEnv 开始,然后阅读 agent/agent.go 中的 updateCommandEnv,以确认继承的环境条目和显式解析的环境条目是如何排序和去重的。让新会话优先使用刚从 /etc/passwd 读取的 shell,然后验证 SSH、Web 终端和重新连接的 PTY 会话在 chsh 后会公开更新后的 $SHELL。
由索引模型根据 Issue 内容生成。
描述
Is there an existing issue for this?
- I have searched the existing issues
Current Behavior
On first workspace boot, the Coder agent process starts before the startup script runs. At that point, /etc/passwd has the default shell (/bin/bash), and the agent process inherits SHELL=/bin/bash in its own environment.
The startup script then runs chsh -s /usr/bin/zsh, which correctly updates /etc/passwd. However, the agent process's own environment (os.Environ()) still contains SHELL=/bin/bash for the lifetime of the process.
When any session is created (SSH, web terminal, reconnecting PTY), CommandEnv() in agent/agentssh/agentssh.go:
- Calls
ei.Shell(username)→ reads/etc/passwdfresh → returns/usr/bin/zsh✓ - Calls
ei.Environ()→ returnsos.Environ()from agent process → containsSHELL=/bin/bash✗
The env array is built with os.Environ() first (containing SHELL=/bin/bash), then SHELL=/usr/bin/zsh is appended from the fresh passwd read. This array is passed to updateCommandEnv() in agent/agent.go, which uses first-wins dedup:
for _, env := range current {
parts := strings.SplitN(env, "=", 2)
if len(parts) != 2 {
continue
}
if _, ok := envs[parts[0]]; !ok {
envs[parts[0]] = parts[1]
}
}
The stale SHELL=/bin/bash from os.Environ() is encountered first and inserted into the map. The correct SHELL=/usr/bin/zsh appended later is silently dropped because the key already exists.
The shell binary used to run the session command is correct (it comes from the shell return value of CommandEnv(), which uses the fresh /etc/passwd read). But the $SHELL environment variable visible inside the session is stale.
Expected Behavior
After chsh changes the login shell in /etc/passwd, all new sessions created by the agent should have $SHELL reflecting the updated value.
The freshly-read shell from /etc/passwd should take precedence over the agent process's inherited $SHELL. Possible fixes:
- In
CommandEnv(), filterSHELLout ofei.Environ()before appending the fresh value — since the fresh value is explicitly resolved from/etc/passwd, the process-inherited value should never compete with it. - Append
ei.Environ()after the explicitSHELL=entry so the first-wins logic picks the correct value. - Have
updateCommandEnv()use explicit-wins semantics for variables that are intentionally set byCommandEnv().
Steps to Reproduce
- Create a workspace template with a startup script that runs:
apt-get install -y zsh chsh -s /usr/bin/zsh $USER - Start the workspace (first boot)
- Connect via SSH (or web terminal) after startup completes
- Run
echo $SHELL→ shows/bin/bash(wrong) - Run
grep $USER /etc/passwd→ shows/usr/bin/zsh(correct in passwd)
Environment
- Host OS: Ubuntu (workspace image)
- Coder version: v2.x (tested on latest)
Additional Context
- The issue only affects the first boot — subsequent workspace restarts have the correct shell already in
/etc/passwdwhen the agent starts. - This affects all session types (SSH, web terminal, reconnecting PTY), not just SSH — they all go through
CommandEnv()→updateCommandEnv(). - The root cause is that the agent is a long-running process whose
os.Environ()is frozen at startup time, andupdateCommandEnv()'s first-wins dedup lets this stale value shadow the explicitly-resolved correct value.
- 主要语言
- Go
- 星标
- 16.6k
- 派生
- 1.6k
- 平均合并
- 1 天 23 小时
- 30 天内合并 PR
- 543
贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
coder/coder 的其他 Issue
-
bug: AI Gateway client filter lists "Unknown" twice when NULL and literal Unknown clients coexist 未关闭bug
难度 2/5 1-3 小时 新手友好度 90/100
-
feat(site): suppress the web terminal context menu when the application has enabled mouse tracking 未关闭
难度 2/5 1-3 小时 新手友好度 78/100
-
bug frontend
难度 2/5 1-3 小时 新手友好度 84/100
-
bug site
难度 2/5 1-3 小时 新手友好度 84/100
-
难度 2/5 1-3 小时 新手友好度 85/100
相似的 Issue
-
agentic-workflows
难度 2/5 1-3 小时 新手友好度 76/100
-
agentic-workflows
难度 2/5 1-3 小时 新手友好度 70/100
microsoft/agent-framework-go#1179 ·
-
bug
难度 2/5 1-3 小时 新手友好度 76/100
-
[Bug]: OLLAMA_KEEP_ALIVE="5m" / "24h" crashes Ollama embedding and vision models with ValueError 未关闭
难度 2/5 1-3 小时 新手友好度 75/100
infiniflow/ragflow#20223 · 1 个 reaction ·
-
bug needs triage pkg/translator/faro
难度 2/5 1-3 小时 新手友好度 88/100
open-telemetry/opentelemetry-collector-contrib#51484 · 1 条评论 ·