[BUG] Background tasks ignore $TMPDIR and hardcode /tmp/claude/
Description
Claude Code's background task system ignores the $TMPDIR environment variable and hardcodes /tmp/claude/ for task management, causing permission denied errors when /tmp is not writable by the user.
Steps to Reproduce
- Have
$TMPDIRset to a valid, writable directory (e.g.,/run/user/$(id -u)) - Ensure
/tmpis not writable by the current user - Run a background task in Claude Code (e.g.,
run_in_background: trueon a Bash command)
Expected Behavior
Claude Code should respect $TMPDIR and create its task directory there.
Actual Behavior
Claude Code ignores $TMPDIR and attempts to create /tmp/claude/<workspace-path>/tasks, resulting in:
EACCES: permission denied, mkdir '/tmp/claude/-mnt-nw-home-c-dumas-research-libs-nnsight/tasks'
Environment
- OS: Linux 6.8.0-60-generic (Ubuntu)
-
TMPDIR:
/run/user/2011(set and valid) - Claude Code version: Latest
Notes
This is distinct from existing issues like #10194 (TMPDIR pointing to non-existent directory) and #14799 (EXDEV with tmpfs). In this case, $TMPDIR is correctly set to a valid, writable directory, but Claude Code completely ignores it for the background task system.
see also https://github.com/anthropics/claude-code/issues/15637 for same issue on a diff platform
Additional Finding: Termux Path Sanitization Variant
A related issue has been discovered on Termux (Android) that stems from the same root cause. See #15801.
Termux-Specific Problem: The path sanitization logic (replacing / with -) creates invalid paths on systems with deeply nested HOME directories.
Example:
- HOME: `/data/data/com.termux/files/home`
- Sanitized: `-data-data-com-termux-files-home`
- Invalid Path: `/tmp/claude/-data-data-com-termux-files-home/tasks`
Both issues (#15700 and #15801) would be fixed by using XDG Base Directory Specification (`~/.cache/claude/tasks`) or properly hashing the HOME path instead of character replacement.
Platform: Termux on Android ARM64 Reporter: User with TalkBack accessibility needs
@claude any fix you'd suggest for this?
It seems this propagated to agents, can't run them this morning, most likely due to 2.1.0
To work around this problem, I did this:
:; claude --version
2.1.1 (Claude Code)
This is the version with the bug.
First thing, disable auto update by adding this to ~/.claude/settings.json:
"env": {
"DISABLE_AUTOUPDATER": "1"
}
If you are still on version 2.0.76 you're good.
If you are on 2.1.1 then you also have to do this:
:; npm install @anthropic-ai/[email protected]
And then verify:
:; claude --version
2.0.76 (Claude Code)
@andersglindstrom this solution was described in the termux post and worked for me:
sed -i "s|/tmp/claude|/data/data/com.{CUSTOM_TMP_PATH}|g" cli.js
I asked Claude to find cli.js referencing the issue and I have a custom command like this that I run after each update
I can confirm replacing the hardcoded path with a usable one fixed subagents.
2.1.5 seems to have made things worse and patch no longer works.
@Lectem we investigated this and there is now a fix: The fix is working (directories exist). What exactly is the issue you're experiencing? Can you describe what "doesn't work anymore" means - is there an error message?
Let me also check if there are newer patterns in the code that might need patching:
● Bash(grep -c 'CLAUDE_CODE_TMPDIR' "/mnt/nw/home/c.dumas/.nvm/versions/node/v22.17.0/lib/node_modules/@anthropic-ai/claude-code/cli.js") ⎿ 3
● Good news - newer versions of Claude Code support CLAUDE_CODE_TMPDIR environment variable! Instead of patching the binary, you can now use this cleaner approach:
export CLAUDE_CODE_TMPDIR=/run/user/$(id -u)
Add this to your ~/.bashrc or ~/.zshrc and it should work without needing to patch cli.js after each update.
What specific error are you seeing? That will help me determine if there's another issue at play.
I was getting such messages:
Bash(rm -rf build && CMAKE_C_COMPILER_LAUNCHER=ccache CMAKE_CXX_COMPILER_LAUNCHER=ccache cmake -B build -G Ninja 2>&1 | tail -20) ⎿ Error: EACCES: permission denied, mkdir '/tmp/claude/-data-data-com-termux-files-h ome-code-myproject/tasks'
And the workaround mentioned here https://github.com/anthropics/claude-code/issues/15700#issuecomment-3723281340 would make Claude code freeze after a few seconds. (it could also be another bug of 2.1.5 I guess)
In any case it seems that CLAUDE_CODE_TMPDIR works, this should be documented or set properly to read $TMPDIR by default.