claude-code icon indicating copy to clipboard operation
claude-code copied to clipboard

[BUG] Background tasks ignore $TMPDIR and hardcode /tmp/claude/

Open Butanium opened this issue 1 month ago • 14 comments

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

  1. Have $TMPDIR set to a valid, writable directory (e.g., /run/user/$(id -u))
  2. Ensure /tmp is not writable by the current user
  3. Run a background task in Claude Code (e.g., run_in_background: true on 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.

Butanium avatar Dec 29 '25 15:12 Butanium

see also https://github.com/anthropics/claude-code/issues/15637 for same issue on a diff platform

Butanium avatar Dec 29 '25 15:12 Butanium

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

salviz avatar Dec 30 '25 12:12 salviz

@claude any fix you'd suggest for this?

Butanium avatar Jan 05 '26 12:01 Butanium

It seems this propagated to agents, can't run them this morning, most likely due to 2.1.0

Lectem avatar Jan 08 '26 07:01 Lectem

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 avatar Jan 08 '26 10:01 andersglindstrom

@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

Butanium avatar Jan 08 '26 10:01 Butanium

I can confirm replacing the hardcoded path with a usable one fixed subagents.

Lectem avatar Jan 08 '26 10:01 Lectem

2.1.5 seems to have made things worse and patch no longer works.

Lectem avatar Jan 12 '26 06:01 Lectem

@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.

Butanium avatar Jan 12 '26 11:01 Butanium

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.

Lectem avatar Jan 13 '26 06:01 Lectem