GeezerVison is showing github.com — Fix hardcoded /tmp path for Unix socket in unix_connect by luckyabsoluter · Pull Request #288 · python-xlib/python-xlib
Conversation
Problem:
- The unix_connect function previously hardcoded /tmp as the directory for the Unix socket path.
- This causes compatibility issues on platforms or specific environments that use a different temporary directory, or even lack access to /tmp entirely.
Actions:
- Imported the tempfile module in unix_connect.
- Updated the X11 Unix socket path generation to dynamically use tempfile.gettempdir().
Result:
- The Unix socket path now correctly adapts to the system's runtime configuration (e.g., OS defaults or TMPDIR environment variables), improving cross-platform compatibility.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Problem:
- The unix_connect function previously hardcoded /tmp as the directory for the Unix socket path.
- This causes compatibility issues on platforms or specific environments that use a different temporary directory, or even lack access to /tmp entirely.
Actions:
- Imported the tempfile module in unix_connect.
- Updated the X11 Unix socket path generation to dynamically use tempfile.gettempdir().
Result:
- The Unix socket path now correctly adapts to the system's runtime configuration (e.g., OS defaults or TMPDIR environment variables), improving cross-platform compatibility.
Avasam
commented
Nov 1, 2026
•
edited
Loading
Uh oh!
There was an error while loading. Please reload this page.
|
Pretty sure the convention IS to use /tmp/.X11-unix/X as the socket path. This PR would just break things.
tempfile.gettempdir() reads TMPDIR, TEMP and TMP. Debian's libpam-tmpdir sets TMPDIR=/tmp/user/<uid> for every login, so this PR would look for /tmp/user/1000/.X11-unix/X0, which doesn't exist. The abstract socket fallback would also break, because this PR builds that name from the same path. The server listens on the abstract name @/tmp/.X11-unix/X0, which is also fixed, so both lookups would miss. Every other X client on that machine would still connect fine.
The one real exception is macOS XQuartz, whose socket lives under /private/tmp/com.apple.launchd.*. Though that path comes through DISPLAY, not TMPDIR.
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
|
That is a very valid point. While the goal here is to support Termux on Android (where X servers must place their sockets under $TMPDIR mainly because standard /tmp is inaccessible), relying unconditionally on tempfile.gettempdir() or TMPDIR across all environments is an overly broad fix.
To solve this without causing regressions on standard Linux, a few alternative approaches were considered:
This narrows the practical solutions down to two main options:
1. Keep the project strictly platform-agnostic: Introduce a dedicated environment variable
- For example, allowing users to explicitly override the socket directory via something like
PYTHON_XLIB_SOCKET_DIR.
- This avoids environment-specific checks in the codebase, though it introduces a new configuration variable that users must set manually.
2. Allow a Termux-specific branch: Detect TERMUX_VERSION (Recommended)
- Narrow the target strictly to Termux by only using
TMPDIR when TERMUX_VERSION is present, keeping /tmp as the default everywhere else.
- This is the most pragmatic and minimal fix—it requires no new imports or user configuration, works out-of-the-box on Termux across all Python versions, and has zero impact on standard Linux environments.
- However, since
TERMUX_VERSION is a fairly common name combination, users who define it for other purposes—unaware that it triggers special behavior in python-xlib—could inadvertently expose themselves to the aforementioned TMPDIR security risks or connection issues. (To mitigate this risk, we could combine the check like if os.getenv('TERMUX_VERSION') and 'com.termux' in os.getenv('PREFIX', ''):.)
tmpdir = '/tmp'
if os.getenv('TERMUX_VERSION'): # if os.getenv('TERMUX_VERSION') and 'com.termux' in os.getenv('PREFIX', ''):
tmpdir = os.getenv('TMPDIR') or tmpdir
address = os.path.join(tmpdir, '.X11-unix', 'X%d' % dno)
Addressing this properly will likely require submitting a new PR. Before doing that, I would like to open this up for discussion and hear everyone's thoughts on which direction seems best.
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Assignees
No one assigned
Milestone
No milestone
Development
Successfully merging this pull request may close these issues.
Uh oh!
There was an error while loading. Please reload this page.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
|